]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libjson/json_object.c
added escaping for internal " characters
[Evergreen.git] / OpenSRF / src / libjson / json_object.c
1 /*
2  * $Id$
3  *
4  * Copyright Metaparadigm Pte. Ltd. 2004.
5  * Michael Clark <michael@metaparadigm.com>
6  *
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.
11  *
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/
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
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"
29
30
31 /* #define REFCOUNT_DEBUG */
32
33 char *json_number_chars = "0123456789.+-e";
34 char *json_hex_chars = "0123456789abcdef";
35
36 #ifdef REFCOUNT_DEBUG
37 static char* json_type_name[] = {
38   "null",
39   "boolean",
40   "double",
41   "int",
42   "object",
43   "array",
44   "string",
45 };
46 #endif
47
48 static void json_object_generic_delete(struct json_object* this);
49 static struct json_object* json_object_new(enum json_type o_type);
50
51
52 /* ref count debugging */
53
54 #ifdef REFCOUNT_DEBUG
55
56 static struct lh_table *json_object_table;
57
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);
62 }
63
64 static void json_object_fini() __attribute__ ((destructor));
65 static void json_object_fini() {
66   struct lh_entry *ent;
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);
73     }
74   }
75   mc_debug("json_object_fini: freeing object table\n");
76   lh_table_free(json_object_table);
77 }
78 #endif
79
80
81 /* string escaping */
82
83 static int json_escape_str(struct printbuf *pb, char *str)
84 {
85   int pos = 0, start_offset = 0;
86   char c;
87   do {
88     c = str[pos];
89     switch(c) {
90     case '\b':
91     case '\n':
92     case '\r':
93     case '\t':
94          case '"':
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;
103       break;
104     default:
105       if(c && c < ' ') {
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;
112       } else if(c) pos++;
113     }
114   } while(c);
115   if(pos - start_offset > 0)
116     printbuf_memappend(pb, str + start_offset, pos - start_offset);
117   return 0;
118 }
119
120
121 /* reference counting */
122
123 extern struct json_object* json_object_get(struct json_object *this)
124 {
125   if(this) {
126     this->_ref_count++;
127   }
128   return this;
129 }
130
131 extern void json_object_put(struct json_object *this)
132 {
133   if(this) {
134     this->_ref_count--;
135     if(!this->_ref_count) this->_delete(this);
136   }
137 }
138
139
140 /* generic object construction and destruction parts */
141
142 static void json_object_generic_delete(struct json_object* this)
143 {
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);
148 #endif
149   printbuf_free(this->_pb);
150   free(this);
151 }
152
153 static struct json_object* json_object_new(enum json_type o_type)
154 {
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);
163 #endif
164   return this;
165 }
166
167
168 /* type checking functions */
169
170 int json_object_is_type(struct json_object *this, enum json_type type)
171 {
172   return (this->o_type == type);
173 }
174
175 enum json_type json_object_get_type(struct json_object *this)
176 {
177   return this->o_type;
178 }
179
180
181 /* json_object_to_json_string */
182
183 char* json_object_to_json_string(struct json_object *this)
184 {
185   if(!this) return "null";
186   if(!this->_pb) {
187     if(!(this->_pb = printbuf_new())) return NULL;
188   } else {
189     printbuf_reset(this->_pb);
190   }
191   if(this->_to_json_string(this, this->_pb) < 0) return NULL;
192   return this->_pb->buf;
193 }
194
195
196 /* json_object_object */
197
198 static int json_object_object_to_json_string(struct json_object* this,
199                                              struct printbuf *pb)
200 {
201   int i=0;
202   sprintbuf(pb, "{");
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);
210     i++;
211   }
212   return sprintbuf(pb, " }");
213 }
214
215 static void json_object_lh_entry_free(struct lh_entry *ent)
216 {
217   free(ent->k);
218   json_object_put((struct json_object*)ent->v);
219 }
220
221 static void json_object_object_delete(struct json_object* this)
222 {
223   lh_table_free(this->o.c_object);
224   json_object_generic_delete(this);
225 }
226
227 struct json_object* json_object_new_object()
228 {
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);
235   return this;
236 }
237
238 struct lh_table* json_object_get_object(struct json_object *this)
239 {
240   if(!this) return NULL;
241   switch(this->o_type) {
242   case json_type_object:
243     return this->o.c_object;
244   default:
245     return NULL;
246   }
247 }
248
249 void json_object_object_add(struct json_object* this, char *key,
250                             struct json_object *val)
251 {
252   lh_table_delete(this->o.c_object, key);
253   lh_table_insert(this->o.c_object, strdup(key), val);
254 }
255
256 struct json_object* json_object_object_get(struct json_object* this, char *key)
257 {
258   return (struct json_object*) lh_table_lookup(this->o.c_object, key);
259 }
260
261 void json_object_object_del(struct json_object* this, char *key)
262 {
263   lh_table_delete(this->o.c_object, key);
264 }
265
266
267 /* json_object_boolean */
268
269 static int json_object_boolean_to_json_string(struct json_object* this,
270                                               struct printbuf *pb)
271 {
272   if(this->o.c_boolean) return sprintbuf(pb, "true");
273   else return sprintbuf(pb, "false");
274 }
275
276 struct json_object* json_object_new_boolean(boolean b)
277 {
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;
282   return this;
283 }
284
285 boolean json_object_get_boolean(struct json_object *this)
286 {
287   if(!this) return FALSE;
288   switch(this->o_type) {
289   case json_type_boolean:
290     return this->o.c_boolean;
291   case json_type_int:
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;
297   default:
298     return TRUE;
299   }
300 }
301
302
303 /* json_object_int */
304
305 static int json_object_int_to_json_string(struct json_object* this,
306                                           struct printbuf *pb)
307 {
308   return sprintbuf(pb, "%d", this->o.c_int);
309 }
310
311 struct json_object* json_object_new_int(int i)
312 {
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;
316   this->o.c_int = i;
317   return this;
318 }
319
320 int json_object_get_int(struct json_object *this)
321 {
322   int cint;
323
324   if(!this) return 0;
325   switch(this->o_type) {
326   case json_type_int:
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;
334   default:
335     return 0;
336   }
337 }
338
339
340 /* json_object_double */
341
342 static int json_object_double_to_json_string(struct json_object* this,
343                                              struct printbuf *pb)
344 {
345   return sprintbuf(pb, "%lf", this->o.c_double);
346 }
347
348 struct json_object* json_object_new_double(double d)
349 {
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;
354   return this;
355 }
356
357 double json_object_get_double(struct json_object *this)
358 {
359   double cdouble;
360
361   if(!this) return 0.0;
362   switch(this->o_type) {
363   case json_type_double:
364     return this->o.c_double;
365   case json_type_int:
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;
371   default:
372     return 0.0;
373   }
374 }
375
376
377 /* json_object_string */
378
379 static int json_object_string_to_json_string(struct json_object* this,
380                                              struct printbuf *pb)
381 {
382   sprintbuf(pb, "\"");
383   json_escape_str(pb, this->o.c_string);
384   sprintbuf(pb, "\"");
385   return 0;
386 }
387
388 static void json_object_string_delete(struct json_object* this)
389 {
390   free(this->o.c_string);
391   json_object_generic_delete(this);
392 }
393
394 struct json_object* json_object_new_string(char *s)
395 {
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);
401   return this;
402 }
403
404 struct json_object* json_object_new_string_len(char *s, int len)
405 {
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);
411   return this;
412 }
413
414 char* json_object_get_string(struct json_object *this)
415 {
416   if(!this) return NULL;
417   switch(this->o_type) {
418   case json_type_string:
419     return this->o.c_string;
420   default:
421     return json_object_to_json_string(this);
422   }
423 }
424
425
426 /* json_object_array */
427
428 static int json_object_array_to_json_string(struct json_object* this,
429                                             struct printbuf *pb)
430 {
431   sprintbuf(pb, "[");
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);
438   }
439   return sprintbuf(pb, " ]");
440 }
441
442 static void json_object_array_entry_free(void *data)
443 {
444   json_object_put((struct json_object*)data);
445 }
446
447 static void json_object_array_delete(struct json_object* this)
448 {
449   array_list_free(this->o.c_array);
450   json_object_generic_delete(this);
451 }
452
453 struct json_object* json_object_new_array()
454 {
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);
460   return this;
461 }
462
463 struct array_list* json_object_get_array(struct json_object *this)
464 {
465   if(!this) return NULL;
466   switch(this->o_type) {
467   case json_type_array:
468     return this->o.c_array;
469   default:
470     return NULL;
471   }
472 }
473
474 int json_object_array_length(struct json_object *this)
475 {
476   return array_list_length(this->o.c_array);
477 }
478
479 int json_object_array_add(struct json_object *this,struct json_object *val)
480 {
481   return array_list_add(this->o.c_array, val);
482 }
483
484 int json_object_array_put_idx(struct json_object *this, int idx,
485                               struct json_object *val)
486 {
487   return array_list_put_idx(this->o.c_array, idx, val);
488 }
489
490 struct json_object* json_object_array_get_idx(struct json_object *this,
491                                               int idx)
492 {
493   return (struct json_object*)array_list_get_idx(this->o.c_array, idx);
494 }
495