Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 164 lines (130 sloc) 6.36 kb
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
1 #include <stdio.h>
2 #include <stdlib.h>
c8f4a6e7 » michaeljclark
2007-12-07 * Fix bug with use of capital E in numbers with exponents
3 #include <stddef.h>
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
4 #include <string.h>
5
6 #include "json.h"
7
8 int main(int argc, char **argv)
9 {
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
10 json_tokener *tok;
11 json_object *my_string, *my_int, *my_object, *my_array;
12 json_object *new_obj;
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
13 int i;
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
14
dfaf6704 » michaeljclark
2007-10-25 * Add macros to enable compiling out debug code
15 MC_SET_DEBUG(1);
a850f8e2 » michaeljclark
2007-03-13 * Add escaping of backslash to json output
16
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
17 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
a850f8e2 » michaeljclark
2007-03-13 * Add escaping of backslash to json output
22 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
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
27 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");
f6a6e486 » michaeljclark
2007-03-13 * Make headers C++ compatible by change *this to *obj
41 for(i=0; i < json_object_array_length(my_array); i++) {
aaec1ef3 » michaeljclark
2009-02-25 * Don't use this as a variable, so we can compile with a C++ compiler
42 json_object *obj = json_object_array_get_idx(my_array, i);
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
43 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");
dfaf6704 » michaeljclark
2007-10-25 * Add macros to enable compiling out debug code
55 /*json_object_object_add(my_object, "arr", my_array);*/
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
56 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
a850f8e2 » michaeljclark
2007-03-13 * Add escaping of backslash to json output
110 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
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
114 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
0370baa7 » michaeljclark
2007-03-13 * Fix additional error case in object parsing
138 new_obj = json_tokener_parse("{ foo }");
139 if(is_error(new_obj)) printf("got error as expected\n");
140
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
141 new_obj = json_tokener_parse("foo");
142 if(is_error(new_obj)) printf("got error as expected\n");
143
a850f8e2 » michaeljclark
2007-03-13 * Add escaping of backslash to json output
144 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
f0d08887 » michaeljclark
2007-03-13 import of version 0.1
158 json_object_put(my_string);
159 json_object_put(my_int);
160 json_object_put(my_object);
161 //json_object_put(my_array);
162
163 return 0;
164 }
Something went wrong with that request. Please try again.