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