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;
95 if(pos - start_offset > 0)
96 printbuf_memappend(pb, str + start_offset, pos - start_offset);
97 if(c == '\b') printbuf_memappend(pb, "\\b", 2);
98 else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
99 else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
100 else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
101 else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
102 start_offset = ++pos;
106 if(pos - start_offset > 0)
107 printbuf_memappend(pb, str + start_offset, pos - start_offset);
108 sprintbuf(pb, "\\u00%c%c",
109 json_hex_chars[c >> 4],
110 json_hex_chars[c & 0xf]);
111 start_offset = ++pos;
115 if(pos - start_offset > 0)
116 printbuf_memappend(pb, str + start_offset, pos - start_offset);
121 /* reference counting */
123 extern struct json_object* json_object_get(struct json_object *this)
131 extern void json_object_put(struct json_object *this)
135 if(!this->_ref_count) this->_delete(this);
140 /* generic object construction and destruction parts */
142 static void json_object_generic_delete(struct json_object* this)
144 #ifdef REFCOUNT_DEBUG
145 mc_debug("json_object_delete_%s: %p\n",
146 json_type_name[this->o_type], this);
147 lh_table_delete(json_object_table, this);
149 printbuf_free(this->_pb);
153 static struct json_object* json_object_new(enum json_type o_type)
155 struct json_object *this = calloc(sizeof(struct json_object), 1);
156 if(!this) return NULL;
157 this->o_type = o_type;
158 this->_ref_count = 1;
159 this->_delete = &json_object_generic_delete;
160 #ifdef REFCOUNT_DEBUG
161 lh_table_insert(json_object_table, this, this);
162 mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
168 /* type checking functions */
170 int json_object_is_type(struct json_object *this, enum json_type type)
172 return (this->o_type == type);
175 enum json_type json_object_get_type(struct json_object *this)
181 /* json_object_to_json_string */
183 char* json_object_to_json_string(struct json_object *this)
185 if(!this) return "null";
187 if(!(this->_pb = printbuf_new())) return NULL;
189 printbuf_reset(this->_pb);
191 if(this->_to_json_string(this, this->_pb) < 0) return NULL;
192 return this->_pb->buf;
196 /* json_object_object */
198 static int json_object_object_to_json_string(struct json_object* this,
203 json_object_object_foreach(this, key, val) {
204 if(i) sprintbuf(pb, ",");
205 sprintbuf(pb, " \"");
206 json_escape_str(pb, key);
207 sprintbuf(pb, "\": ");
208 if(val == NULL) sprintbuf(pb, "null");
209 else val->_to_json_string(val, pb);
212 return sprintbuf(pb, " }");
215 static void json_object_lh_entry_free(struct lh_entry *ent)
218 json_object_put((struct json_object*)ent->v);
221 static void json_object_object_delete(struct json_object* this)
223 lh_table_free(this->o.c_object);
224 json_object_generic_delete(this);
227 struct json_object* json_object_new_object()
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);
238 struct lh_table* json_object_get_object(struct json_object *this)
240 if(!this) return NULL;
241 switch(this->o_type) {
242 case json_type_object:
243 return this->o.c_object;
249 void json_object_object_add(struct json_object* this, char *key,
250 struct json_object *val)
252 lh_table_delete(this->o.c_object, key);
253 lh_table_insert(this->o.c_object, strdup(key), val);
256 struct json_object* json_object_object_get(struct json_object* this, char *key)
258 return (struct json_object*) lh_table_lookup(this->o.c_object, key);
261 void json_object_object_del(struct json_object* this, char *key)
263 lh_table_delete(this->o.c_object, key);
267 /* json_object_boolean */
269 static int json_object_boolean_to_json_string(struct json_object* this,
272 if(this->o.c_boolean) return sprintbuf(pb, "true");
273 else return sprintbuf(pb, "false");
276 struct json_object* json_object_new_boolean(boolean b)
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;
285 boolean json_object_get_boolean(struct json_object *this)
287 if(!this) return FALSE;
288 switch(this->o_type) {
289 case json_type_boolean:
290 return this->o.c_boolean;
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;
303 /* json_object_int */
305 static int json_object_int_to_json_string(struct json_object* this,
308 return sprintbuf(pb, "%d", this->o.c_int);
311 struct json_object* json_object_new_int(int i)
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;
320 int json_object_get_int(struct json_object *this)
325 switch(this->o_type) {
327 return this->o.c_int;
328 case json_type_double:
329 return this->o.c_double;
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;
340 /* json_object_double */
342 static int json_object_double_to_json_string(struct json_object* this,
345 return sprintbuf(pb, "%lf", this->o.c_double);
348 struct json_object* json_object_new_double(double d)
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;
357 double json_object_get_double(struct json_object *this)
361 if(!this) return 0.0;
362 switch(this->o_type) {
363 case json_type_double:
364 return this->o.c_double;
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;
377 /* json_object_string */
379 static int json_object_string_to_json_string(struct json_object* this,
383 json_escape_str(pb, this->o.c_string);
388 static void json_object_string_delete(struct json_object* this)
390 free(this->o.c_string);
391 json_object_generic_delete(this);
394 struct json_object* json_object_new_string(char *s)
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);
404 struct json_object* json_object_new_string_len(char *s, int len)
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);
414 char* json_object_get_string(struct json_object *this)
416 if(!this) return NULL;
417 switch(this->o_type) {
418 case json_type_string:
419 return this->o.c_string;
421 return json_object_to_json_string(this);
426 /* json_object_array */
428 static int json_object_array_to_json_string(struct json_object* this,
432 for(int i=0; i < json_object_array_length(this); i++) {
433 if(i) sprintbuf(pb, ", ");
434 else sprintbuf(pb, " ");
435 struct json_object *val = json_object_array_get_idx(this, i);
436 if(val == NULL) sprintbuf(pb, "null");
437 else val->_to_json_string(val, pb);
439 return sprintbuf(pb, " ]");
442 static void json_object_array_entry_free(void *data)
444 json_object_put((struct json_object*)data);
447 static void json_object_array_delete(struct json_object* this)
449 array_list_free(this->o.c_array);
450 json_object_generic_delete(this);
453 struct json_object* json_object_new_array()
455 struct json_object *this = json_object_new(json_type_array);
456 if(!this) return NULL;
457 this->_delete = &json_object_array_delete;
458 this->_to_json_string = &json_object_array_to_json_string;
459 this->o.c_array = array_list_new(&json_object_array_entry_free);
463 struct array_list* json_object_get_array(struct json_object *this)
465 if(!this) return NULL;
466 switch(this->o_type) {
467 case json_type_array:
468 return this->o.c_array;
474 int json_object_array_length(struct json_object *this)
476 return array_list_length(this->o.c_array);
479 int json_object_array_add(struct json_object *this,struct json_object *val)
481 return array_list_add(this->o.c_array, val);
484 int json_object_array_put_idx(struct json_object *this, int idx,
485 struct json_object *val)
487 return array_list_put_idx(this->o.c_array, idx, val);
490 struct json_object* json_object_array_get_idx(struct json_object *this,
493 return (struct json_object*)array_list_get_idx(this->o.c_array, idx);