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