blob: 1ca4db93363017679a8d4251a9da8149b0353a9f [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001#include <stdio.h>
2#include <stdlib.h>
Michael Clarkc8f4a6e2007-12-07 02:44:24 +00003#include <stddef.h>
Michael Clarkf0d08882007-03-13 08:26:18 +00004#include <string.h>
Jehiah Czebotara503ee82010-12-08 03:52:07 +00005#include <assert.h>
Michael Clarkf0d08882007-03-13 08:26:18 +00006
7#include "json.h"
8
Frederik Deweerdtc43871c2011-10-07 21:07:18 +02009static int sort_fn (const void *j1, const void *j2)
10{
Eric Haszlakiewicz44f0f622012-02-15 19:37:04 -060011 json_object * const *jso1, * const *jso2;
Frederik Deweerdtc43871c2011-10-07 21:07:18 +020012 int i1, i2;
13
14 jso1 = j1;
15 jso2 = j2;
16 if (!*jso1 && !*jso2) {
17 return 0;
18 }
19 if (!*jso1) {
20 return -1;
21 }
22 if (!*jso2) {
23 return 1;
24 }
25
26 i1 = json_object_get_int(*jso1);
27 i2 = json_object_get_int(*jso2);
28
29 return i1 - i2;
30}
31
Michael Clarkf0d08882007-03-13 08:26:18 +000032int main(int argc, char **argv)
33{
Michael Clarkaaec1ef2009-02-25 02:31:32 +000034 json_tokener *tok;
35 json_object *my_string, *my_int, *my_object, *my_array;
36 json_object *new_obj;
Michael Clarkf6a6e482007-03-13 08:26:23 +000037 int i;
Michael Clarkf0d08882007-03-13 08:26:18 +000038
Michael Clarkdfaf6702007-10-25 02:26:00 +000039 MC_SET_DEBUG(1);
Michael Clarka850f8e2007-03-13 08:26:26 +000040
Michael Clarkf0d08882007-03-13 08:26:18 +000041 my_string = json_object_new_string("\t");
42 printf("my_string=%s\n", json_object_get_string(my_string));
43 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
44 json_object_put(my_string);
45
Michael Clarka850f8e2007-03-13 08:26:26 +000046 my_string = json_object_new_string("\\");
47 printf("my_string=%s\n", json_object_get_string(my_string));
48 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
49 json_object_put(my_string);
50
Michael Clarkf0d08882007-03-13 08:26:18 +000051 my_string = json_object_new_string("foo");
52 printf("my_string=%s\n", json_object_get_string(my_string));
53 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
54
55 my_int = json_object_new_int(9);
56 printf("my_int=%d\n", json_object_get_int(my_int));
57 printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
58
59 my_array = json_object_new_array();
60 json_object_array_add(my_array, json_object_new_int(1));
61 json_object_array_add(my_array, json_object_new_int(2));
62 json_object_array_add(my_array, json_object_new_int(3));
63 json_object_array_put_idx(my_array, 4, json_object_new_int(5));
64 printf("my_array=\n");
Michael Clarkf6a6e482007-03-13 08:26:23 +000065 for(i=0; i < json_object_array_length(my_array); i++) {
Michael Clarkaaec1ef2009-02-25 02:31:32 +000066 json_object *obj = json_object_array_get_idx(my_array, i);
Michael Clarkf0d08882007-03-13 08:26:18 +000067 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
68 }
69 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
70
Frederik Deweerdtc43871c2011-10-07 21:07:18 +020071 json_object_put(my_array);
72
73 my_array = json_object_new_array();
74 json_object_array_add(my_array, json_object_new_int(3));
75 json_object_array_add(my_array, json_object_new_int(1));
76 json_object_array_add(my_array, json_object_new_int(2));
77 json_object_array_put_idx(my_array, 4, json_object_new_int(0));
78 printf("my_array=\n");
79 for(i=0; i < json_object_array_length(my_array); i++) {
80 json_object *obj = json_object_array_get_idx(my_array, i);
81 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
82 }
83 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
84 json_object_array_sort(my_array, sort_fn);
85 printf("my_array=\n");
86 for(i=0; i < json_object_array_length(my_array); i++) {
87 json_object *obj = json_object_array_get_idx(my_array, i);
88 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
89 }
90 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
91
Michael Clarkf0d08882007-03-13 08:26:18 +000092 my_object = json_object_new_object();
93 json_object_object_add(my_object, "abc", json_object_new_int(12));
94 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
95 json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
96 json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
97 json_object_object_add(my_object, "baz", json_object_new_string("bang"));
98 json_object_object_add(my_object, "baz", json_object_new_string("fark"));
99 json_object_object_del(my_object, "baz");
Michael Clarkdfaf6702007-10-25 02:26:00 +0000100 /*json_object_object_add(my_object, "arr", my_array);*/
Michael Clarkf0d08882007-03-13 08:26:18 +0000101 printf("my_object=\n");
102 json_object_object_foreach(my_object, key, val) {
103 printf("\t%s: %s\n", key, json_object_to_json_string(val));
104 }
105 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
106
107 new_obj = json_tokener_parse("\"\003\"");
108 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
109 json_object_put(new_obj);
110
111 new_obj = json_tokener_parse("/* hello */\"foo\"");
112 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
113 json_object_put(new_obj);
114
115 new_obj = json_tokener_parse("// hello\n\"foo\"");
116 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
117 json_object_put(new_obj);
118
119 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
120 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
121 json_object_put(new_obj);
122
123 new_obj = json_tokener_parse("null");
124 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
125 json_object_put(new_obj);
126
127 new_obj = json_tokener_parse("True");
128 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
129 json_object_put(new_obj);
130
131 new_obj = json_tokener_parse("12");
132 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
133 json_object_put(new_obj);
134
135 new_obj = json_tokener_parse("12.3");
136 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
137 json_object_put(new_obj);
138
139 new_obj = json_tokener_parse("[\"\\n\"]");
140 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
141 json_object_put(new_obj);
142
143 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
144 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
145 json_object_put(new_obj);
146
147 new_obj = json_tokener_parse("[null]");
148 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
149 json_object_put(new_obj);
150
151 new_obj = json_tokener_parse("[]");
152 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
153 json_object_put(new_obj);
154
Michael Clarka850f8e2007-03-13 08:26:26 +0000155 new_obj = json_tokener_parse("[false]");
156 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
157 json_object_put(new_obj);
158
Michael Clarkf0d08882007-03-13 08:26:18 +0000159 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
160 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
161 json_object_put(new_obj);
162
163 new_obj = json_tokener_parse("{}");
164 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
165 json_object_put(new_obj);
166
167 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
168 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
169 json_object_put(new_obj);
170
171 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
172 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
173 json_object_put(new_obj);
174
175 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
176 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
177 json_object_put(new_obj);
178
179 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
180 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
181 json_object_put(new_obj);
182
Jehiah Czebotara503ee82010-12-08 03:52:07 +0000183 enum json_tokener_error error = json_tokener_success;
184 new_obj = json_tokener_parse_verbose("{ foo }", &error);
185 assert (error == json_tokener_error_parse_object_key_name);
186 assert (new_obj == NULL);
187
Michael Clark0370baa2007-03-13 08:26:22 +0000188 new_obj = json_tokener_parse("{ foo }");
Jehiah Czebotara503ee82010-12-08 03:52:07 +0000189 assert (new_obj == NULL);
190
191 // if(is_error(new_obj)) printf("got error as expected\n");
Michael Clark0370baa2007-03-13 08:26:22 +0000192
Michael Clarkf0d08882007-03-13 08:26:18 +0000193 new_obj = json_tokener_parse("foo");
Jehiah Czebotara503ee82010-12-08 03:52:07 +0000194 assert (new_obj == NULL);
195 new_obj = json_tokener_parse_verbose("foo", &error);
196 assert (new_obj == NULL);
197 assert (error == json_tokener_error_parse_boolean);
Michael Clarkf0d08882007-03-13 08:26:18 +0000198
Michael Clarka850f8e2007-03-13 08:26:26 +0000199 new_obj = json_tokener_parse("{ \"foo");
200 if(is_error(new_obj)) printf("got error as expected\n");
201
202 /* test incremental parsing */
203 tok = json_tokener_new();
204 new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
205 if(is_error(new_obj)) printf("got error as expected\n");
206 new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
207 if(is_error(new_obj)) printf("got error as expected\n");
208 new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
209 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
210 json_object_put(new_obj);
211 json_tokener_free(tok);
212
Michael Clarkf0d08882007-03-13 08:26:18 +0000213 json_object_put(my_string);
214 json_object_put(my_int);
215 json_object_put(my_object);
Michael Clarkc4dceae2010-10-06 16:39:20 +0000216 json_object_put(my_array);
Michael Clarkf0d08882007-03-13 08:26:18 +0000217
218 return 0;
219}