2 * $Id: json_object.c,v 1.15 2006/01/30 23:07:57 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
21 #include "arraylist.h"
22 #include "json_object.h"
23 #include "json_object_private.h"
24 #include "json_tokener.h"
27 char* strndup(const char* str, size_t n);
28 #endif /* !HAVE_STRNDUP */
30 /* #define REFCOUNT_DEBUG 1 */
32 char *json_number_chars = "0123456789.+-e";
33 char *json_hex_chars = "0123456789abcdef";
36 static char* json_type_name[] = {
45 #endif /* REFCOUNT_DEBUG */
47 static void json_object_generic_delete(struct json_object* this);
48 static struct json_object* json_object_new(enum json_type o_type);
51 /* ref count debugging */
55 static struct lh_table *json_object_table;
57 static void json_object_init() __attribute__ ((constructor));
58 static void json_object_init() {
59 mc_debug("json_object_init: creating object table\n");
60 json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
63 static void json_object_fini() __attribute__ ((destructor));
64 static void json_object_fini() {
66 if(mc_get_debug() && json_object_table->count) {
67 mc_debug("json_object_fini: %d referenced objects at exit\n",
68 json_object_table->count);
69 lh_foreach(json_object_table, ent) {
70 struct json_object* obj = (struct json_object*)ent->v;
71 mc_debug("\t%s:%p\n", json_type_name[obj->o_type], obj);
74 mc_debug("json_object_fini: freeing object table\n");
75 lh_table_free(json_object_table);
77 #endif /* REFCOUNT_DEBUG */
82 static int json_escape_str(struct printbuf *pb, char *str)
84 int pos = 0, start_offset = 0;
96 if(pos - start_offset > 0)
97 printbuf_memappend(pb, str + start_offset, pos - start_offset);
98 if(c == '\b') printbuf_memappend(pb, "\\b", 2);
99 else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
100 else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
101 else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
102 else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
103 start_offset = ++pos;
107 if(pos - start_offset > 0)
108 printbuf_memappend(pb, str + start_offset, pos - start_offset);
109 sprintbuf(pb, "\\u00%c%c",
110 json_hex_chars[c >> 4],
111 json_hex_chars[c & 0xf]);
112 start_offset = ++pos;
116 if(pos - start_offset > 0)
117 printbuf_memappend(pb, str + start_offset, pos - start_offset);
122 /* reference counting */
124 extern struct json_object* json_object_get(struct json_object *this)
132 extern void json_object_put(struct json_object *this)
136 if(!this->_ref_count) this->_delete(this);
141 /* generic object construction and destruction parts */
143 static void json_object_generic_delete(struct json_object* this)
145 #ifdef REFCOUNT_DEBUG
146 mc_debug("json_object_delete_%s: %p\n",
147 json_type_name[this->o_type], this);
148 lh_table_delete(json_object_table, this);
149 #endif /* REFCOUNT_DEBUG */
150 printbuf_free(this->_pb);
154 static struct json_object* json_object_new(enum json_type o_type)
156 struct json_object *this = calloc(sizeof(struct json_object), 1);
157 if(!this) return NULL;
158 this->o_type = o_type;
159 this->_ref_count = 1;
160 this->_delete = &json_object_generic_delete;
161 #ifdef REFCOUNT_DEBUG
162 lh_table_insert(json_object_table, this, this);
163 mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
164 #endif /* REFCOUNT_DEBUG */
169 /* type checking functions */
171 int json_object_is_type(struct json_object *this, enum json_type type)
173 return (this->o_type == type);
176 enum json_type json_object_get_type(struct json_object *this)
182 /* json_object_to_json_string */
184 char* json_object_to_json_string(struct json_object *this)
186 if(!this) return "null";
188 if(!(this->_pb = printbuf_new())) return NULL;
190 printbuf_reset(this->_pb);
192 if(this->_to_json_string(this, this->_pb) < 0) return NULL;
193 return this->_pb->buf;
197 /* json_object_object */
199 static int json_object_object_to_json_string(struct json_object* this,
203 struct json_object_iter iter;
206 /* CAW: scope operator to make ANSI correctness */
207 /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
208 json_object_object_foreachC(this, iter) {
209 if(i) sprintbuf(pb, ",");
210 sprintbuf(pb, " \"");
211 json_escape_str(pb, iter.key);
212 sprintbuf(pb, "\": ");
213 if(iter.val == NULL) sprintbuf(pb, "null");
214 else iter.val->_to_json_string(iter.val, pb);
218 return sprintbuf(pb, " }");
221 static void json_object_lh_entry_free(struct lh_entry *ent)
224 json_object_put((struct json_object*)ent->v);
227 static void json_object_object_delete(struct json_object* this)
229 lh_table_free(this->o.c_object);
230 json_object_generic_delete(this);
233 struct json_object* json_object_new_object()
235 struct json_object *this = json_object_new(json_type_object);
236 if(!this) return NULL;
237 this->_delete = &json_object_object_delete;
238 this->_to_json_string = &json_object_object_to_json_string;
239 this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
240 NULL, &json_object_lh_entry_free);
244 struct lh_table* json_object_get_object(struct json_object *this)
246 if(!this) return NULL;
247 switch(this->o_type) {
248 case json_type_object:
249 return this->o.c_object;
255 void json_object_object_add(struct json_object* this, char *key,
256 struct json_object *val)
258 lh_table_delete(this->o.c_object, key);
259 lh_table_insert(this->o.c_object, strdup(key), val);
262 struct json_object* json_object_object_get(struct json_object* this, char *key)
264 return (struct json_object*) lh_table_lookup(this->o.c_object, key);
267 void json_object_object_del(struct json_object* this, char *key)
269 lh_table_delete(this->o.c_object, key);
273 /* json_object_boolean */
275 static int json_object_boolean_to_json_string(struct json_object* this,
278 if(this->o.c_boolean) return sprintbuf(pb, "true");
279 else return sprintbuf(pb, "false");
282 struct json_object* json_object_new_boolean(boolean b)
284 struct json_object *this = json_object_new(json_type_boolean);
285 if(!this) return NULL;
286 this->_to_json_string = &json_object_boolean_to_json_string;
287 this->o.c_boolean = b;
291 boolean json_object_get_boolean(struct json_object *this)
293 if(!this) return FALSE;
294 switch(this->o_type) {
295 case json_type_boolean:
296 return this->o.c_boolean;
298 return (this->o.c_int != 0);
299 case json_type_double:
300 return (this->o.c_double != 0);
301 case json_type_string:
302 if(strlen(this->o.c_string)) return TRUE;
309 /* json_object_int */
311 static int json_object_int_to_json_string(struct json_object* this,
314 return sprintbuf(pb, "%d", this->o.c_int);
317 struct json_object* json_object_new_int(int i)
319 struct json_object *this = json_object_new(json_type_int);
320 if(!this) return NULL;
321 this->_to_json_string = &json_object_int_to_json_string;
326 int json_object_get_int(struct json_object *this)
331 switch(this->o_type) {
333 return this->o.c_int;
334 case json_type_double:
335 return (int)this->o.c_double;
336 case json_type_boolean:
337 return this->o.c_boolean;
338 case json_type_string:
339 if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint;
346 /* json_object_double */
348 static int json_object_double_to_json_string(struct json_object* this,
351 return sprintbuf(pb, "%lf", this->o.c_double);
354 struct json_object* json_object_new_double(double d)
356 struct json_object *this = json_object_new(json_type_double);
357 if(!this) return NULL;
358 this->_to_json_string = &json_object_double_to_json_string;
359 this->o.c_double = d;
363 double json_object_get_double(struct json_object *this)
367 if(!this) return 0.0;
368 switch(this->o_type) {
369 case json_type_double:
370 return this->o.c_double;
372 return this->o.c_int;
373 case json_type_boolean:
374 return this->o.c_boolean;
375 case json_type_string:
376 if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble;
383 /* json_object_string */
385 static int json_object_string_to_json_string(struct json_object* this,
389 json_escape_str(pb, this->o.c_string);
394 static void json_object_string_delete(struct json_object* this)
396 free(this->o.c_string);
397 json_object_generic_delete(this);
400 struct json_object* json_object_new_string(char *s)
402 struct json_object *this = json_object_new(json_type_string);
403 if(!this) return NULL;
404 this->_delete = &json_object_string_delete;
405 this->_to_json_string = &json_object_string_to_json_string;
406 this->o.c_string = strdup(s);
410 struct json_object* json_object_new_string_len(char *s, int len)
412 struct json_object *this = json_object_new(json_type_string);
413 if(!this) return NULL;
414 this->_delete = &json_object_string_delete;
415 this->_to_json_string = &json_object_string_to_json_string;
416 this->o.c_string = strndup(s, len);
420 char* json_object_get_string(struct json_object *this)
422 if(!this) return NULL;
423 switch(this->o_type) {
424 case json_type_string:
425 return this->o.c_string;
427 return json_object_to_json_string(this);
432 /* json_object_array */
434 static int json_object_array_to_json_string(struct json_object* this,
439 for(i=0; i < json_object_array_length(this); i++) {
440 struct json_object *val;
441 if(i) { sprintbuf(pb, ", "); }
442 else { sprintbuf(pb, " "); }
444 val = json_object_array_get_idx(this, i);
445 if(val == NULL) { sprintbuf(pb, "null"); }
446 else { val->_to_json_string(val, pb); }
448 return sprintbuf(pb, " ]");
451 static void json_object_array_entry_free(void *data)
453 json_object_put((struct json_object*)data);
456 static void json_object_array_delete(struct json_object* this)
458 array_list_free(this->o.c_array);
459 json_object_generic_delete(this);
462 struct json_object* json_object_new_array()
464 struct json_object *this = json_object_new(json_type_array);
465 if(!this) return NULL;
466 this->_delete = &json_object_array_delete;
467 this->_to_json_string = &json_object_array_to_json_string;
468 this->o.c_array = array_list_new(&json_object_array_entry_free);
472 struct array_list* json_object_get_array(struct json_object *this)
474 if(!this) return NULL;
475 switch(this->o_type) {
476 case json_type_array:
477 return this->o.c_array;
483 int json_object_array_length(struct json_object *this)
485 return array_list_length(this->o.c_array);
488 int json_object_array_add(struct json_object *this,struct json_object *val)
490 return array_list_add(this->o.c_array, val);
493 int json_object_array_put_idx(struct json_object *this, int idx,
494 struct json_object *val)
496 return array_list_put_idx(this->o.c_array, idx, val);
499 struct json_object* json_object_array_get_idx(struct json_object *this,
502 return (struct json_object*)array_list_get_idx(this->o.c_array, idx);