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