2 * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
6 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
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.
23 #include "arraylist.h"
24 #include "json_inttypes.h"
25 #include "json_object.h"
26 #include "json_object_private.h"
27 #include "json_util.h"
29 #if !defined(HAVE_STRDUP) && defined(_MSC_VER)
30 /* MSC has the version as _strdup */
31 # define strdup _strdup
32 #elif !defined(HAVE_STRDUP)
33 # error You do not have strdup on your system.
34 #endif /* HAVE_STRDUP */
36 #if !defined(HAVE_STRNDUP)
37 char* strndup(const char* str, size_t n);
38 #endif /* !HAVE_STRNDUP */
40 // Don't define this. It's not thread-safe.
41 /* #define REFCOUNT_DEBUG 1 */
43 const char *json_number_chars = "0123456789.+-eE";
44 const char *json_hex_chars = "0123456789abcdefABCDEF";
46 static void json_object_generic_delete(struct json_object* jso);
47 static struct json_object* json_object_new(enum json_type o_type);
49 static json_object_to_json_string_fn json_object_object_to_json_string;
50 static json_object_to_json_string_fn json_object_boolean_to_json_string;
51 static json_object_to_json_string_fn json_object_int_to_json_string;
52 static json_object_to_json_string_fn json_object_double_to_json_string;
53 static json_object_to_json_string_fn json_object_string_to_json_string;
54 static json_object_to_json_string_fn json_object_array_to_json_string;
57 /* ref count debugging */
61 static struct lh_table *json_object_table;
63 static void json_object_init(void) __attribute__ ((constructor));
64 static void json_object_init(void) {
65 MC_DEBUG("json_object_init: creating object table\n");
66 json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
69 static void json_object_fini(void) __attribute__ ((destructor));
70 static void json_object_fini(void) {
73 if (json_object_table->count) {
74 MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
75 json_object_table->count);
76 lh_foreach(json_object_table, ent) {
77 struct json_object* obj = (struct json_object*)ent->v;
78 MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
82 MC_DEBUG("json_object_fini: freeing object table\n");
83 lh_table_free(json_object_table);
85 #endif /* REFCOUNT_DEBUG */
90 static int json_escape_str(struct printbuf *pb, char *str, int len)
92 int pos = 0, start_offset = 0;
105 if(pos - start_offset > 0)
106 printbuf_memappend(pb, str + start_offset, pos - start_offset);
107 if(c == '\b') printbuf_memappend(pb, "\\b", 2);
108 else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
109 else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
110 else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
111 else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
112 else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
113 else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
114 else if(c == '/') printbuf_memappend(pb, "\\/", 2);
115 start_offset = ++pos;
119 if(pos - start_offset > 0)
120 printbuf_memappend(pb, str + start_offset, pos - start_offset);
121 sprintbuf(pb, "\\u00%c%c",
122 json_hex_chars[c >> 4],
123 json_hex_chars[c & 0xf]);
124 start_offset = ++pos;
128 if(pos - start_offset > 0)
129 printbuf_memappend(pb, str + start_offset, pos - start_offset);
134 /* reference counting */
136 extern struct json_object* json_object_get(struct json_object *jso)
144 int json_object_put(struct json_object *jso)
151 if (jso->_user_delete)
152 jso->_user_delete(jso, jso->_userdata);
161 /* generic object construction and destruction parts */
163 static void json_object_generic_delete(struct json_object* jso)
165 #ifdef REFCOUNT_DEBUG
166 MC_DEBUG("json_object_delete_%s: %p\n",
167 json_type_to_name(jso->o_type), jso);
168 lh_table_delete(json_object_table, jso);
169 #endif /* REFCOUNT_DEBUG */
170 printbuf_free(jso->_pb);
174 static struct json_object* json_object_new(enum json_type o_type)
176 struct json_object *jso;
178 jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
179 if(!jso) return NULL;
180 jso->o_type = o_type;
182 jso->_delete = &json_object_generic_delete;
183 #ifdef REFCOUNT_DEBUG
184 lh_table_insert(json_object_table, jso, jso);
185 MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
186 #endif /* REFCOUNT_DEBUG */
191 /* type checking functions */
193 int json_object_is_type(struct json_object *jso, enum json_type type)
196 return (type == json_type_null);
197 return (jso->o_type == type);
200 enum json_type json_object_get_type(struct json_object *jso)
203 return json_type_null;
207 /* set a custom conversion to string */
209 void json_object_set_serializer(json_object *jso,
210 json_object_to_json_string_fn to_string_func,
212 json_object_delete_fn *user_delete)
214 // First, clean up any previously existing user info
215 if (jso->_user_delete)
217 jso->_user_delete(jso, jso->_userdata);
219 jso->_userdata = NULL;
220 jso->_user_delete = NULL;
222 if (to_string_func == NULL)
224 // Reset to the standard serialization function
228 jso->_to_json_string = NULL;
230 case json_type_boolean:
231 jso->_to_json_string = &json_object_boolean_to_json_string;
233 case json_type_double:
234 jso->_to_json_string = &json_object_double_to_json_string;
237 jso->_to_json_string = &json_object_int_to_json_string;
239 case json_type_object:
240 jso->_to_json_string = &json_object_object_to_json_string;
242 case json_type_array:
243 jso->_to_json_string = &json_object_array_to_json_string;
245 case json_type_string:
246 jso->_to_json_string = &json_object_string_to_json_string;
252 jso->_to_json_string = to_string_func;
253 jso->_userdata = userdata;
254 jso->_user_delete = user_delete;
258 /* extended conversion to string */
260 const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
265 if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
268 printbuf_reset(jso->_pb);
270 if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
273 return jso->_pb->buf;
276 /* backwards-compatible conversion to string */
278 const char* json_object_to_json_string(struct json_object *jso)
280 return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
283 static void indent(struct printbuf *pb, int level, int flags)
285 if (flags & JSON_C_TO_STRING_PRETTY)
287 printbuf_memset(pb, -1, ' ', level * 2);
291 /* json_object_object */
293 static int json_object_object_to_json_string(struct json_object* jso,
298 int had_children = 0;
299 struct json_object_iter iter;
301 sprintbuf(pb, "{" /*}*/);
302 if (flags & JSON_C_TO_STRING_PRETTY)
304 json_object_object_foreachC(jso, iter)
309 if (flags & JSON_C_TO_STRING_PRETTY)
313 if (flags & JSON_C_TO_STRING_SPACED)
315 indent(pb, level+1, flags);
317 json_escape_str(pb, iter.key, strlen(iter.key));
318 if (flags & JSON_C_TO_STRING_SPACED)
319 sprintbuf(pb, "\": ");
321 sprintbuf(pb, "\":");
323 sprintbuf(pb, "null");
325 iter.val->_to_json_string(iter.val, pb, level+1,flags);
327 if (flags & JSON_C_TO_STRING_PRETTY)
331 indent(pb,level,flags);
333 if (flags & JSON_C_TO_STRING_SPACED)
334 return sprintbuf(pb, /*{*/ " }");
336 return sprintbuf(pb, /*{*/ "}");
340 static void json_object_lh_entry_free(struct lh_entry *ent)
343 json_object_put((struct json_object*)ent->v);
346 static void json_object_object_delete(struct json_object* jso)
348 lh_table_free(jso->o.c_object);
349 json_object_generic_delete(jso);
352 struct json_object* json_object_new_object(void)
354 struct json_object *jso = json_object_new(json_type_object);
355 if(!jso) return NULL;
356 jso->_delete = &json_object_object_delete;
357 jso->_to_json_string = &json_object_object_to_json_string;
358 jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
359 NULL, &json_object_lh_entry_free);
363 struct lh_table* json_object_get_object(struct json_object *jso)
365 if(!jso) return NULL;
366 switch(jso->o_type) {
367 case json_type_object:
368 return jso->o.c_object;
374 void json_object_object_add(struct json_object* jso, const char *key,
375 struct json_object *val)
377 // We lookup the entry and replace the value, rather than just deleting
378 // and re-adding it, so the existing key remains valid.
379 json_object *existing_value = NULL;
380 struct lh_entry *existing_entry;
381 existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
384 lh_table_insert(jso->o.c_object, strdup(key), val);
387 existing_value = (void *)existing_entry->v;
389 json_object_put(existing_value);
390 existing_entry->v = val;
393 struct json_object* json_object_object_get(struct json_object* jso, const char *key)
395 struct json_object *result = NULL;
396 json_object_object_get_ex(jso, key, &result);
400 json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
410 case json_type_object:
411 return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
419 void json_object_object_del(struct json_object* jso, const char *key)
421 lh_table_delete(jso->o.c_object, key);
425 /* json_object_boolean */
427 static int json_object_boolean_to_json_string(struct json_object* jso,
432 if(jso->o.c_boolean) return sprintbuf(pb, "true");
433 else return sprintbuf(pb, "false");
436 struct json_object* json_object_new_boolean(json_bool b)
438 struct json_object *jso = json_object_new(json_type_boolean);
439 if(!jso) return NULL;
440 jso->_to_json_string = &json_object_boolean_to_json_string;
441 jso->o.c_boolean = b;
445 json_bool json_object_get_boolean(struct json_object *jso)
447 if(!jso) return FALSE;
448 switch(jso->o_type) {
449 case json_type_boolean:
450 return jso->o.c_boolean;
452 return (jso->o.c_int64 != 0);
453 case json_type_double:
454 return (jso->o.c_double != 0);
455 case json_type_string:
456 return (jso->o.c_string.len != 0);
463 /* json_object_int */
465 static int json_object_int_to_json_string(struct json_object* jso,
470 return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
473 struct json_object* json_object_new_int(int32_t i)
475 struct json_object *jso = json_object_new(json_type_int);
476 if(!jso) return NULL;
477 jso->_to_json_string = &json_object_int_to_json_string;
482 int32_t json_object_get_int(struct json_object *jso)
485 enum json_type o_type;
489 o_type = jso->o_type;
490 cint64 = jso->o.c_int64;
492 if (o_type == json_type_string)
495 * Parse strings into 64-bit numbers, then use the
496 * 64-to-32-bit number handling below.
498 if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
499 return 0; /* whoops, it didn't work. */
500 o_type = json_type_int;
505 /* Make sure we return the correct values for out of range numbers. */
506 if (cint64 <= INT32_MIN)
508 else if (cint64 >= INT32_MAX)
511 return (int32_t)cint64;
512 case json_type_double:
513 return (int32_t)jso->o.c_double;
514 case json_type_boolean:
515 return jso->o.c_boolean;
521 struct json_object* json_object_new_int64(int64_t i)
523 struct json_object *jso = json_object_new(json_type_int);
524 if(!jso) return NULL;
525 jso->_to_json_string = &json_object_int_to_json_string;
530 int64_t json_object_get_int64(struct json_object *jso)
535 switch(jso->o_type) {
537 return jso->o.c_int64;
538 case json_type_double:
539 return (int64_t)jso->o.c_double;
540 case json_type_boolean:
541 return jso->o.c_boolean;
542 case json_type_string:
543 if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
550 /* json_object_double */
552 static int json_object_double_to_json_string(struct json_object* jso,
557 return sprintbuf(pb, "%f", jso->o.c_double);
560 struct json_object* json_object_new_double(double d)
562 struct json_object *jso = json_object_new(json_type_double);
563 if(!jso) return NULL;
564 jso->_to_json_string = &json_object_double_to_json_string;
569 double json_object_get_double(struct json_object *jso)
574 switch(jso->o_type) {
575 case json_type_double:
576 return jso->o.c_double;
578 return jso->o.c_int64;
579 case json_type_boolean:
580 return jso->o.c_boolean;
581 case json_type_string:
582 if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
589 /* json_object_string */
591 static int json_object_string_to_json_string(struct json_object* jso,
597 json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
602 static void json_object_string_delete(struct json_object* jso)
604 free(jso->o.c_string.str);
605 json_object_generic_delete(jso);
608 struct json_object* json_object_new_string(const char *s)
610 struct json_object *jso = json_object_new(json_type_string);
611 if(!jso) return NULL;
612 jso->_delete = &json_object_string_delete;
613 jso->_to_json_string = &json_object_string_to_json_string;
614 jso->o.c_string.str = strdup(s);
615 jso->o.c_string.len = strlen(s);
619 struct json_object* json_object_new_string_len(const char *s, int len)
621 struct json_object *jso = json_object_new(json_type_string);
622 if(!jso) return NULL;
623 jso->_delete = &json_object_string_delete;
624 jso->_to_json_string = &json_object_string_to_json_string;
625 jso->o.c_string.str = (char*)malloc(len + 1);
626 memcpy(jso->o.c_string.str, (void *)s, len);
627 jso->o.c_string.str[len] = '\0';
628 jso->o.c_string.len = len;
632 const char* json_object_get_string(struct json_object *jso)
634 if(!jso) return NULL;
635 switch(jso->o_type) {
636 case json_type_string:
637 return jso->o.c_string.str;
639 return json_object_to_json_string(jso);
643 int json_object_get_string_len(struct json_object *jso) {
645 switch(jso->o_type) {
646 case json_type_string:
647 return jso->o.c_string.len;
654 /* json_object_array */
656 static int json_object_array_to_json_string(struct json_object* jso,
661 int had_children = 0;
664 if (flags & JSON_C_TO_STRING_PRETTY)
666 for(ii=0; ii < json_object_array_length(jso); ii++)
668 struct json_object *val;
672 if (flags & JSON_C_TO_STRING_PRETTY)
676 if (flags & JSON_C_TO_STRING_SPACED)
678 indent(pb, level + 1, flags);
679 val = json_object_array_get_idx(jso, ii);
681 sprintbuf(pb, "null");
683 val->_to_json_string(val, pb, level+1, flags);
685 if (flags & JSON_C_TO_STRING_PRETTY)
689 indent(pb,level,flags);
692 if (flags & JSON_C_TO_STRING_SPACED)
693 return sprintbuf(pb, " ]");
695 return sprintbuf(pb, "]");
698 static void json_object_array_entry_free(void *data)
700 json_object_put((struct json_object*)data);
703 static void json_object_array_delete(struct json_object* jso)
705 array_list_free(jso->o.c_array);
706 json_object_generic_delete(jso);
709 struct json_object* json_object_new_array(void)
711 struct json_object *jso = json_object_new(json_type_array);
712 if(!jso) return NULL;
713 jso->_delete = &json_object_array_delete;
714 jso->_to_json_string = &json_object_array_to_json_string;
715 jso->o.c_array = array_list_new(&json_object_array_entry_free);
719 struct array_list* json_object_get_array(struct json_object *jso)
721 if(!jso) return NULL;
722 switch(jso->o_type) {
723 case json_type_array:
724 return jso->o.c_array;
730 void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
732 array_list_sort(jso->o.c_array, sort_fn);
735 int json_object_array_length(struct json_object *jso)
737 return array_list_length(jso->o.c_array);
740 int json_object_array_add(struct json_object *jso,struct json_object *val)
742 return array_list_add(jso->o.c_array, val);
745 int json_object_array_put_idx(struct json_object *jso, int idx,
746 struct json_object *val)
748 return array_list_put_idx(jso->o.c_array, idx, val);
751 struct json_object* json_object_array_get_idx(struct json_object *jso,
754 return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);