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 14 kB

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