| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 3 | #include <stddef.h> |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 4 | #include <string.h> |
| Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 5 | #include <assert.h> |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 6 | |
| 7 | #include "json.h" |
| 8 | |
| Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 9 | static int sort_fn (const void *j1, const void *j2) |
| 10 | { |
| 11 | json_object **jso1, **jso2; |
| 12 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 32 | int main(int argc, char **argv) |
| 33 | { |
| Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 34 | json_tokener *tok; |
| 35 | json_object *my_string, *my_int, *my_object, *my_array; |
| 36 | json_object *new_obj; |
| Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 37 | int i; |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 38 | |
| Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 39 | MC_SET_DEBUG(1); |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 40 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 41 | 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 Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 46 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 51 | 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 Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 65 | for(i=0; i < json_object_array_length(my_array); i++) { |
| Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 66 | json_object *obj = json_object_array_get_idx(my_array, i); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 67 | 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 Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 71 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 92 | 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 Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 100 | /*json_object_object_add(my_object, "arr", my_array);*/ |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 101 | 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 Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 155 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 159 | 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 Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 183 | 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 Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 188 | new_obj = json_tokener_parse("{ foo }"); |
| Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 189 | assert (new_obj == NULL); |
| 190 | |
| 191 | // if(is_error(new_obj)) printf("got error as expected\n"); |
| Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 192 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 193 | new_obj = json_tokener_parse("foo"); |
| Jehiah Czebotar | a503ee8 | 2010-12-08 03:52:07 +0000 | [diff] [blame] | 194 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 198 | |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 199 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 213 | json_object_put(my_string); |
| 214 | json_object_put(my_int); |
| 215 | json_object_put(my_object); |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 216 | json_object_put(my_array); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |