4 * Copyright Metaparadigm Pte. Ltd. 2004.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public (LGPL)
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details: http://www.gnu.org/
23 #include "libjson/debug.h"
24 #include "libjson/printbuf.h"
25 #include "libjson/linkhash.h"
26 #include "libjson/arraylist.h"
27 #include "libjson/json_object.h"
28 #include "libjson/json_object_private.h"
31 /* #define REFCOUNT_DEBUG */
33 char *json_number_chars = "0123456789.+-e";
34 char *json_hex_chars = "0123456789abcdef";
37 static char* json_type_name[] = {
48 static void json_object_generic_delete(struct json_object* this);
49 static struct json_object* json_object_new(enum json_type o_type);
52 /* ref count debugging */
56 static struct lh_table *json_object_table;
58 static void json_object_init() __attribute__ ((constructor));
59 static void json_object_init() {
60 mc_debug("json_object_init: creating object table\n");
61 json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
64 static void json_object_fini() __attribute__ ((destructor));
65 static void json_object_fini() {
67 if(mc_get_debug() && 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);
75 mc_debug("json_object_fini: freeing object table\n");
76 lh_table_free(json_object_table);
83 static int json_escape_str(struct printbuf *pb, char *str)
85 int pos = 0, start_offset = 0;
94 if(pos - start_offset > 0)
95 printbuf_memappend(pb, str + start_offset, pos - start_offset);
96 if(c == '\b') printbuf_memappend(pb, "\\b", 2);
97 else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
98 else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
99 else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
100 start_offset = ++pos;
104 if(pos - start_offset > 0)
105 printbuf_memappend(pb, str + start_offset, pos - start_offset);
106 sprintbuf(pb, "\\u00%c%c",
107 json_hex_chars[c >> 4],
108 json_hex_chars[c & 0xf]);
109 start_offset = ++pos;
113 if(pos - start_offset > 0)
114 printbuf_memappend(pb, str + start_offset, pos - start_offset);
119 /* reference counting */
121 extern struct json_object* json_object_get(struct json_object *this)
129 extern void json_object_put(struct json_object *this)
133 if(!this->_ref_count) this->_delete(this);
138 /* generic object construction and destruction parts */
140 static void json_object_generic_delete(struct json_object* this)
142 #ifdef REFCOUNT_DEBUG
143 mc_debug("json_object_delete_%s: %p\n",
144 json_type_name[this->o_type], this);
145 lh_table_delete(json_object_table, this);
147 printbuf_free(this->_pb);
151 static struct json_object* json_object_new(enum json_type o_type)
153 struct json_object *this = calloc(sizeof(struct json_object), 1);
154 if(!this) return NULL;
155 this->o_type = o_type;
156 this->_ref_count = 1;
157 this->_delete = &json_object_generic_delete;
158 #ifdef REFCOUNT_DEBUG
159 lh_table_insert(json_object_table, this, this);
160 mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
166 /* type checking functions */
168 int json_object_is_type(struct json_object *this, enum json_type type)
170 return (this->o_type == type);
173 enum json_type json_object_get_type(struct json_object *this)
179 /* json_object_to_json_string */
181 char* json_object_to_json_string(struct json_object *this)
183 if(!this) return "null";
185 if(!(this->_pb = printbuf_new())) return NULL;
187 printbuf_reset(this->_pb);
189 if(this->_to_json_string(this, this->_pb) < 0) return NULL;
190 return this->_pb->buf;
194 /* json_object_object */
196 static int json_object_object_to_json_string(struct json_object* this,
201 json_object_object_foreach(this, key, val) {
202 if(i) sprintbuf(pb, ",");
203 sprintbuf(pb, " \"");
204 json_escape_str(pb, key);
205 sprintbuf(pb, "\": ");
206 if(val == NULL) sprintbuf(pb, "null");
207 else val->_to_json_string(val, pb);
210 return sprintbuf(pb, " }");
213 static void json_object_lh_entry_free(struct lh_entry *ent)
216 json_object_put((struct json_object*)ent->v);
219 static void json_object_object_delete(struct json_object* this)
221 lh_table_free(this->o.c_object);
222 json_object_generic_delete(this);
225 struct json_object* json_object_new_object()
227 struct json_object *this = json_object_new(json_type_object);
228 if(!this) return NULL;
229 this->_delete = &json_object_object_delete;
230 this->_to_json_string = &json_object_object_to_json_string;
231 this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
232 NULL, &json_object_lh_entry_free);
236 struct lh_table* json_object_get_object(struct json_object *this)
238 if(!this) return NULL;
239 switch(this->o_type) {
240 case json_type_object:
241 return this->o.c_object;
247 void json_object_object_add(struct json_object* this, char *key,
248 struct json_object *val)
250 lh_table_delete(this->o.c_object, key);
251 lh_table_insert(this->o.c_object, strdup(key), val);
254 struct json_object* json_object_object_get(struct json_object* this, char *key)
256 return (struct json_object*) lh_table_lookup(this->o.c_object, key);
259 void json_object_object_del(struct json_object* this, char *key)
261 lh_table_delete(this->o.c_object, key);
265 /* json_object_boolean */
267 static int json_object_boolean_to_json_string(struct json_object* this,
270 if(this->o.c_boolean) return sprintbuf(pb, "true");
271 else return sprintbuf(pb, "false");
274 struct json_object* json_object_new_boolean(boolean b)
276 struct json_object *this = json_object_new(json_type_boolean);
277 if(!this) return NULL;
278 this->_to_json_string = &json_object_boolean_to_json_string;
279 this->o.c_boolean = b;
283 boolean json_object_get_boolean(struct json_object *this)
285 if(!this) return FALSE;
286 switch(this->o_type) {
287 case json_type_boolean:
288 return this->o.c_boolean;
290 return (this->o.c_int != 0);
291 case json_type_double:
292 return (this->o.c_double != 0);
293 case json_type_string:
294 if(strlen(this->o.c_string)) return TRUE;
301 /* json_object_int */
303 static int json_object_int_to_json_string(struct json_object* this,
306 return sprintbuf(pb, "%d", this->o.c_int);
309 struct json_object* json_object_new_int(int i)
311 struct json_object *this = json_object_new(json_type_int);
312 if(!this) return NULL;
313 this->_to_json_string = &json_object_int_to_json_string;
318 int json_object_get_int(struct json_object *this)
323 switch(this->o_type) {
325 return this->o.c_int;
326 case json_type_double:
327 return this->o.c_double;
328 case json_type_boolean:
329 return this->o.c_boolean;
330 case json_type_string:
331 if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint;
338 /* json_object_double */
340 static int json_object_double_to_json_string(struct json_object* this,
343 return sprintbuf(pb, "%lf", this->o.c_double);
346 struct json_object* json_object_new_double(double d)
348 struct json_object *this = json_object_new(json_type_double);
349 if(!this) return NULL;
350 this->_to_json_string = &json_object_double_to_json_string;
351 this->o.c_double = d;
355 double json_object_get_double(struct json_object *this)
359 if(!this) return 0.0;
360 switch(this->o_type) {
361 case json_type_double:
362 return this->o.c_double;
364 return this->o.c_int;
365 case json_type_boolean:
366 return this->o.c_boolean;
367 case json_type_string:
368 if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble;
375 /* json_object_string */
377 static int json_object_string_to_json_string(struct json_object* this,
381 json_escape_str(pb, this->o.c_string);
386 static void json_object_string_delete(struct json_object* this)
388 free(this->o.c_string);
389 json_object_generic_delete(this);
392 struct json_object* json_object_new_string(char *s)
394 struct json_object *this = json_object_new(json_type_string);
395 if(!this) return NULL;
396 this->_delete = &json_object_string_delete;
397 this->_to_json_string = &json_object_string_to_json_string;
398 this->o.c_string = strdup(s);
402 struct json_object* json_object_new_string_len(char *s, int len)
404 struct json_object *this = json_object_new(json_type_string);
405 if(!this) return NULL;
406 this->_delete = &json_object_string_delete;
407 this->_to_json_string = &json_object_string_to_json_string;
408 this->o.c_string = strndup(s, len);
412 char* json_object_get_string(struct json_object *this)
414 if(!this) return NULL;
415 switch(this->o_type) {
416 case json_type_string:
417 return this->o.c_string;
419 return json_object_to_json_string(this);
424 /* json_object_array */
426 static int json_object_array_to_json_string(struct json_object* this,
430 for(int i=0; i < json_object_array_length(this); i++) {
431 if(i) sprintbuf(pb, ", ");
432 else sprintbuf(pb, " ");
433 struct json_object *val = json_object_array_get_idx(this, i);
434 if(val == NULL) sprintbuf(pb, "null");
435 else val->_to_json_string(val, pb);
437 return sprintbuf(pb, " ]");
440 static void json_object_array_entry_free(void *data)
442 json_object_put((struct json_object*)data);
445 static void json_object_array_delete(struct json_object* this)
447 array_list_free(this->o.c_array);
448 json_object_generic_delete(this);
451 struct json_object* json_object_new_array()
453 struct json_object *this = json_object_new(json_type_array);
454 if(!this) return NULL;
455 this->_delete = &json_object_array_delete;
456 this->_to_json_string = &json_object_array_to_json_string;
457 this->o.c_array = array_list_new(&json_object_array_entry_free);
461 struct array_list* json_object_get_array(struct json_object *this)
463 if(!this) return NULL;
464 switch(this->o_type) {
465 case json_type_array:
466 return this->o.c_array;
472 int json_object_array_length(struct json_object *this)
474 return array_list_length(this->o.c_array);
477 int json_object_array_add(struct json_object *this,struct json_object *val)
479 return array_list_add(this->o.c_array, val);
482 int json_object_array_put_idx(struct json_object *this, int idx,
483 struct json_object *val)
485 return array_list_put_idx(this->o.c_array, idx, val);
488 struct json_object* json_object_array_get_idx(struct json_object *this,
491 return (struct json_object*)array_list_get_idx(this->o.c_array, idx);