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