You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  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.
  9. *
  10. */
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <string.h>
  16. #include "debug.h"
  17. #include "printbuf.h"
  18. #include "linkhash.h"
  19. #include "arraylist.h"
  20. #include "json_object.h"
  21. #include "json_object_private.h"
  22. #if !HAVE_STRNDUP
  23. char* strndup(const char* str, size_t n);
  24. #endif /* !HAVE_STRNDUP */
  25. /* #define REFCOUNT_DEBUG 1 */
  26. const char *json_number_chars = "0123456789.+-eE";
  27. const char *json_hex_chars = "0123456789abcdef";
  28. #ifdef REFCOUNT_DEBUG
  29. static const char* json_type_name[] = {
  30. "null",
  31. "boolean",
  32. "double",
  33. "int",
  34. "object",
  35. "array",
  36. "string",
  37. };
  38. #endif /* REFCOUNT_DEBUG */
  39. static void json_object_generic_delete(struct json_object* jso);
  40. static struct json_object* json_object_new(enum json_type o_type);
  41. /* ref count debugging */
  42. #ifdef REFCOUNT_DEBUG
  43. static struct lh_table *json_object_table;
  44. static void json_object_init(void) __attribute__ ((constructor));
  45. static void json_object_init(void) {
  46. MC_DEBUG("json_object_init: creating object table\n");
  47. json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
  48. }
  49. static void json_object_fini(void) __attribute__ ((destructor));
  50. static void json_object_fini(void) {
  51. struct lh_entry *ent;
  52. if(MC_GET_DEBUG()) {
  53. if (json_object_table->count) {
  54. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  55. json_object_table->count);
  56. lh_foreach(json_object_table, ent) {
  57. struct json_object* obj = (struct json_object*)ent->v;
  58. MC_DEBUG("\t%s:%p\n", json_type_name[obj->o_type], obj);
  59. }
  60. }
  61. }
  62. MC_DEBUG("json_object_fini: freeing object table\n");
  63. lh_table_free(json_object_table);
  64. }
  65. #endif /* REFCOUNT_DEBUG */
  66. /* string escaping */
  67. static int json_escape_str(struct printbuf *pb, char *str)
  68. {
  69. int pos = 0, start_offset = 0;
  70. unsigned char c;
  71. do {
  72. c = str[pos];
  73. switch(c) {
  74. case '\0':
  75. break;
  76. case '\b':
  77. case '\n':
  78. case '\r':
  79. case '\t':
  80. case '"':
  81. case '\\':
  82. case '/':
  83. if(pos - start_offset > 0)
  84. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  85. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  86. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  87. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  88. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  89. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  90. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  91. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  92. start_offset = ++pos;
  93. break;
  94. default:
  95. if(c < ' ') {
  96. if(pos - start_offset > 0)
  97. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  98. sprintbuf(pb, "\\u00%c%c",
  99. json_hex_chars[c >> 4],
  100. json_hex_chars[c & 0xf]);
  101. start_offset = ++pos;
  102. } else pos++;
  103. }
  104. } while(c);
  105. if(pos - start_offset > 0)
  106. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  107. return 0;
  108. }
  109. /* reference counting */
  110. extern struct json_object* json_object_get(struct json_object *jso)
  111. {
  112. if(jso) {
  113. jso->_ref_count++;
  114. }
  115. return jso;
  116. }
  117. extern void json_object_put(struct json_object *jso)
  118. {
  119. if(jso) {
  120. jso->_ref_count--;
  121. if(!jso->_ref_count) jso->_delete(jso);
  122. }
  123. }
  124. /* generic object construction and destruction parts */
  125. static void json_object_generic_delete(struct json_object* jso)
  126. {
  127. #ifdef REFCOUNT_DEBUG
  128. MC_DEBUG("json_object_delete_%s: %p\n",
  129. json_type_name[jso->o_type], jso);
  130. lh_table_delete(json_object_table, jso);
  131. #endif /* REFCOUNT_DEBUG */
  132. printbuf_free(jso->_pb);
  133. free(jso);
  134. }
  135. static struct json_object* json_object_new(enum json_type o_type)
  136. {
  137. struct json_object *jso;
  138. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  139. if(!jso) return NULL;
  140. jso->o_type = o_type;
  141. jso->_ref_count = 1;
  142. jso->_delete = &json_object_generic_delete;
  143. #ifdef REFCOUNT_DEBUG
  144. lh_table_insert(json_object_table, jso, jso);
  145. MC_DEBUG("json_object_new_%s: %p\n", json_type_name[jso->o_type], jso);
  146. #endif /* REFCOUNT_DEBUG */
  147. return jso;
  148. }
  149. /* type checking functions */
  150. int json_object_is_type(struct json_object *jso, enum json_type type)
  151. {
  152. return (jso->o_type == type);
  153. }
  154. enum json_type json_object_get_type(struct json_object *jso)
  155. {
  156. return jso->o_type;
  157. }
  158. /* json_object_to_json_string */
  159. const char* json_object_to_json_string(struct json_object *jso)
  160. {
  161. if(!jso) return "null";
  162. if(!jso->_pb) {
  163. if(!(jso->_pb = printbuf_new())) return NULL;
  164. } else {
  165. printbuf_reset(jso->_pb);
  166. }
  167. if(jso->_to_json_string(jso, jso->_pb) < 0) return NULL;
  168. return jso->_pb->buf;
  169. }
  170. /* json_object_object */
  171. static int json_object_object_to_json_string(struct json_object* jso,
  172. struct printbuf *pb)
  173. {
  174. int i=0;
  175. struct json_object_iter iter;
  176. sprintbuf(pb, "{");
  177. /* CAW: scope operator to make ANSI correctness */
  178. /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
  179. json_object_object_foreachC(jso, iter) {
  180. if(i) sprintbuf(pb, ",");
  181. sprintbuf(pb, " \"");
  182. json_escape_str(pb, iter.key);
  183. sprintbuf(pb, "\": ");
  184. if(iter.val == NULL) sprintbuf(pb, "null");
  185. else iter.val->_to_json_string(iter.val, pb);
  186. i++;
  187. }
  188. return sprintbuf(pb, " }");
  189. }
  190. static void json_object_lh_entry_free(struct lh_entry *ent)
  191. {
  192. free(ent->k);
  193. json_object_put((struct json_object*)ent->v);
  194. }
  195. static void json_object_object_delete(struct json_object* jso)
  196. {
  197. lh_table_free(jso->o.c_object);
  198. json_object_generic_delete(jso);
  199. }
  200. struct json_object* json_object_new_object(void)
  201. {
  202. struct json_object *jso = json_object_new(json_type_object);
  203. if(!jso) return NULL;
  204. jso->_delete = &json_object_object_delete;
  205. jso->_to_json_string = &json_object_object_to_json_string;
  206. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  207. NULL, &json_object_lh_entry_free);
  208. return jso;
  209. }
  210. struct lh_table* json_object_get_object(struct json_object *jso)
  211. {
  212. if(!jso) return NULL;
  213. switch(jso->o_type) {
  214. case json_type_object:
  215. return jso->o.c_object;
  216. default:
  217. return NULL;
  218. }
  219. }
  220. void json_object_object_add(struct json_object* jso, const char *key,
  221. struct json_object *val)
  222. {
  223. lh_table_delete(jso->o.c_object, key);
  224. lh_table_insert(jso->o.c_object, strdup(key), val);
  225. }
  226. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  227. {
  228. return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
  229. }
  230. void json_object_object_del(struct json_object* jso, const char *key)
  231. {
  232. lh_table_delete(jso->o.c_object, key);
  233. }
  234. /* json_object_boolean */
  235. static int json_object_boolean_to_json_string(struct json_object* jso,
  236. struct printbuf *pb)
  237. {
  238. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  239. else return sprintbuf(pb, "false");
  240. }
  241. struct json_object* json_object_new_boolean(boolean b)
  242. {
  243. struct json_object *jso = json_object_new(json_type_boolean);
  244. if(!jso) return NULL;
  245. jso->_to_json_string = &json_object_boolean_to_json_string;
  246. jso->o.c_boolean = b;
  247. return jso;
  248. }
  249. boolean json_object_get_boolean(struct json_object *jso)
  250. {
  251. if(!jso) return FALSE;
  252. switch(jso->o_type) {
  253. case json_type_boolean:
  254. return jso->o.c_boolean;
  255. case json_type_int:
  256. return (jso->o.c_int != 0);
  257. case json_type_double:
  258. return (jso->o.c_double != 0);
  259. case json_type_string:
  260. return (strlen(jso->o.c_string) != 0);
  261. default:
  262. return FALSE;
  263. }
  264. }
  265. /* json_object_int */
  266. static int json_object_int_to_json_string(struct json_object* jso,
  267. struct printbuf *pb)
  268. {
  269. return sprintbuf(pb, "%d", jso->o.c_int);
  270. }
  271. struct json_object* json_object_new_int(int i)
  272. {
  273. struct json_object *jso = json_object_new(json_type_int);
  274. if(!jso) return NULL;
  275. jso->_to_json_string = &json_object_int_to_json_string;
  276. jso->o.c_int = i;
  277. return jso;
  278. }
  279. int json_object_get_int(struct json_object *jso)
  280. {
  281. int cint;
  282. if(!jso) return 0;
  283. switch(jso->o_type) {
  284. case json_type_int:
  285. return jso->o.c_int;
  286. case json_type_double:
  287. return (int)jso->o.c_double;
  288. case json_type_boolean:
  289. return jso->o.c_boolean;
  290. case json_type_string:
  291. if(sscanf(jso->o.c_string, "%d", &cint) == 1) return cint;
  292. default:
  293. return 0;
  294. }
  295. }
  296. /* json_object_double */
  297. static int json_object_double_to_json_string(struct json_object* jso,
  298. struct printbuf *pb)
  299. {
  300. return sprintbuf(pb, "%lf", jso->o.c_double);
  301. }
  302. struct json_object* json_object_new_double(double d)
  303. {
  304. struct json_object *jso = json_object_new(json_type_double);
  305. if(!jso) return NULL;
  306. jso->_to_json_string = &json_object_double_to_json_string;
  307. jso->o.c_double = d;
  308. return jso;
  309. }
  310. double json_object_get_double(struct json_object *jso)
  311. {
  312. double cdouble;
  313. if(!jso) return 0.0;
  314. switch(jso->o_type) {
  315. case json_type_double:
  316. return jso->o.c_double;
  317. case json_type_int:
  318. return jso->o.c_int;
  319. case json_type_boolean:
  320. return jso->o.c_boolean;
  321. case json_type_string:
  322. if(sscanf(jso->o.c_string, "%lf", &cdouble) == 1) return cdouble;
  323. default:
  324. return 0.0;
  325. }
  326. }
  327. /* json_object_string */
  328. static int json_object_string_to_json_string(struct json_object* jso,
  329. struct printbuf *pb)
  330. {
  331. sprintbuf(pb, "\"");
  332. json_escape_str(pb, jso->o.c_string);
  333. sprintbuf(pb, "\"");
  334. return 0;
  335. }
  336. static void json_object_string_delete(struct json_object* jso)
  337. {
  338. free(jso->o.c_string);
  339. json_object_generic_delete(jso);
  340. }
  341. struct json_object* json_object_new_string(const char *s)
  342. {
  343. struct json_object *jso = json_object_new(json_type_string);
  344. if(!jso) return NULL;
  345. jso->_delete = &json_object_string_delete;
  346. jso->_to_json_string = &json_object_string_to_json_string;
  347. jso->o.c_string = strdup(s);
  348. return jso;
  349. }
  350. struct json_object* json_object_new_string_len(const char *s, int len)
  351. {
  352. struct json_object *jso = json_object_new(json_type_string);
  353. if(!jso) return NULL;
  354. jso->_delete = &json_object_string_delete;
  355. jso->_to_json_string = &json_object_string_to_json_string;
  356. jso->o.c_string = strndup(s, len);
  357. return jso;
  358. }
  359. const char* json_object_get_string(struct json_object *jso)
  360. {
  361. if(!jso) return NULL;
  362. switch(jso->o_type) {
  363. case json_type_string:
  364. return jso->o.c_string;
  365. default:
  366. return json_object_to_json_string(jso);
  367. }
  368. }
  369. /* json_object_array */
  370. static int json_object_array_to_json_string(struct json_object* jso,
  371. struct printbuf *pb)
  372. {
  373. int i;
  374. sprintbuf(pb, "[");
  375. for(i=0; i < json_object_array_length(jso); i++) {
  376. struct json_object *val;
  377. if(i) { sprintbuf(pb, ", "); }
  378. else { sprintbuf(pb, " "); }
  379. val = json_object_array_get_idx(jso, i);
  380. if(val == NULL) { sprintbuf(pb, "null"); }
  381. else { val->_to_json_string(val, pb); }
  382. }
  383. return sprintbuf(pb, " ]");
  384. }
  385. static void json_object_array_entry_free(void *data)
  386. {
  387. json_object_put((struct json_object*)data);
  388. }
  389. static void json_object_array_delete(struct json_object* jso)
  390. {
  391. array_list_free(jso->o.c_array);
  392. json_object_generic_delete(jso);
  393. }
  394. struct json_object* json_object_new_array(void)
  395. {
  396. struct json_object *jso = json_object_new(json_type_array);
  397. if(!jso) return NULL;
  398. jso->_delete = &json_object_array_delete;
  399. jso->_to_json_string = &json_object_array_to_json_string;
  400. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  401. return jso;
  402. }
  403. struct array_list* json_object_get_array(struct json_object *jso)
  404. {
  405. if(!jso) return NULL;
  406. switch(jso->o_type) {
  407. case json_type_array:
  408. return jso->o.c_array;
  409. default:
  410. return NULL;
  411. }
  412. }
  413. int json_object_array_length(struct json_object *jso)
  414. {
  415. return array_list_length(jso->o.c_array);
  416. }
  417. int json_object_array_add(struct json_object *jso,struct json_object *val)
  418. {
  419. return array_list_add(jso->o.c_array, val);
  420. }
  421. int json_object_array_put_idx(struct json_object *jso, int idx,
  422. struct json_object *val)
  423. {
  424. return array_list_put_idx(jso->o.c_array, idx, val);
  425. }
  426. struct json_object* json_object_array_get_idx(struct json_object *jso,
  427. int idx)
  428. {
  429. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  430. }