-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.c
123 lines (104 loc) · 2.85 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include "json.h"
const char *json_get_string(json_object *o, const char *key)
{
json_object *tmp;
if (json_object_object_get_ex(o, key, &tmp))
if (json_object_is_type(tmp, json_type_string))
return json_object_get_string(tmp);
return NULL;
}
int json_get_int(json_object *o, const char *key, int defval)
{
json_object *tmp;
if (json_object_object_get_ex(o, key, &tmp))
if (json_object_is_type(tmp, json_type_int))
return json_object_get_int(tmp);
return defval;
}
double json_get_double(json_object *o, const char *key, double defval)
{
json_object *tmp;
if (json_object_object_get_ex(o, key, &tmp))
if (json_object_is_type(tmp, json_type_double))
return json_object_get_double(tmp);
return defval;
}
int json_get_bool(json_object *o, const char *key, int defval)
{
json_object *tmp;
if (json_object_object_get_ex(o, key, &tmp))
if (json_object_is_type(tmp, json_type_boolean))
return json_object_get_boolean(tmp);
return defval;
}
array_list* json_get_array(json_object *o, const char *key)
{
json_object *tmp;
if (json_object_object_get_ex(o, key, &tmp))
if (json_object_is_type(tmp, json_type_array))
return json_object_get_array(tmp);
return NULL;
}
void json_dump_type(int type)
{
switch (type) {
case json_type_null: printf("json_type_null\n");
break;
case json_type_boolean: printf("json_type_boolean\n");
break;
case json_type_double: printf("json_type_double\n");
break;
case json_type_int: printf("json_type_int\n");
break;
case json_type_object: printf("json_type_object\n");
break;
case json_type_array: printf("json_type_array\n");
break;
case json_type_string: printf("json_type_string\n");
break;
}
}
void json_dump(json_object *o)
{
int type=json_object_get_type(o);
printf("\nJSON Object: [%d], ", type);
json_dump_type(type);
switch (type) {
case json_type_string:
printf("String: %s\n", json_object_get_string(o));
break;
case json_type_int:
printf("Int: %d\n", json_object_get_int(o));
break;
case json_type_double:
printf("Double: %f\n", json_object_get_double(o));
break;
case json_type_boolean:
printf("Bool: %d\n", json_object_get_boolean(o));
break;
case json_type_object:
{
printf("Object: fields: %d\n", json_object_object_length(o));
json_object_object_foreach(o, key, val) {
type = json_object_get_type(val);
printf("key: \"%s\", contents:\n", key);
json_dump(val);
}
printf("--\n");
}
break;
case json_type_array:
{
int x,l=json_object_array_length(o);
printf("Array: items: %d\n", l);
for (x=0;x<l;x++ ) {
json_object *a=json_object_array_get_idx(o, x);
json_dump(a);
}
printf("--\n");
}
break;
default:;
}
}