Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 2 | * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 6 | * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 7 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 8 | * This library is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the MIT license. See COPYING for details. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 13 | #include "config.h" |
| 14 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 17 | #include <stddef.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
| 20 | #include "debug.h" |
| 21 | #include "printbuf.h" |
| 22 | #include "linkhash.h" |
| 23 | #include "arraylist.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 24 | #include "json_inttypes.h" |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 25 | #include "json_object.h" |
| 26 | #include "json_object_private.h" |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 27 | #include "json_util.h" |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 28 | |
Mateusz Loskot | a6f39a3 | 2012-05-21 23:22:36 +0100 | [diff] [blame] | 29 | #if !defined(HAVE_STRDUP) && defined(_MSC_VER) |
| 30 | /* MSC has the version as _strdup */ |
| 31 | # define strdup _strdup |
| 32 | #elif !defined(HAVE_STRDUP) |
| 33 | # error You do not have strdup on your system. |
| 34 | #endif /* HAVE_STRDUP */ |
| 35 | |
| 36 | #if !defined(HAVE_STRNDUP) |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 37 | char* strndup(const char* str, size_t n); |
| 38 | #endif /* !HAVE_STRNDUP */ |
| 39 | |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 40 | // Don't define this. It's not thread-safe. |
Christopher Watford | c5cbf82 | 2009-06-30 03:40:53 +0000 | [diff] [blame] | 41 | /* #define REFCOUNT_DEBUG 1 */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 42 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 43 | const char *json_number_chars = "0123456789.+-eE"; |
Federico Culloca | 7ec34c9 | 2011-10-30 12:13:15 +0100 | [diff] [blame] | 44 | const char *json_hex_chars = "0123456789abcdefABCDEF"; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 45 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 46 | static void json_object_generic_delete(struct json_object* jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 47 | static struct json_object* json_object_new(enum json_type o_type); |
| 48 | |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 49 | static json_object_to_json_string_fn json_object_object_to_json_string; |
| 50 | static json_object_to_json_string_fn json_object_boolean_to_json_string; |
| 51 | static json_object_to_json_string_fn json_object_int_to_json_string; |
| 52 | static json_object_to_json_string_fn json_object_double_to_json_string; |
| 53 | static json_object_to_json_string_fn json_object_string_to_json_string; |
| 54 | static json_object_to_json_string_fn json_object_array_to_json_string; |
| 55 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 56 | |
| 57 | /* ref count debugging */ |
| 58 | |
| 59 | #ifdef REFCOUNT_DEBUG |
| 60 | |
| 61 | static struct lh_table *json_object_table; |
| 62 | |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 63 | static void json_object_init(void) __attribute__ ((constructor)); |
| 64 | static void json_object_init(void) { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 65 | MC_DEBUG("json_object_init: creating object table\n"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 66 | json_object_table = lh_kptr_table_new(128, "json_object_table", NULL); |
| 67 | } |
| 68 | |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 69 | static void json_object_fini(void) __attribute__ ((destructor)); |
| 70 | static void json_object_fini(void) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 71 | struct lh_entry *ent; |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 72 | if(MC_GET_DEBUG()) { |
| 73 | if (json_object_table->count) { |
| 74 | MC_DEBUG("json_object_fini: %d referenced objects at exit\n", |
| 75 | json_object_table->count); |
| 76 | lh_foreach(json_object_table, ent) { |
| 77 | struct json_object* obj = (struct json_object*)ent->v; |
Eric Haszlakiewicz | 886c4fb | 2011-05-03 20:40:49 +0000 | [diff] [blame] | 78 | MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj); |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 79 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 82 | MC_DEBUG("json_object_fini: freeing object table\n"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 83 | lh_table_free(json_object_table); |
| 84 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 85 | #endif /* REFCOUNT_DEBUG */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | /* string escaping */ |
| 89 | |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 90 | static int json_escape_str(struct printbuf *pb, char *str, int len) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 91 | { |
| 92 | int pos = 0, start_offset = 0; |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 93 | unsigned char c; |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 94 | while (len--) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 95 | c = str[pos]; |
| 96 | switch(c) { |
| 97 | case '\b': |
| 98 | case '\n': |
| 99 | case '\r': |
| 100 | case '\t': |
Abioy | 7eaa849 | 2012-12-24 22:22:05 +0800 | [diff] [blame] | 101 | case '\f': |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 102 | case '"': |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 103 | case '\\': |
| 104 | case '/': |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 105 | if(pos - start_offset > 0) |
| 106 | printbuf_memappend(pb, str + start_offset, pos - start_offset); |
| 107 | if(c == '\b') printbuf_memappend(pb, "\\b", 2); |
| 108 | else if(c == '\n') printbuf_memappend(pb, "\\n", 2); |
| 109 | else if(c == '\r') printbuf_memappend(pb, "\\r", 2); |
| 110 | else if(c == '\t') printbuf_memappend(pb, "\\t", 2); |
Abioy | 7eaa849 | 2012-12-24 22:22:05 +0800 | [diff] [blame] | 111 | else if(c == '\f') printbuf_memappend(pb, "\\f", 2); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 112 | else if(c == '"') printbuf_memappend(pb, "\\\"", 2); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 113 | else if(c == '\\') printbuf_memappend(pb, "\\\\", 2); |
| 114 | else if(c == '/') printbuf_memappend(pb, "\\/", 2); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 115 | start_offset = ++pos; |
| 116 | break; |
| 117 | default: |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 118 | if(c < ' ') { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 119 | if(pos - start_offset > 0) |
| 120 | printbuf_memappend(pb, str + start_offset, pos - start_offset); |
| 121 | sprintbuf(pb, "\\u00%c%c", |
| 122 | json_hex_chars[c >> 4], |
| 123 | json_hex_chars[c & 0xf]); |
| 124 | start_offset = ++pos; |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 125 | } else pos++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 126 | } |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 127 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 128 | if(pos - start_offset > 0) |
| 129 | printbuf_memappend(pb, str + start_offset, pos - start_offset); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* reference counting */ |
| 135 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 136 | extern struct json_object* json_object_get(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 137 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 138 | if(jso) { |
| 139 | jso->_ref_count++; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 140 | } |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 141 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 144 | int json_object_put(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 145 | { |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 146 | if(jso) |
| 147 | { |
| 148 | jso->_ref_count--; |
| 149 | if(!jso->_ref_count) |
| 150 | { |
| 151 | if (jso->_user_delete) |
| 152 | jso->_user_delete(jso, jso->_userdata); |
| 153 | jso->_delete(jso); |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 154 | return 1; |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 157 | return 0; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | /* generic object construction and destruction parts */ |
| 162 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 163 | static void json_object_generic_delete(struct json_object* jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 164 | { |
| 165 | #ifdef REFCOUNT_DEBUG |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 166 | MC_DEBUG("json_object_delete_%s: %p\n", |
Eric Haszlakiewicz | 886c4fb | 2011-05-03 20:40:49 +0000 | [diff] [blame] | 167 | json_type_to_name(jso->o_type), jso); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 168 | lh_table_delete(json_object_table, jso); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 169 | #endif /* REFCOUNT_DEBUG */ |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 170 | printbuf_free(jso->_pb); |
| 171 | free(jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static struct json_object* json_object_new(enum json_type o_type) |
| 175 | { |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 176 | struct json_object *jso; |
| 177 | |
| 178 | jso = (struct json_object*)calloc(sizeof(struct json_object), 1); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 179 | if(!jso) return NULL; |
| 180 | jso->o_type = o_type; |
| 181 | jso->_ref_count = 1; |
| 182 | jso->_delete = &json_object_generic_delete; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 183 | #ifdef REFCOUNT_DEBUG |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 184 | lh_table_insert(json_object_table, jso, jso); |
Eric Haszlakiewicz | 886c4fb | 2011-05-03 20:40:49 +0000 | [diff] [blame] | 185 | MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 186 | #endif /* REFCOUNT_DEBUG */ |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 187 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | /* type checking functions */ |
| 192 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 193 | int json_object_is_type(struct json_object *jso, enum json_type type) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 194 | { |
Eric Haszlakiewicz | aef439a | 2012-03-31 13:47:28 -0500 | [diff] [blame] | 195 | if (!jso) |
| 196 | return (type == json_type_null); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 197 | return (jso->o_type == type); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 200 | enum json_type json_object_get_type(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 201 | { |
Eric Haszlakiewicz | aef439a | 2012-03-31 13:47:28 -0500 | [diff] [blame] | 202 | if (!jso) |
| 203 | return json_type_null; |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 204 | return jso->o_type; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Eric Haszlakiewicz | 38f421a | 2012-09-02 15:21:56 -0500 | [diff] [blame] | 207 | /* set a custom conversion to string */ |
| 208 | |
| 209 | void json_object_set_serializer(json_object *jso, |
| 210 | json_object_to_json_string_fn to_string_func, |
| 211 | void *userdata, |
| 212 | json_object_delete_fn *user_delete) |
| 213 | { |
| 214 | // First, clean up any previously existing user info |
| 215 | if (jso->_user_delete) |
| 216 | { |
| 217 | jso->_user_delete(jso, jso->_userdata); |
| 218 | } |
| 219 | jso->_userdata = NULL; |
| 220 | jso->_user_delete = NULL; |
| 221 | |
| 222 | if (to_string_func == NULL) |
| 223 | { |
| 224 | // Reset to the standard serialization function |
| 225 | switch(jso->o_type) |
| 226 | { |
| 227 | case json_type_null: |
| 228 | jso->_to_json_string = NULL; |
| 229 | break; |
| 230 | case json_type_boolean: |
| 231 | jso->_to_json_string = &json_object_boolean_to_json_string; |
| 232 | break; |
| 233 | case json_type_double: |
| 234 | jso->_to_json_string = &json_object_double_to_json_string; |
| 235 | break; |
| 236 | case json_type_int: |
| 237 | jso->_to_json_string = &json_object_int_to_json_string; |
| 238 | break; |
| 239 | case json_type_object: |
| 240 | jso->_to_json_string = &json_object_object_to_json_string; |
| 241 | break; |
| 242 | case json_type_array: |
| 243 | jso->_to_json_string = &json_object_array_to_json_string; |
| 244 | break; |
| 245 | case json_type_string: |
| 246 | jso->_to_json_string = &json_object_string_to_json_string; |
| 247 | break; |
| 248 | } |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | jso->_to_json_string = to_string_func; |
| 253 | jso->_userdata = userdata; |
| 254 | jso->_user_delete = user_delete; |
| 255 | } |
| 256 | |
| 257 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 258 | /* extended conversion to string */ |
| 259 | |
| 260 | const char* json_object_to_json_string_ext(struct json_object *jso, int flags) |
| 261 | { |
| 262 | if (!jso) |
| 263 | return "null"; |
| 264 | |
| 265 | if ((!jso->_pb) && !(jso->_pb = printbuf_new())) |
| 266 | return NULL; |
| 267 | |
| 268 | printbuf_reset(jso->_pb); |
| 269 | |
| 270 | if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0) |
| 271 | return NULL; |
| 272 | |
| 273 | return jso->_pb->buf; |
| 274 | } |
| 275 | |
| 276 | /* backwards-compatible conversion to string */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 277 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 278 | const char* json_object_to_json_string(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 279 | { |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 280 | return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 283 | static void indent(struct printbuf *pb, int level, int flags) |
| 284 | { |
| 285 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 286 | { |
| 287 | printbuf_memset(pb, -1, ' ', level * 2); |
| 288 | } |
| 289 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 290 | |
| 291 | /* json_object_object */ |
| 292 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 293 | static int json_object_object_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 294 | struct printbuf *pb, |
| 295 | int level, |
| 296 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 297 | { |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 298 | int had_children = 0; |
| 299 | struct json_object_iter iter; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 300 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 301 | sprintbuf(pb, "{" /*}*/); |
| 302 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 303 | sprintbuf(pb, "\n"); |
| 304 | json_object_object_foreachC(jso, iter) |
| 305 | { |
| 306 | if (had_children) |
| 307 | { |
| 308 | sprintbuf(pb, ","); |
| 309 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 310 | sprintbuf(pb, "\n"); |
| 311 | } |
| 312 | had_children = 1; |
| 313 | if (flags & JSON_C_TO_STRING_SPACED) |
| 314 | sprintbuf(pb, " "); |
| 315 | indent(pb, level+1, flags); |
| 316 | sprintbuf(pb, "\""); |
| 317 | json_escape_str(pb, iter.key, strlen(iter.key)); |
| 318 | if (flags & JSON_C_TO_STRING_SPACED) |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 319 | sprintbuf(pb, "\": "); |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 320 | else |
| 321 | sprintbuf(pb, "\":"); |
| 322 | if(iter.val == NULL) |
| 323 | sprintbuf(pb, "null"); |
| 324 | else |
| 325 | iter.val->_to_json_string(iter.val, pb, level+1,flags); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 326 | } |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 327 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 328 | { |
| 329 | if (had_children) |
| 330 | sprintbuf(pb, "\n"); |
| 331 | indent(pb,level,flags); |
| 332 | } |
| 333 | if (flags & JSON_C_TO_STRING_SPACED) |
| 334 | return sprintbuf(pb, /*{*/ " }"); |
| 335 | else |
| 336 | return sprintbuf(pb, /*{*/ "}"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 339 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 340 | static void json_object_lh_entry_free(struct lh_entry *ent) |
| 341 | { |
| 342 | free(ent->k); |
| 343 | json_object_put((struct json_object*)ent->v); |
| 344 | } |
| 345 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 346 | static void json_object_object_delete(struct json_object* jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 347 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 348 | lh_table_free(jso->o.c_object); |
| 349 | json_object_generic_delete(jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Michael Clark | e8de078 | 2009-02-25 01:45:00 +0000 | [diff] [blame] | 352 | struct json_object* json_object_new_object(void) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 353 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 354 | struct json_object *jso = json_object_new(json_type_object); |
| 355 | if(!jso) return NULL; |
| 356 | jso->_delete = &json_object_object_delete; |
| 357 | jso->_to_json_string = &json_object_object_to_json_string; |
| 358 | jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 359 | NULL, &json_object_lh_entry_free); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 360 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 363 | struct lh_table* json_object_get_object(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 364 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 365 | if(!jso) return NULL; |
| 366 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 367 | case json_type_object: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 368 | return jso->o.c_object; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 369 | default: |
| 370 | return NULL; |
| 371 | } |
| 372 | } |
| 373 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 374 | void json_object_object_add(struct json_object* jso, const char *key, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 375 | struct json_object *val) |
| 376 | { |
Eric Haszlakiewicz | 6988f53 | 2012-07-24 23:27:41 -0500 | [diff] [blame] | 377 | // We lookup the entry and replace the value, rather than just deleting |
| 378 | // and re-adding it, so the existing key remains valid. |
| 379 | json_object *existing_value = NULL; |
| 380 | struct lh_entry *existing_entry; |
| 381 | existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key); |
| 382 | if (!existing_entry) |
| 383 | { |
| 384 | lh_table_insert(jso->o.c_object, strdup(key), val); |
| 385 | return; |
| 386 | } |
| 387 | existing_value = (void *)existing_entry->v; |
| 388 | if (existing_value) |
| 389 | json_object_put(existing_value); |
| 390 | existing_entry->v = val; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Greg Hazel | cca74c6 | 2013-01-11 01:36:55 -0800 | [diff] [blame^] | 393 | int json_object_object_length(struct json_object *jso) |
| 394 | { |
| 395 | return lh_table_length(jso->o.c_object); |
| 396 | } |
| 397 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 398 | struct json_object* json_object_object_get(struct json_object* jso, const char *key) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 399 | { |
Eric Haszlakiewicz | e36e562 | 2012-10-18 17:16:36 -0500 | [diff] [blame] | 400 | struct json_object *result = NULL; |
| 401 | json_object_object_get_ex(jso, key, &result); |
| 402 | return result; |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value) |
| 406 | { |
Eric Haszlakiewicz | 5450bed | 2012-10-18 17:14:41 -0500 | [diff] [blame] | 407 | if (value != NULL) |
| 408 | *value = NULL; |
| 409 | |
Eric Haszlakiewicz | e36e562 | 2012-10-18 17:16:36 -0500 | [diff] [blame] | 410 | if (NULL == jso) |
| 411 | return FALSE; |
Keith Derrick | 6917586 | 2012-04-12 11:44:13 -0700 | [diff] [blame] | 412 | |
Eric Haszlakiewicz | e36e562 | 2012-10-18 17:16:36 -0500 | [diff] [blame] | 413 | switch(jso->o_type) |
| 414 | { |
| 415 | case json_type_object: |
| 416 | return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value); |
| 417 | default: |
| 418 | if (value != NULL) |
| 419 | *value = NULL; |
| 420 | return FALSE; |
| 421 | } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 424 | void json_object_object_del(struct json_object* jso, const char *key) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 425 | { |
Eric Haszlakiewicz | 5f4739e | 2012-10-18 17:10:09 -0500 | [diff] [blame] | 426 | lh_table_delete(jso->o.c_object, key); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
| 430 | /* json_object_boolean */ |
| 431 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 432 | static int json_object_boolean_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 433 | struct printbuf *pb, |
| 434 | int level, |
| 435 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 436 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 437 | if(jso->o.c_boolean) return sprintbuf(pb, "true"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 438 | else return sprintbuf(pb, "false"); |
| 439 | } |
| 440 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 441 | struct json_object* json_object_new_boolean(json_bool b) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 442 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 443 | struct json_object *jso = json_object_new(json_type_boolean); |
| 444 | if(!jso) return NULL; |
| 445 | jso->_to_json_string = &json_object_boolean_to_json_string; |
| 446 | jso->o.c_boolean = b; |
| 447 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Keith Derrick | 37e7467 | 2012-03-26 14:29:31 -0700 | [diff] [blame] | 450 | json_bool json_object_get_boolean(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 451 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 452 | if(!jso) return FALSE; |
| 453 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 454 | case json_type_boolean: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 455 | return jso->o.c_boolean; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 456 | case json_type_int: |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 457 | return (jso->o.c_int64 != 0); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 458 | case json_type_double: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 459 | return (jso->o.c_double != 0); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 460 | case json_type_string: |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 461 | return (jso->o.c_string.len != 0); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 462 | default: |
Michael Clark | 6f70e44 | 2009-04-27 08:19:27 +0000 | [diff] [blame] | 463 | return FALSE; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* json_object_int */ |
| 469 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 470 | static int json_object_int_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 471 | struct printbuf *pb, |
| 472 | int level, |
| 473 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 474 | { |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 475 | return sprintbuf(pb, "%"PRId64, jso->o.c_int64); |
| 476 | } |
| 477 | |
| 478 | struct json_object* json_object_new_int(int32_t i) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 479 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 480 | struct json_object *jso = json_object_new(json_type_int); |
| 481 | if(!jso) return NULL; |
| 482 | jso->_to_json_string = &json_object_int_to_json_string; |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 483 | jso->o.c_int64 = i; |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 484 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 487 | int32_t json_object_get_int(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 488 | { |
John Arbash Meinel | 6a231e4 | 2012-02-01 09:27:49 +0100 | [diff] [blame] | 489 | int64_t cint64; |
| 490 | enum json_type o_type; |
| 491 | |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 492 | if(!jso) return 0; |
| 493 | |
John Arbash Meinel | 6a231e4 | 2012-02-01 09:27:49 +0100 | [diff] [blame] | 494 | o_type = jso->o_type; |
| 495 | cint64 = jso->o.c_int64; |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 496 | |
| 497 | if (o_type == json_type_string) |
| 498 | { |
| 499 | /* |
| 500 | * Parse strings into 64-bit numbers, then use the |
| 501 | * 64-to-32-bit number handling below. |
| 502 | */ |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 503 | if (json_parse_int64(jso->o.c_string.str, &cint64) != 0) |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 504 | return 0; /* whoops, it didn't work. */ |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 505 | o_type = json_type_int; |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Eric Haszlakiewicz | e2e1601 | 2011-05-03 20:39:07 +0000 | [diff] [blame] | 508 | switch(o_type) { |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 509 | case json_type_int: |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 510 | /* Make sure we return the correct values for out of range numbers. */ |
| 511 | if (cint64 <= INT32_MIN) |
| 512 | return INT32_MIN; |
| 513 | else if (cint64 >= INT32_MAX) |
| 514 | return INT32_MAX; |
| 515 | else |
| 516 | return (int32_t)cint64; |
| 517 | case json_type_double: |
| 518 | return (int32_t)jso->o.c_double; |
| 519 | case json_type_boolean: |
| 520 | return jso->o.c_boolean; |
| 521 | default: |
| 522 | return 0; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | struct json_object* json_object_new_int64(int64_t i) |
| 527 | { |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 528 | struct json_object *jso = json_object_new(json_type_int); |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 529 | if(!jso) return NULL; |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 530 | jso->_to_json_string = &json_object_int_to_json_string; |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 531 | jso->o.c_int64 = i; |
| 532 | return jso; |
| 533 | } |
| 534 | |
| 535 | int64_t json_object_get_int64(struct json_object *jso) |
| 536 | { |
| 537 | int64_t cint; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 538 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 539 | if(!jso) return 0; |
| 540 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 541 | case json_type_int: |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 542 | return jso->o.c_int64; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 543 | case json_type_double: |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 544 | return (int64_t)jso->o.c_double; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 545 | case json_type_boolean: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 546 | return jso->o.c_boolean; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 547 | case json_type_string: |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 548 | if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 549 | default: |
| 550 | return 0; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | |
| 555 | /* json_object_double */ |
| 556 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 557 | static int json_object_double_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 558 | struct printbuf *pb, |
| 559 | int level, |
| 560 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 561 | { |
Eric Haszlakiewicz | 0cc1db6 | 2012-04-29 11:04:33 -0500 | [diff] [blame] | 562 | return sprintbuf(pb, "%f", jso->o.c_double); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | struct json_object* json_object_new_double(double d) |
| 566 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 567 | struct json_object *jso = json_object_new(json_type_double); |
| 568 | if(!jso) return NULL; |
| 569 | jso->_to_json_string = &json_object_double_to_json_string; |
| 570 | jso->o.c_double = d; |
| 571 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 574 | double json_object_get_double(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 575 | { |
| 576 | double cdouble; |
| 577 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 578 | if(!jso) return 0.0; |
| 579 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 580 | case json_type_double: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 581 | return jso->o.c_double; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 582 | case json_type_int: |
Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 583 | return jso->o.c_int64; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 584 | case json_type_boolean: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 585 | return jso->o.c_boolean; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 586 | case json_type_string: |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 587 | if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 588 | default: |
| 589 | return 0.0; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /* json_object_string */ |
| 595 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 596 | static int json_object_string_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 597 | struct printbuf *pb, |
| 598 | int level, |
| 599 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 600 | { |
| 601 | sprintbuf(pb, "\""); |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 602 | json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 603 | sprintbuf(pb, "\""); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 607 | static void json_object_string_delete(struct json_object* jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 608 | { |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 609 | free(jso->o.c_string.str); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 610 | json_object_generic_delete(jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 613 | struct json_object* json_object_new_string(const char *s) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 614 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 615 | struct json_object *jso = json_object_new(json_type_string); |
| 616 | if(!jso) return NULL; |
| 617 | jso->_delete = &json_object_string_delete; |
| 618 | jso->_to_json_string = &json_object_string_to_json_string; |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 619 | jso->o.c_string.str = strdup(s); |
| 620 | jso->o.c_string.len = strlen(s); |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 621 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 624 | struct json_object* json_object_new_string_len(const char *s, int len) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 625 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 626 | struct json_object *jso = json_object_new(json_type_string); |
| 627 | if(!jso) return NULL; |
| 628 | jso->_delete = &json_object_string_delete; |
| 629 | jso->_to_json_string = &json_object_string_to_json_string; |
Eric Haszlakiewicz | 4e4af93 | 2012-12-09 16:32:11 -0600 | [diff] [blame] | 630 | jso->o.c_string.str = (char*)malloc(len + 1); |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 631 | memcpy(jso->o.c_string.str, (void *)s, len); |
Eric Haszlakiewicz | 4e4af93 | 2012-12-09 16:32:11 -0600 | [diff] [blame] | 632 | jso->o.c_string.str[len] = '\0'; |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 633 | jso->o.c_string.len = len; |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 634 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 637 | const char* json_object_get_string(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 638 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 639 | if(!jso) return NULL; |
| 640 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 641 | case json_type_string: |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 642 | return jso->o.c_string.str; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 643 | default: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 644 | return json_object_to_json_string(jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Jehiah Czebotar | ac601b5 | 2011-01-14 17:23:06 +0000 | [diff] [blame] | 648 | int json_object_get_string_len(struct json_object *jso) { |
| 649 | if(!jso) return 0; |
| 650 | switch(jso->o_type) { |
| 651 | case json_type_string: |
| 652 | return jso->o.c_string.len; |
| 653 | default: |
| 654 | return 0; |
| 655 | } |
| 656 | } |
| 657 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 658 | |
| 659 | /* json_object_array */ |
| 660 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 661 | static int json_object_array_to_json_string(struct json_object* jso, |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 662 | struct printbuf *pb, |
| 663 | int level, |
| 664 | int flags) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 665 | { |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 666 | int had_children = 0; |
| 667 | int ii; |
| 668 | sprintbuf(pb, "["); |
| 669 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 670 | sprintbuf(pb, "\n"); |
| 671 | for(ii=0; ii < json_object_array_length(jso); ii++) |
| 672 | { |
| 673 | struct json_object *val; |
| 674 | if (had_children) |
| 675 | { |
| 676 | sprintbuf(pb, ","); |
| 677 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 678 | sprintbuf(pb, "\n"); |
| 679 | } |
| 680 | had_children = 1; |
| 681 | if (flags & JSON_C_TO_STRING_SPACED) |
| 682 | sprintbuf(pb, " "); |
| 683 | indent(pb, level + 1, flags); |
| 684 | val = json_object_array_get_idx(jso, ii); |
| 685 | if(val == NULL) |
| 686 | sprintbuf(pb, "null"); |
| 687 | else |
| 688 | val->_to_json_string(val, pb, level+1, flags); |
| 689 | } |
| 690 | if (flags & JSON_C_TO_STRING_PRETTY) |
| 691 | { |
| 692 | if (had_children) |
| 693 | sprintbuf(pb, "\n"); |
| 694 | indent(pb,level,flags); |
| 695 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 696 | |
Eric Haszlakiewicz | 3fcffe1 | 2012-04-28 13:26:09 -0500 | [diff] [blame] | 697 | if (flags & JSON_C_TO_STRING_SPACED) |
| 698 | return sprintbuf(pb, " ]"); |
| 699 | else |
| 700 | return sprintbuf(pb, "]"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void json_object_array_entry_free(void *data) |
| 704 | { |
| 705 | json_object_put((struct json_object*)data); |
| 706 | } |
| 707 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 708 | static void json_object_array_delete(struct json_object* jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 709 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 710 | array_list_free(jso->o.c_array); |
| 711 | json_object_generic_delete(jso); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Michael Clark | e8de078 | 2009-02-25 01:45:00 +0000 | [diff] [blame] | 714 | struct json_object* json_object_new_array(void) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 715 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 716 | struct json_object *jso = json_object_new(json_type_array); |
| 717 | if(!jso) return NULL; |
| 718 | jso->_delete = &json_object_array_delete; |
| 719 | jso->_to_json_string = &json_object_array_to_json_string; |
| 720 | jso->o.c_array = array_list_new(&json_object_array_entry_free); |
| 721 | return jso; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 724 | struct array_list* json_object_get_array(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 725 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 726 | if(!jso) return NULL; |
| 727 | switch(jso->o_type) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 728 | case json_type_array: |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 729 | return jso->o.c_array; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 730 | default: |
| 731 | return NULL; |
| 732 | } |
| 733 | } |
| 734 | |
Frederik Deweerdt | c43871c | 2011-10-07 21:07:18 +0200 | [diff] [blame] | 735 | void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)) |
| 736 | { |
| 737 | array_list_sort(jso->o.c_array, sort_fn); |
| 738 | } |
| 739 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 740 | int json_object_array_length(struct json_object *jso) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 741 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 742 | return array_list_length(jso->o.c_array); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 745 | int json_object_array_add(struct json_object *jso,struct json_object *val) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 746 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 747 | return array_list_add(jso->o.c_array, val); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 750 | int json_object_array_put_idx(struct json_object *jso, int idx, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 751 | struct json_object *val) |
| 752 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 753 | return array_list_put_idx(jso->o.c_array, idx, val); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 756 | struct json_object* json_object_array_get_idx(struct json_object *jso, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 757 | int idx) |
| 758 | { |
Michael Clark | 266a3fd | 2009-02-25 01:55:31 +0000 | [diff] [blame] | 759 | return (struct json_object*)array_list_get_idx(jso->o.c_array, idx); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 760 | } |
| 761 | |