| 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> |
| 5 | |
| 6 | #include "json.h" |
| 7 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 8 | int main(int argc, char **argv) |
| 9 | { |
| Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 10 | json_tokener *tok; |
| 11 | json_object *my_string, *my_int, *my_object, *my_array; |
| 12 | json_object *new_obj; |
| Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 13 | int i; |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | |
| Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 15 | MC_SET_DEBUG(1); |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 16 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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 | |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 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 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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"); |
| Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 41 | for(i=0; i < json_object_array_length(my_array); i++) { |
| Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 42 | json_object *obj = json_object_array_get_idx(my_array, i); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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"); |
| Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 55 | /*json_object_object_add(my_object, "arr", my_array);*/ |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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 | |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 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 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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 | |
| Michael Clark | 0370baa | 2007-03-13 08:26:22 +0000 | [diff] [blame] | 138 | new_obj = json_tokener_parse("{ foo }"); |
| 139 | if(is_error(new_obj)) printf("got error as expected\n"); |
| 140 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 141 | new_obj = json_tokener_parse("foo"); |
| 142 | if(is_error(new_obj)) printf("got error as expected\n"); |
| 143 | |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 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 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 158 | json_object_put(my_string); |
| 159 | json_object_put(my_int); |
| 160 | json_object_put(my_object); |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 161 | json_object_put(my_array); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |