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