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