blob: a3cc6d9753ef4b74ab33994d83ae1340058ea1d1 [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>
5
6#include "json.h"
7
Michael Clarkf0d08882007-03-13 08:26:18 +00008int main(int argc, char **argv)
9{
Michael Clarkaaec1ef2009-02-25 02:31:32 +000010 json_tokener *tok;
11 json_object *my_string, *my_int, *my_object, *my_array;
12 json_object *new_obj;
Michael Clarkf6a6e482007-03-13 08:26:23 +000013 int i;
Michael Clarkf0d08882007-03-13 08:26:18 +000014
Michael Clarkdfaf6702007-10-25 02:26:00 +000015 MC_SET_DEBUG(1);
Michael Clarka850f8e2007-03-13 08:26:26 +000016
Michael Clarkf0d08882007-03-13 08:26:18 +000017 my_string = json_object_new_string("\t");
18 printf("my_string=%s\n", json_object_get_string(my_string));
19 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
20 json_object_put(my_string);
21
Michael Clarka850f8e2007-03-13 08:26:26 +000022 my_string = json_object_new_string("\\");
23 printf("my_string=%s\n", json_object_get_string(my_string));
24 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
25 json_object_put(my_string);
26
Michael Clarkf0d08882007-03-13 08:26:18 +000027 my_string = json_object_new_string("foo");
28 printf("my_string=%s\n", json_object_get_string(my_string));
29 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
30
31 my_int = json_object_new_int(9);
32 printf("my_int=%d\n", json_object_get_int(my_int));
33 printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
34
35 my_array = json_object_new_array();
36 json_object_array_add(my_array, json_object_new_int(1));
37 json_object_array_add(my_array, json_object_new_int(2));
38 json_object_array_add(my_array, json_object_new_int(3));
39 json_object_array_put_idx(my_array, 4, json_object_new_int(5));
40 printf("my_array=\n");
Michael Clarkf6a6e482007-03-13 08:26:23 +000041 for(i=0; i < json_object_array_length(my_array); i++) {
Michael Clarkaaec1ef2009-02-25 02:31:32 +000042 json_object *obj = json_object_array_get_idx(my_array, i);
Michael Clarkf0d08882007-03-13 08:26:18 +000043 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
44 }
45 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
46
47 my_object = json_object_new_object();
48 json_object_object_add(my_object, "abc", json_object_new_int(12));
49 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
50 json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
51 json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
52 json_object_object_add(my_object, "baz", json_object_new_string("bang"));
53 json_object_object_add(my_object, "baz", json_object_new_string("fark"));
54 json_object_object_del(my_object, "baz");
Michael Clarkdfaf6702007-10-25 02:26:00 +000055 /*json_object_object_add(my_object, "arr", my_array);*/
Michael Clarkf0d08882007-03-13 08:26:18 +000056 printf("my_object=\n");
57 json_object_object_foreach(my_object, key, val) {
58 printf("\t%s: %s\n", key, json_object_to_json_string(val));
59 }
60 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
61
62 new_obj = json_tokener_parse("\"\003\"");
63 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
64 json_object_put(new_obj);
65
66 new_obj = json_tokener_parse("/* hello */\"foo\"");
67 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
68 json_object_put(new_obj);
69
70 new_obj = json_tokener_parse("// hello\n\"foo\"");
71 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
72 json_object_put(new_obj);
73
74 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
75 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
76 json_object_put(new_obj);
77
78 new_obj = json_tokener_parse("null");
79 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
80 json_object_put(new_obj);
81
82 new_obj = json_tokener_parse("True");
83 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
84 json_object_put(new_obj);
85
86 new_obj = json_tokener_parse("12");
87 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
88 json_object_put(new_obj);
89
90 new_obj = json_tokener_parse("12.3");
91 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
92 json_object_put(new_obj);
93
94 new_obj = json_tokener_parse("[\"\\n\"]");
95 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
96 json_object_put(new_obj);
97
98 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
99 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
100 json_object_put(new_obj);
101
102 new_obj = json_tokener_parse("[null]");
103 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
104 json_object_put(new_obj);
105
106 new_obj = json_tokener_parse("[]");
107 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
108 json_object_put(new_obj);
109
Michael Clarka850f8e2007-03-13 08:26:26 +0000110 new_obj = json_tokener_parse("[false]");
111 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
112 json_object_put(new_obj);
113
Michael Clarkf0d08882007-03-13 08:26:18 +0000114 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
115 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
116 json_object_put(new_obj);
117
118 new_obj = json_tokener_parse("{}");
119 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
120 json_object_put(new_obj);
121
122 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
123 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
124 json_object_put(new_obj);
125
126 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
127 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
128 json_object_put(new_obj);
129
130 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
131 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
132 json_object_put(new_obj);
133
134 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
135 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
136 json_object_put(new_obj);
137
Michael Clark0370baa2007-03-13 08:26:22 +0000138 new_obj = json_tokener_parse("{ foo }");
139 if(is_error(new_obj)) printf("got error as expected\n");
140
Michael Clarkf0d08882007-03-13 08:26:18 +0000141 new_obj = json_tokener_parse("foo");
142 if(is_error(new_obj)) printf("got error as expected\n");
143
Michael Clarka850f8e2007-03-13 08:26:26 +0000144 new_obj = json_tokener_parse("{ \"foo");
145 if(is_error(new_obj)) printf("got error as expected\n");
146
147 /* test incremental parsing */
148 tok = json_tokener_new();
149 new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
150 if(is_error(new_obj)) printf("got error as expected\n");
151 new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
152 if(is_error(new_obj)) printf("got error as expected\n");
153 new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
154 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
155 json_object_put(new_obj);
156 json_tokener_free(tok);
157
Michael Clarkf0d08882007-03-13 08:26:18 +0000158 json_object_put(my_string);
159 json_object_put(my_int);
160 json_object_put(my_object);
Michael Clarkc4dceae2010-10-06 16:39:20 +0000161 json_object_put(my_array);
Michael Clarkf0d08882007-03-13 08:26:18 +0000162
163 return 0;
164}