| 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 | { |
| Eric Haszlakiewicz | 44f0f62 | 2012-02-15 19:37:04 -0600 | [diff] [blame] | 11 | json_object * const *jso1, * const *jso2; |
| Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 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_object *my_string, *my_int, *my_object, *my_array; |
| Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 35 | int i; |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 36 | |
| Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 37 | MC_SET_DEBUG(1); |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 38 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 39 | my_string = json_object_new_string("\t"); |
| 40 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 41 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 42 | json_object_put(my_string); |
| 43 | |
| Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 44 | my_string = json_object_new_string("\\"); |
| 45 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 46 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 47 | json_object_put(my_string); |
| 48 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 49 | my_string = json_object_new_string("foo"); |
| 50 | printf("my_string=%s\n", json_object_get_string(my_string)); |
| 51 | printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); |
| 52 | |
| 53 | my_int = json_object_new_int(9); |
| 54 | printf("my_int=%d\n", json_object_get_int(my_int)); |
| 55 | printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); |
| 56 | |
| 57 | my_array = json_object_new_array(); |
| 58 | json_object_array_add(my_array, json_object_new_int(1)); |
| 59 | json_object_array_add(my_array, json_object_new_int(2)); |
| 60 | json_object_array_add(my_array, json_object_new_int(3)); |
| 61 | json_object_array_put_idx(my_array, 4, json_object_new_int(5)); |
| 62 | printf("my_array=\n"); |
| Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 63 | for(i=0; i < json_object_array_length(my_array); i++) { |
| Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 64 | json_object *obj = json_object_array_get_idx(my_array, i); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 65 | printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); |
| 66 | } |
| 67 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 68 | |
| Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 69 | json_object_put(my_array); |
| 70 | |
| 71 | my_array = json_object_new_array(); |
| 72 | json_object_array_add(my_array, json_object_new_int(3)); |
| 73 | json_object_array_add(my_array, json_object_new_int(1)); |
| 74 | json_object_array_add(my_array, json_object_new_int(2)); |
| 75 | json_object_array_put_idx(my_array, 4, json_object_new_int(0)); |
| 76 | printf("my_array=\n"); |
| 77 | for(i=0; i < json_object_array_length(my_array); i++) { |
| 78 | json_object *obj = json_object_array_get_idx(my_array, i); |
| 79 | printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); |
| 80 | } |
| 81 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 82 | json_object_array_sort(my_array, sort_fn); |
| 83 | printf("my_array=\n"); |
| 84 | for(i=0; i < json_object_array_length(my_array); i++) { |
| 85 | json_object *obj = json_object_array_get_idx(my_array, i); |
| 86 | printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); |
| 87 | } |
| 88 | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); |
| 89 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 90 | my_object = json_object_new_object(); |
| 91 | json_object_object_add(my_object, "abc", json_object_new_int(12)); |
| 92 | json_object_object_add(my_object, "foo", json_object_new_string("bar")); |
| 93 | json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); |
| 94 | json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); |
| 95 | json_object_object_add(my_object, "baz", json_object_new_string("bang")); |
| 96 | json_object_object_add(my_object, "baz", json_object_new_string("fark")); |
| 97 | json_object_object_del(my_object, "baz"); |
| Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 98 | /*json_object_object_add(my_object, "arr", my_array);*/ |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 99 | printf("my_object=\n"); |
| 100 | json_object_object_foreach(my_object, key, val) { |
| 101 | printf("\t%s: %s\n", key, json_object_to_json_string(val)); |
| 102 | } |
| 103 | printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object)); |
| 104 | |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 105 | json_object_put(my_string); |
| 106 | json_object_put(my_int); |
| 107 | json_object_put(my_object); |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 108 | json_object_put(my_array); |
| Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |