Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175475
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 501
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-26 11:00
文章分类

全部博文(33)

文章存档

2014年(11)

2013年(22)

我的朋友

分类: LINUX

2013-07-02 18:21:31

开源代码

  • json-c-0.9.tar.gz

自己封装的基础函数

代码


点击(此处)折叠或打开

  1. int print_tab(int i)
  2. {
  3.     for (; i>0; --i)
  4.     {
  5.         printf("\t");
  6.     }

  7.     return 0;
  8. }

  9. int json_tree_print(struct json_object *obj, const char *key, int level)
  10. {
  11.     json_type type = json_object_get_type(obj);
  12.     int i = 0;


  13.     //for object
  14.     if (type == json_type_object)
  15.     {
  16.         if (key)
  17.         {
  18.             print_tab(level);
  19.             printf("%s:\n", key);
  20.         }
  21.         print_tab(level);
  22.         printf("{\n");
  23.         level++;

  24.         json_object_object_foreach(obj, key, val) {
  25.             struct json_object *tmp_obj = val;
  26.             json_tree_print(tmp_obj, key, level);
  27.         }

  28.         level--;
  29.         print_tab(level);
  30.         printf("}\n");

  31.     }
  32.     //for array
  33.     else if ((type == json_type_array))
  34.     {
  35.         if (key)
  36.         {
  37.             print_tab(level);
  38.             printf("%s:\n", key);
  39.         }

  40.         print_tab(level);
  41.         printf("[\n");
  42.         level++;

  43.         for(i=0; i < json_object_array_length(obj); i++) {
  44.             struct json_object *array_obj = json_object_array_get_idx(obj, i);
  45.             json_tree_print(array_obj, NULL, level);
  46.         }
  47.         
  48.         level--;
  49.         print_tab(level);
  50.         printf("]\n");

  51.     }
  52.     else
  53.     {
  54.         print_tab(level);
  55.         printf("%s:%s\n", key, json_object_to_json_string(obj));
  56.     }

  57.     return 0;


  58. }

示例


点击(此处)折叠或打开

  1. int main(int argc, char **argv)
  2. {
  3.     struct json_object *root_obj;

  4.     //init
  5.     root_obj = json_tokener_parse(
  6.         "{" \
  7.         " \"switch\": { " \
  8.         " \"ip\":\"192.168.1.1\"," \
  9.         " \"mac\":\"00:11:22:33:44:55\" " \
  10.         " }, " \
  11.         " \"vlan\": { " \
  12.         " \"algo-table\":{" \
  13.         " \"1\":{" \
  14.         " \"src-ip\":1," \
  15.         " \"src-l2-port\":0," \
  16.         " \"src-l4-port\":1" \
  17.         " }" \
  18.         " }," \
  19.         " \"vlan-table\":{" \
  20.         " \"1\":{"
  21.         " \"vid\":123, " \
  22.         " \"port\":\"1-7\" " \
  23.         " }" \
  24.         " }" \
  25.         " }" \
  26.         "}" \
  27.     );

  28.     json_tree_print(root_obj, NULL, 0);

  29.     //release resource
  30.     json_object_put(root_obj);

  31.     return 0;

  32. }

示例的输出


  1. {
  2.     switch:
  3.     {
  4.         ip:"192.168.1.1"
  5.         mac:"00:11:22:33:44:55"
  6.     }
  7.     vlan:
  8.     {
  9.         algo-table:
  10.         {
  11.             1:
  12.             {
  13.                 src-ip:1
  14.                 src-l2-port:0
  15.                 src-l4-port:1
  16.             }
  17.         }
  18.         vlan-table:
  19.         {
  20.             1:
  21.             {
  22.                 vid:123
  23.                 port:"1-7"
  24.             }
  25.         }
  26.     }
  27. }

查找指定的json对象


点击(此处)折叠或打开

  1. typedef struct _key_obj{
  2.     char *key;
  3.     struct json_object *obj;
  4.     struct json_object *parent_obj;

  5. }key_obj;

  6. /*
  7. function description:
  8. if the value of input str is : "vlan.algo-table.1.src-ip", the output top_node_buf will be "vlan"
  9. if the value of input str is : "algo-table.1.src-ip", the output top_node_buf will be "algo-table"
  10. */
  11. int get_top_node(const char *str, char *top_node_buf, int buf_len)
  12. {
  13.     const char *p_start = NULL;
  14.     const char *p_end = NULL;

  15.     if (!str || !top_node_buf)
  16.     {
  17.         return -1;//fail
  18.     }

  19.     p_start = str;
  20.     p_end = strchr(str, '.');

  21.     if (!p_end)
  22.     {
  23.         snprintf(top_node_buf, buf_len, "%s", str);
  24.     }
  25.     else
  26.     {
  27.         int i = 0;
  28.         int len = p_end - p_start;
  29.         
  30.         if ((len) > (buf_len-1))
  31.         {
  32.             return -1;//no enough buffer
  33.         }

  34.         for (i=0; i<len; ++i)
  35.         {
  36.             top_node_buf[i] = p_start[i];
  37.         }
  38.         top_node_buf[i] = '\0';;
  39.     }

  40.     return 0;
  41. }


  42. /*
  43. return key and json object, so we can change the value

  44. only work for json_type_object, for json_type_array, the function will not work
  45. */
  46. int json_get_obj(struct json_object *obj, const char *str, key_obj* output_obj)
  47. {
  48.     json_type type = json_object_get_type(obj);
  49.     char top_node[128] = {0};
  50.     const char *p = NULL;
  51.     int match = 0;
  52.     int i = 0;

  53.     if (!obj || !str)
  54.     {
  55.         return -1;
  56.     }

  57.     if (get_top_node(str, top_node, sizeof(top_node)) != 0)
  58.     {
  59.         return -1;
  60.     }

  61.     p = strchr(str, '.');
  62.     //for object
  63.     if (type == json_type_object)
  64.     {
  65.         json_object_object_foreach(obj, key, val) {
  66.             struct json_object *tmp_obj = val;
  67.             if (!strcmp(top_node, key))
  68.             {
  69.                 match = 1;
  70.                 //leaf now,return node obj
  71.                 if (!p)
  72.                 {
  73.                     output_obj->key = key;
  74.                     output_obj->obj = tmp_obj;
  75.                     output_obj->parent_obj = obj;
  76.                     return 0;
  77.                 }
  78.                 else
  79.                 {
  80.                     return json_get_obj(tmp_obj, ++p, output_obj);
  81.                 }
  82.                 break;
  83.             }
  84.         }

  85.         if (!match)
  86.         {
  87.             return -1;
  88.         }
  89.     }

  90.     return 0;

  91. }


通过文件内容来构造json对象

有现成的函数:

点击(此处)折叠或打开

  1. struct json_object* json_object_from_file(char *filename)

把json对象转化成字符串并保存到文件中

有现成的函数:

点击(此处)折叠或打开

  1. int json_object_to_file(char *filename, struct json_object *obj)

从json对象中读取指定路径的值

代码


点击(此处)折叠或打开

  1. const char * json_get_value(struct json_object *root_obj, const char *str)
  2. {
  3.     key_obj node;

  4.     if (json_get_obj(root_obj, str, &node) != 0)
  5.     {
  6.         return NULL;
  7.     }

  8.     return (const char *)json_object_get_string(node.obj);
  9. }

示例


点击(此处)折叠或打开

  1. int main(int argc, char **argv)
  2. {
  3.     struct json_object *root_obj;

  4.     //init
  5.     root_obj = json_tokener_parse(
  6.         "{" \
  7.         " \"switch\": { " \
  8.         " \"ip\":\"192.168.1.1\"," \
  9.         " \"mac\":\"00:11:22:33:44:55\" " \
  10.         " }, " \
  11.         " \"vlan\": { " \
  12.         " \"algo-table\":{" \
  13.         " \"1\":{" \
  14.         " \"src-ip\":1," \
  15.         " \"src-l2-port\":0," \
  16.         " \"src-l4-port\":1" \
  17.         " }" \
  18.         " }," \
  19.         " \"vlan-table\":{" \
  20.         " \"1\":{"
  21.         " \"vid\":123, " \
  22.         " \"port\":\"1-7\" " \
  23.         " }" \
  24.         " }" \
  25.         " }" \
  26.         "}" \
  27.     );

  28.     printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));

  29.     //release resource
  30.     json_object_put(root_obj);

  31.     return 0;

  32. }

输出:


  1. get value:[1-7]

在json树中修改指定路径的值

代码:

点击(此处)折叠或打开

  1. int json_set_value(struct json_object *root_obj, const char *str, const char *val)
  2. {
  3.     key_obj node;
  4.     char key[64] = {0};
  5.     struct json_object *obj = NULL;

  6.     if (json_get_obj(root_obj, str, &node) != 0)
  7.     {
  8.         return -1;
  9.     }

  10.     obj = json_object_new_string(val);
  11.     snprintf(key, sizeof(key), "%s", node.key);
  12.     json_object_object_add(node.parent_obj, key, obj);

  13.     return 0;
  14. }
示例:

点击(此处)折叠或打开

  1. int main(int argc, char **argv)
  2. {
  3.     struct json_object *root_obj;

  4.     //init
  5.     root_obj = json_tokener_parse(
  6.         "{" \
  7.         " \"switch\": { " \
  8.         " \"ip\":\"192.168.1.1\"," \
  9.         " \"mac\":\"00:11:22:33:44:55\" " \
  10.         " }, " \
  11.         " \"vlan\": { " \
  12.         " \"algo-table\":{" \
  13.         " \"1\":{" \
  14.         " \"src-ip\":1," \
  15.         " \"src-l2-port\":0," \
  16.         " \"src-l4-port\":1" \
  17.         " }" \
  18.         " }," \
  19.         " \"vlan-table\":{" \
  20.         " \"1\":{"
  21.         " \"vid\":123, " \
  22.         " \"port\":\"1-7\" " \
  23.         " }" \
  24.         " }" \
  25.         " }" \
  26.         "}" \
  27.     );

  28.     printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));
  29.     json_set_value(root_obj,"vlan.vlan-table.1.port", "8,9" );
  30.     printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));

  31.     //release resource
  32.     json_object_put(root_obj);

  33.     return 0;

  34. }

输出:

点击(此处)折叠或打开

  1. get value:[1-7]
  2. get value:[8,9]

往json树的指定路径中添加新的对象


点击(此处)折叠或打开

  1. int json_add_json_obj(struct json_object *root_obj, const char *path,
  2.     const char *key, struct json_object *new_obj)
  3. {
  4.     key_obj node;

  5.     if (json_get_obj(root_obj, path, &node) != 0)
  6.     {
  7.         return -1;
  8.     }

  9.     json_object_object_add(node.obj, key, new_obj);

  10.     return 0;
  11. }

示例:

点击(此处)折叠或打开

  1. int main(int argc, char **argv)
  2. {
  3.     struct json_object *root_obj, *new_obj;
  4.     

  5.     int i;
  6.     int ret = -1;

  7.     //init
  8.     root_obj = json_tokener_parse(
  9.         "{" \
  10.         " \"switch\": { " \
  11.         " \"ip\":\"192.168.1.1\"," \
  12.         " \"mac\":\"00:11:22:33:44:55\" " \
  13.         " }, " \
  14.         " \"vlan\": { " \
  15.         " \"algo-table\":{" \
  16.         " \"1\":{" \
  17.         " \"src-ip\":1," \
  18.         " \"src-l2-port\":0," \
  19.         " \"src-l4-port\":1" \
  20.         " }" \
  21.         " }," \
  22.         " \"vlan-table\":{" \
  23.         " \"1\":{"
  24.         " \"vid\":123, " \
  25.         " \"port\":\"1-7\" " \
  26.         " }" \
  27.         " }" \
  28.         " }" \
  29.         "}" \
  30.     );

  31.     new_obj = json_tokener_parse(
  32.         "{" \
  33.         " \"vid\":234," \
  34.         " \"port\":\"8,9\"" \
  35.         "}" \
  36.     );

  37.     printf("-----------------before adding-----------------------------------\n");
  38.     json_tree_print(root_obj, NULL, 0);
  39.     
  40.     json_add_json_obj(root_obj,"vlan.vlan-table","2",new_obj);
  41.     
  42.     printf("-----------------after adding-----------------------------------\n");
  43.     json_tree_print(root_obj, NULL, 0);

  44.     //release resource
  45.     json_object_put(root_obj);

  46.     return 0;

  47. }
输出:

点击(此处)折叠或打开

  1. -----------------before adding-----------------------------------
  2. {
  3.     switch:
  4.     {
  5.         ip:"192.168.1.1"
  6.         mac:"00:11:22:33:44:55"
  7.     }
  8.     vlan:
  9.     {
  10.         algo-table:
  11.         {
  12.             1:
  13.             {
  14.                 src-ip:1
  15.                 src-l2-port:0
  16.                 src-l4-port:1
  17.             }
  18.         }
  19.         vlan-table:
  20.         {
  21.             1:
  22.             {
  23.                 vid:123
  24.                 port:"1-7"
  25.             }
  26.         }
  27.     }
  28. }
  29. -----------------after adding-----------------------------------
  30. {
  31.     switch:
  32.     {
  33.         ip:"192.168.1.1"
  34.         mac:"00:11:22:33:44:55"
  35.     }
  36.     vlan:
  37.     {
  38.         algo-table:
  39.         {
  40.             1:
  41.             {
  42.                 src-ip:1
  43.                 src-l2-port:0
  44.                 src-l4-port:1
  45.             }
  46.         }
  47.         vlan-table:
  48.         {
  49.             1:
  50.             {
  51.                 vid:123
  52.                 port:"1-7"
  53.             }
  54.             2:
  55.             {
  56.                 vid:234
  57.                 port:"8,9"
  58.             }
  59.         }
  60.     }
  61. }

在json树中删除指定路径的对象

代码:

点击(此处)折叠或打开

  1. int json_del_json_obj(struct json_object *root_obj, const char *path)
  2. {
  3.     key_obj node;

  4.     if (json_get_obj(root_obj, path, &node) != 0)
  5.     {
  6.         return -1;
  7.     }

  8.     json_object_object_del(node.parent_obj, node.key);

  9.     return 0;
  10. }

示例:

点击(此处)折叠或打开

  1. int main(int argc, char **argv)
  2. {
  3.     struct json_object *root_obj, *new_obj;
  4.     

  5.     int i;
  6.     int ret = -1;

  7.     //init
  8.     root_obj = json_tokener_parse(
  9.         "{" \
  10.         " \"switch\": { " \
  11.         " \"ip\":\"192.168.1.1\"," \
  12.         " \"mac\":\"00:11:22:33:44:55\" " \
  13.         " }, " \
  14.         " \"vlan\": { " \
  15.         " \"algo-table\":{" \
  16.         " \"1\":{" \
  17.         " \"src-ip\":1," \
  18.         " \"src-l2-port\":0," \
  19.         " \"src-l4-port\":1" \
  20.         " }" \
  21.         " }," \
  22.         " \"vlan-table\":{" \
  23.         " \"1\":{"
  24.         " \"vid\":123, " \
  25.         " \"port\":\"1-7\" " \
  26.         " }" \
  27.         " }" \
  28.         " }" \
  29.         "}" \
  30.     );

  31.     new_obj = json_tokener_parse(
  32.         "{" \
  33.         " \"vid\":234," \
  34.         " \"port\":\"8,9\"" \
  35.         "}" \
  36.     );

  37.     printf("-----------------before deleting-----------------------------------\n");
  38.     json_tree_print(root_obj, NULL, 0);
  39.     
  40.     json_del_json_obj(root_obj,"vlan.vlan-table.1");
  41.     
  42.     printf("-----------------after deleting-----------------------------------\n");
  43.     json_tree_print(root_obj, NULL, 0);

  44.     //release resource
  45.     json_object_put(root_obj);

  46.     return 0;

  47. }

输出:

点击(此处)折叠或打开

  1. -----------------before deleting-----------------------------------
  2. {
  3.     switch:
  4.     {
  5.         ip:"192.168.1.1"
  6.         mac:"00:11:22:33:44:55"
  7.     }
  8.     vlan:
  9.     {
  10.         algo-table:
  11.         {
  12.             1:
  13.             {
  14.                 src-ip:1
  15.                 src-l2-port:0
  16.                 src-l4-port:1
  17.             }
  18.         }
  19.         vlan-table:
  20.         {
  21.             1:
  22.             {
  23.                 vid:123
  24.                 port:"1-7"
  25.             }
  26.         }
  27.     }
  28. }
  29. -----------------after deleting-----------------------------------
  30. {
  31.     switch:
  32.     {
  33.         ip:"192.168.1.1"
  34.         mac:"00:11:22:33:44:55"
  35.     }
  36.     vlan:
  37.     {
  38.         algo-table:
  39.         {
  40.             1:
  41.             {
  42.                 src-ip:1
  43.                 src-l2-port:0
  44.                 src-l4-port:1
  45.             }
  46.         }
  47.         vlan-table:
  48.         {
  49.         }
  50.     }
  51. }












阅读(3832) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~