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