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;
104 if(pos - start_offset > 0)
105 printbuf_memappend(pb, str + start_offset, pos - start_offset);
106 if(c == '\b') printbuf_memappend(pb, "\\b", 2);
107 else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
108 else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
109 else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
110 else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
111 else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
112 else if(c == '/') printbuf_memappend(pb, "\\/", 2);
113 start_offset = ++pos;
117 if(pos - start_offset > 0)
118 printbuf_memappend(pb, str + start_offset, pos - start_offset);
119 sprintbuf(pb, "\\u00%c%c",
120 json_hex_chars[c >> 4],
121 json_hex_chars[c & 0xf]);
122 start_offset = ++pos;
126 if(pos - start_offset > 0)
127 printbuf_memappend(pb, str + start_offset, pos - start_offset);
132 /* reference counting */
134 extern struct json_object* json_object_get(struct json_object *jso)
142 int json_object_put(struct json_object *jso)
149 if (jso->_user_delete)
150 jso->_user_delete(jso, jso->_userdata);
159 /* generic object construction and destruction parts */
161 static void json_object_generic_delete(struct json_object* jso)
163 #ifdef REFCOUNT_DEBUG
164 MC_DEBUG("json_object_delete_%s: %p\n",
165 json_type_to_name(jso->o_type), jso);
166 lh_table_delete(json_object_table, jso);
167 #endif /* REFCOUNT_DEBUG */
168 printbuf_free(jso->_pb);
172 static struct json_object* json_object_new(enum json_type o_type)
174 struct json_object *jso;
176 jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
177 if(!jso) return NULL;
178 jso->o_type = o_type;
180 jso->_delete = &json_object_generic_delete;
181 #ifdef REFCOUNT_DEBUG
182 lh_table_insert(json_object_table, jso, jso);
183 MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
184 #endif /* REFCOUNT_DEBUG */
189 /* type checking functions */
191 int json_object_is_type(struct json_object *jso, enum json_type type)
194 return (type == json_type_null);
195 return (jso->o_type == type);
198 enum json_type json_object_get_type(struct json_object *jso)
201 return json_type_null;
205 /* set a custom conversion to string */
207 void json_object_set_serializer(json_object *jso,
208 json_object_to_json_string_fn to_string_func,
210 json_object_delete_fn *user_delete)
212 // First, clean up any previously existing user info
213 if (jso->_user_delete)
215 jso->_user_delete(jso, jso->_userdata);
217 jso->_userdata = NULL;
218 jso->_user_delete = NULL;
220 if (to_string_func == NULL)
222 // Reset to the standard serialization function
226 jso->_to_json_string = NULL;
228 case json_type_boolean:
229 jso->_to_json_string = &json_object_boolean_to_json_string;
231 case json_type_double:
232 jso->_to_json_string = &json_object_double_to_json_string;
235 jso->_to_json_string = &json_object_int_to_json_string;
237 case json_type_object:
238 jso->_to_json_string = &json_object_object_to_json_string;
240 case json_type_array:
241 jso->_to_json_string = &json_object_array_to_json_string;
243 case json_type_string:
244 jso->_to_json_string = &json_object_string_to_json_string;
250 jso->_to_json_string = to_string_func;
251 jso->_userdata = userdata;
252 jso->_user_delete = user_delete;
256 /* extended conversion to string */
258 const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
263 if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
266 printbuf_reset(jso->_pb);
268 if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
271 return jso->_pb->buf;
274 /* backwards-compatible conversion to string */
276 const char* json_object_to_json_string(struct json_object *jso)
278 return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
281 static void indent(struct printbuf *pb, int level, int flags)
283 if (flags & JSON_C_TO_STRING_PRETTY)
285 printbuf_memset(pb, -1, ' ', level * 2);
289 /* json_object_object */
291 static int json_object_object_to_json_string(struct json_object* jso,
296 int had_children = 0;
297 struct json_object_iter iter;
299 sprintbuf(pb, "{" /*}*/);
300 if (flags & JSON_C_TO_STRING_PRETTY)
302 json_object_object_foreachC(jso, iter)
307 if (flags & JSON_C_TO_STRING_PRETTY)
311 if (flags & JSON_C_TO_STRING_SPACED)
313 indent(pb, level+1, flags);
315 json_escape_str(pb, iter.key, strlen(iter.key));
316 if (flags & JSON_C_TO_STRING_SPACED)
317 sprintbuf(pb, "\": ");
319 sprintbuf(pb, "\":");
321 sprintbuf(pb, "null");
323 iter.val->_to_json_string(iter.val, pb, level+1,flags);
325 if (flags & JSON_C_TO_STRING_PRETTY)
329 indent(pb,level,flags);
331 if (flags & JSON_C_TO_STRING_SPACED)
332 return sprintbuf(pb, /*{*/ " }");
334 return sprintbuf(pb, /*{*/ "}");
338 static void json_object_lh_entry_free(struct lh_entry *ent)
341 json_object_put((struct json_object*)ent->v);
344 static void json_object_object_delete(struct json_object* jso)
346 lh_table_free(jso->o.c_object);
347 json_object_generic_delete(jso);
350 struct json_object* json_object_new_object(void)
352 struct json_object *jso = json_object_new(json_type_object);
353 if(!jso) return NULL;
354 jso->_delete = &json_object_object_delete;
355 jso->_to_json_string = &json_object_object_to_json_string;
356 jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
357 NULL, &json_object_lh_entry_free);
361 struct lh_table* json_object_get_object(struct json_object *jso)
363 if(!jso) return NULL;
364 switch(jso->o_type) {
365 case json_type_object:
366 return jso->o.c_object;
372 void json_object_object_add(struct json_object* jso, const char *key,
373 struct json_object *val)
375 // We lookup the entry and replace the value, rather than just deleting
376 // and re-adding it, so the existing key remains valid.
377 json_object *existing_value = NULL;
378 struct lh_entry *existing_entry;
379 existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
382 lh_table_insert(jso->o.c_object, strdup(key), val);
385 existing_value = (void *)existing_entry->v;
387 json_object_put(existing_value);
388 existing_entry->v = val;
391 struct json_object* json_object_object_get(struct json_object* jso, const char *key)
393 struct json_object *result = NULL;
394 json_object_object_get_ex(jso, key, &result);
398 json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
408 case json_type_object:
409 return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
417 void json_object_object_del(struct json_object* jso, const char *key)
419 lh_table_delete(jso->o.c_object, key);
423 /* json_object_boolean */
425 static int json_object_boolean_to_json_string(struct json_object* jso,
430 if(jso->o.c_boolean) return sprintbuf(pb, "true");
431 else return sprintbuf(pb, "false");
434 struct json_object* json_object_new_boolean(json_bool b)
436 struct json_object *jso = json_object_new(json_type_boolean);
437 if(!jso) return NULL;
438 jso->_to_json_string = &json_object_boolean_to_json_string;
439 jso->o.c_boolean = b;
443 json_bool json_object_get_boolean(struct json_object *jso)
445 if(!jso) return FALSE;
446 switch(jso->o_type) {
447 case json_type_boolean:
448 return jso->o.c_boolean;
450 return (jso->o.c_int64 != 0);
451 case json_type_double:
452 return (jso->o.c_double != 0);
453 case json_type_string:
454 return (jso->o.c_string.len != 0);
461 /* json_object_int */
463 static int json_object_int_to_json_string(struct json_object* jso,
468 return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
471 struct json_object* json_object_new_int(int32_t i)
473 struct json_object *jso = json_object_new(json_type_int);
474 if(!jso) return NULL;
475 jso->_to_json_string = &json_object_int_to_json_string;
480 int32_t json_object_get_int(struct json_object *jso)
483 enum json_type o_type;
487 o_type = jso->o_type;
488 cint64 = jso->o.c_int64;
490 if (o_type == json_type_string)
493 * Parse strings into 64-bit numbers, then use the
494 * 64-to-32-bit number handling below.
496 if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
497 return 0; /* whoops, it didn't work. */
498 o_type = json_type_int;
503 /* Make sure we return the correct values for out of range numbers. */
504 if (cint64 <= INT32_MIN)
506 else if (cint64 >= INT32_MAX)
509 return (int32_t)cint64;
510 case json_type_double:
511 return (int32_t)jso->o.c_double;
512 case json_type_boolean:
513 return jso->o.c_boolean;
519 struct json_object* json_object_new_int64(int64_t i)
521 struct json_object *jso = json_object_new(json_type_int);
522 if(!jso) return NULL;
523 jso->_to_json_string = &json_object_int_to_json_string;
528 int64_t json_object_get_int64(struct json_object *jso)
533 switch(jso->o_type) {
535 return jso->o.c_int64;
536 case json_type_double:
537 return (int64_t)jso->o.c_double;
538 case json_type_boolean:
539 return jso->o.c_boolean;
540 case json_type_string:
541 if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
548 /* json_object_double */
550 static int json_object_double_to_json_string(struct json_object* jso,
555 return sprintbuf(pb, "%f", jso->o.c_double);
558 struct json_object* json_object_new_double(double d)
560 struct json_object *jso = json_object_new(json_type_double);
561 if(!jso) return NULL;
562 jso->_to_json_string = &json_object_double_to_json_string;
567 double json_object_get_double(struct json_object *jso)
572 switch(jso->o_type) {
573 case json_type_double:
574 return jso->o.c_double;
576 return jso->o.c_int64;
577 case json_type_boolean:
578 return jso->o.c_boolean;
579 case json_type_string:
580 if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
587 /* json_object_string */
589 static int json_object_string_to_json_string(struct json_object* jso,
595 json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
600 static void json_object_string_delete(struct json_object* jso)
602 free(jso->o.c_string.str);
603 json_object_generic_delete(jso);
606 struct json_object* json_object_new_string(const char *s)
608 struct json_object *jso = json_object_new(json_type_string);
609 if(!jso) return NULL;
610 jso->_delete = &json_object_string_delete;
611 jso->_to_json_string = &json_object_string_to_json_string;
612 jso->o.c_string.str = strdup(s);
613 jso->o.c_string.len = strlen(s);
617 struct json_object* json_object_new_string_len(const char *s, int len)
619 struct json_object *jso = json_object_new(json_type_string);
620 if(!jso) return NULL;
621 jso->_delete = &json_object_string_delete;
622 jso->_to_json_string = &json_object_string_to_json_string;
623 jso->o.c_string.str = (char*)malloc(len + 1);
624 memcpy(jso->o.c_string.str, (void *)s, len);
625 jso->o.c_string.str[len] = '\0';
626 jso->o.c_string.len = len;
630 const char* json_object_get_string(struct json_object *jso)
632 if(!jso) return NULL;
633 switch(jso->o_type) {
634 case json_type_string:
635 return jso->o.c_string.str;
637 return json_object_to_json_string(jso);
641 int json_object_get_string_len(struct json_object *jso) {
643 switch(jso->o_type) {
644 case json_type_string:
645 return jso->o.c_string.len;
652 /* json_object_array */
654 static int json_object_array_to_json_string(struct json_object* jso,
659 int had_children = 0;
662 if (flags & JSON_C_TO_STRING_PRETTY)
664 for(ii=0; ii < json_object_array_length(jso); ii++)
666 struct json_object *val;
670 if (flags & JSON_C_TO_STRING_PRETTY)
674 if (flags & JSON_C_TO_STRING_SPACED)
676 indent(pb, level + 1, flags);
677 val = json_object_array_get_idx(jso, ii);
679 sprintbuf(pb, "null");
681 val->_to_json_string(val, pb, level+1, flags);
683 if (flags & JSON_C_TO_STRING_PRETTY)
687 indent(pb,level,flags);
690 if (flags & JSON_C_TO_STRING_SPACED)
691 return sprintbuf(pb, " ]");
693 return sprintbuf(pb, "]");
696 static void json_object_array_entry_free(void *data)
698 json_object_put((struct json_object*)data);
701 static void json_object_array_delete(struct json_object* jso)
703 array_list_free(jso->o.c_array);
704 json_object_generic_delete(jso);
707 struct json_object* json_object_new_array(void)
709 struct json_object *jso = json_object_new(json_type_array);
710 if(!jso) return NULL;
711 jso->_delete = &json_object_array_delete;
712 jso->_to_json_string = &json_object_array_to_json_string;
713 jso->o.c_array = array_list_new(&json_object_array_entry_free);
717 struct array_list* json_object_get_array(struct json_object *jso)
719 if(!jso) return NULL;
720 switch(jso->o_type) {
721 case json_type_array:
722 return jso->o.c_array;
728 void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
730 array_list_sort(jso->o.c_array, sort_fn);
733 int json_object_array_length(struct json_object *jso)
735 return array_list_length(jso->o.c_array);
738 int json_object_array_add(struct json_object *jso,struct json_object *val)
740 return array_list_add(jso->o.c_array, val);
743 int json_object_array_put_idx(struct json_object *jso, int idx,
744 struct json_object *val)
746 return array_list_put_idx(jso->o.c_array, idx, val);
749 struct json_object* json_object_array_get_idx(struct json_object *jso,
752 return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);