Skip to content
Newer
Older
100644 135 lines (105 sloc) 5.14 KB
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored Mar 13, 2007
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "json.h"
6
7
8 int main(int argc, char **argv)
9 {
10 struct json_object *my_string, *my_int, *my_object, *my_array;
11 struct json_object *new_obj;
12
13 my_string = json_object_new_string("\t");
14 printf("my_string=%s\n", json_object_get_string(my_string));
15 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
16 json_object_put(my_string);
17
18 my_string = json_object_new_string("foo");
19 printf("my_string=%s\n", json_object_get_string(my_string));
20 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
21
22 my_int = json_object_new_int(9);
23 printf("my_int=%d\n", json_object_get_int(my_int));
24 printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
25
26 my_array = json_object_new_array();
27 json_object_array_add(my_array, json_object_new_int(1));
28 json_object_array_add(my_array, json_object_new_int(2));
29 json_object_array_add(my_array, json_object_new_int(3));
30 json_object_array_put_idx(my_array, 4, json_object_new_int(5));
31 printf("my_array=\n");
32 for(int i=0; i < json_object_array_length(my_array); i++) {
33 struct json_object *obj = json_object_array_get_idx(my_array, i);
34 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
35 }
36 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
37
38 my_object = json_object_new_object();
39 json_object_object_add(my_object, "abc", json_object_new_int(12));
40 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
41 json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
42 json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
43 json_object_object_add(my_object, "baz", json_object_new_string("bang"));
44 json_object_object_add(my_object, "baz", json_object_new_string("fark"));
45 json_object_object_del(my_object, "baz");
46 json_object_object_add(my_object, "arr", my_array);
47 printf("my_object=\n");
48 json_object_object_foreach(my_object, key, val) {
49 printf("\t%s: %s\n", key, json_object_to_json_string(val));
50 }
51 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
52
53 new_obj = json_tokener_parse("\"\003\"");
54 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
55 json_object_put(new_obj);
56
57 new_obj = json_tokener_parse("/* hello */\"foo\"");
58 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
59 json_object_put(new_obj);
60
61 new_obj = json_tokener_parse("// hello\n\"foo\"");
62 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
63 json_object_put(new_obj);
64
65 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
66 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
67 json_object_put(new_obj);
68
69 new_obj = json_tokener_parse("null");
70 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
71 json_object_put(new_obj);
72
73 new_obj = json_tokener_parse("True");
74 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
75 json_object_put(new_obj);
76
77 new_obj = json_tokener_parse("12");
78 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
79 json_object_put(new_obj);
80
81 new_obj = json_tokener_parse("12.3");
82 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
83 json_object_put(new_obj);
84
85 new_obj = json_tokener_parse("[\"\\n\"]");
86 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
87 json_object_put(new_obj);
88
89 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
90 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
91 json_object_put(new_obj);
92
93 new_obj = json_tokener_parse("[null]");
94 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
95 json_object_put(new_obj);
96
97 new_obj = json_tokener_parse("[]");
98 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
99 json_object_put(new_obj);
100
101 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
102 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
103 json_object_put(new_obj);
104
105 new_obj = json_tokener_parse("{}");
106 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
107 json_object_put(new_obj);
108
109 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
110 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
111 json_object_put(new_obj);
112
113 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
114 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
115 json_object_put(new_obj);
116
117 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
118 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
119 json_object_put(new_obj);
120
121 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
122 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
123 json_object_put(new_obj);
124
125 new_obj = json_tokener_parse("foo");
126 if(is_error(new_obj)) printf("got error as expected\n");
127
128 json_object_put(my_string);
129 json_object_put(my_int);
130 json_object_put(my_object);
131 //json_object_put(my_array);
132
133 return 0;
134 }
Something went wrong with that request. Please try again.