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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. if (!jso)
  142. return (type == json_type_null);
  143. return (jso->o_type == type);
  144. }
  145. enum json_type json_object_get_type(struct json_object *jso)
  146. {
  147. if (!jso)
  148. return json_type_null;
  149. return jso->o_type;
  150. }
  151. /* json_object_to_json_string */
  152. const char* json_object_to_json_string(struct json_object *jso)
  153. {
  154. if(!jso) return "null";
  155. if(!jso->_pb) {
  156. if(!(jso->_pb = printbuf_new())) return NULL;
  157. } else {
  158. printbuf_reset(jso->_pb);
  159. }
  160. if(jso->_to_json_string(jso, jso->_pb) < 0) return NULL;
  161. return jso->_pb->buf;
  162. }
  163. /* json_object_object */
  164. static int json_object_object_to_json_string(struct json_object* jso,
  165. struct printbuf *pb)
  166. {
  167. int i=0;
  168. struct json_object_iter iter;
  169. sprintbuf(pb, "{");
  170. /* CAW: scope operator to make ANSI correctness */
  171. /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
  172. json_object_object_foreachC(jso, iter) {
  173. if(i) sprintbuf(pb, ",");
  174. sprintbuf(pb, " \"");
  175. json_escape_str(pb, iter.key, strlen(iter.key));
  176. sprintbuf(pb, "\": ");
  177. if(iter.val == NULL) sprintbuf(pb, "null");
  178. else iter.val->_to_json_string(iter.val, pb);
  179. i++;
  180. }
  181. return sprintbuf(pb, " }");
  182. }
  183. static void json_object_lh_entry_free(struct lh_entry *ent)
  184. {
  185. free(ent->k);
  186. json_object_put((struct json_object*)ent->v);
  187. }
  188. static void json_object_object_delete(struct json_object* jso)
  189. {
  190. lh_table_free(jso->o.c_object);
  191. json_object_generic_delete(jso);
  192. }
  193. struct json_object* json_object_new_object(void)
  194. {
  195. struct json_object *jso = json_object_new(json_type_object);
  196. if(!jso) return NULL;
  197. jso->_delete = &json_object_object_delete;
  198. jso->_to_json_string = &json_object_object_to_json_string;
  199. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  200. NULL, &json_object_lh_entry_free);
  201. return jso;
  202. }
  203. struct lh_table* json_object_get_object(struct json_object *jso)
  204. {
  205. if(!jso) return NULL;
  206. switch(jso->o_type) {
  207. case json_type_object:
  208. return jso->o.c_object;
  209. default:
  210. return NULL;
  211. }
  212. }
  213. void json_object_object_add(struct json_object* jso, const char *key,
  214. struct json_object *val)
  215. {
  216. lh_table_delete(jso->o.c_object, key);
  217. lh_table_insert(jso->o.c_object, strdup(key), val);
  218. }
  219. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  220. {
  221. if(!jso) return NULL;
  222. return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
  223. }
  224. void json_object_object_del(struct json_object* jso, const char *key)
  225. {
  226. lh_table_delete(jso->o.c_object, key);
  227. }
  228. /* json_object_boolean */
  229. static int json_object_boolean_to_json_string(struct json_object* jso,
  230. struct printbuf *pb)
  231. {
  232. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  233. else return sprintbuf(pb, "false");
  234. }
  235. struct json_object* json_object_new_boolean(json_bool b)
  236. {
  237. struct json_object *jso = json_object_new(json_type_boolean);
  238. if(!jso) return NULL;
  239. jso->_to_json_string = &json_object_boolean_to_json_string;
  240. jso->o.c_boolean = b;
  241. return jso;
  242. }
  243. json_bool json_object_get_boolean(struct json_object *jso)
  244. {
  245. if(!jso) return FALSE;
  246. switch(jso->o_type) {
  247. case json_type_boolean:
  248. return jso->o.c_boolean;
  249. case json_type_int:
  250. return (jso->o.c_int64 != 0);
  251. case json_type_double:
  252. return (jso->o.c_double != 0);
  253. case json_type_string:
  254. return (jso->o.c_string.len != 0);
  255. default:
  256. return FALSE;
  257. }
  258. }
  259. /* json_object_int */
  260. static int json_object_int_to_json_string(struct json_object* jso,
  261. struct printbuf *pb)
  262. {
  263. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  264. }
  265. struct json_object* json_object_new_int(int32_t i)
  266. {
  267. struct json_object *jso = json_object_new(json_type_int);
  268. if(!jso) return NULL;
  269. jso->_to_json_string = &json_object_int_to_json_string;
  270. jso->o.c_int64 = i;
  271. return jso;
  272. }
  273. int32_t json_object_get_int(struct json_object *jso)
  274. {
  275. int64_t cint64;
  276. enum json_type o_type;
  277. if(!jso) return 0;
  278. o_type = jso->o_type;
  279. cint64 = jso->o.c_int64;
  280. if (o_type == json_type_string)
  281. {
  282. /*
  283. * Parse strings into 64-bit numbers, then use the
  284. * 64-to-32-bit number handling below.
  285. */
  286. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  287. return 0; /* whoops, it didn't work. */
  288. o_type = json_type_int;
  289. }
  290. switch(o_type) {
  291. case json_type_int:
  292. /* Make sure we return the correct values for out of range numbers. */
  293. if (cint64 <= INT32_MIN)
  294. return INT32_MIN;
  295. else if (cint64 >= INT32_MAX)
  296. return INT32_MAX;
  297. else
  298. return (int32_t)cint64;
  299. case json_type_double:
  300. return (int32_t)jso->o.c_double;
  301. case json_type_boolean:
  302. return jso->o.c_boolean;
  303. default:
  304. return 0;
  305. }
  306. }
  307. struct json_object* json_object_new_int64(int64_t i)
  308. {
  309. struct json_object *jso = json_object_new(json_type_int);
  310. if(!jso) return NULL;
  311. jso->_to_json_string = &json_object_int_to_json_string;
  312. jso->o.c_int64 = i;
  313. return jso;
  314. }
  315. int64_t json_object_get_int64(struct json_object *jso)
  316. {
  317. int64_t cint;
  318. if(!jso) return 0;
  319. switch(jso->o_type) {
  320. case json_type_int:
  321. return jso->o.c_int64;
  322. case json_type_double:
  323. return (int64_t)jso->o.c_double;
  324. case json_type_boolean:
  325. return jso->o.c_boolean;
  326. case json_type_string:
  327. if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
  328. default:
  329. return 0;
  330. }
  331. }
  332. /* json_object_double */
  333. static int json_object_double_to_json_string(struct json_object* jso,
  334. struct printbuf *pb)
  335. {
  336. return sprintbuf(pb, "%lf", jso->o.c_double);
  337. }
  338. struct json_object* json_object_new_double(double d)
  339. {
  340. struct json_object *jso = json_object_new(json_type_double);
  341. if(!jso) return NULL;
  342. jso->_to_json_string = &json_object_double_to_json_string;
  343. jso->o.c_double = d;
  344. return jso;
  345. }
  346. double json_object_get_double(struct json_object *jso)
  347. {
  348. double cdouble;
  349. if(!jso) return 0.0;
  350. switch(jso->o_type) {
  351. case json_type_double:
  352. return jso->o.c_double;
  353. case json_type_int:
  354. return jso->o.c_int64;
  355. case json_type_boolean:
  356. return jso->o.c_boolean;
  357. case json_type_string:
  358. if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
  359. default:
  360. return 0.0;
  361. }
  362. }
  363. /* json_object_string */
  364. static int json_object_string_to_json_string(struct json_object* jso,
  365. struct printbuf *pb)
  366. {
  367. sprintbuf(pb, "\"");
  368. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  369. sprintbuf(pb, "\"");
  370. return 0;
  371. }
  372. static void json_object_string_delete(struct json_object* jso)
  373. {
  374. free(jso->o.c_string.str);
  375. json_object_generic_delete(jso);
  376. }
  377. struct json_object* json_object_new_string(const char *s)
  378. {
  379. struct json_object *jso = json_object_new(json_type_string);
  380. if(!jso) return NULL;
  381. jso->_delete = &json_object_string_delete;
  382. jso->_to_json_string = &json_object_string_to_json_string;
  383. jso->o.c_string.str = strdup(s);
  384. jso->o.c_string.len = strlen(s);
  385. return jso;
  386. }
  387. struct json_object* json_object_new_string_len(const char *s, int len)
  388. {
  389. struct json_object *jso = json_object_new(json_type_string);
  390. if(!jso) return NULL;
  391. jso->_delete = &json_object_string_delete;
  392. jso->_to_json_string = &json_object_string_to_json_string;
  393. jso->o.c_string.str = malloc(len);
  394. memcpy(jso->o.c_string.str, (void *)s, len);
  395. jso->o.c_string.len = len;
  396. return jso;
  397. }
  398. const char* json_object_get_string(struct json_object *jso)
  399. {
  400. if(!jso) return NULL;
  401. switch(jso->o_type) {
  402. case json_type_string:
  403. return jso->o.c_string.str;
  404. default:
  405. return json_object_to_json_string(jso);
  406. }
  407. }
  408. int json_object_get_string_len(struct json_object *jso) {
  409. if(!jso) return 0;
  410. switch(jso->o_type) {
  411. case json_type_string:
  412. return jso->o.c_string.len;
  413. default:
  414. return 0;
  415. }
  416. }
  417. /* json_object_array */
  418. static int json_object_array_to_json_string(struct json_object* jso,
  419. struct printbuf *pb)
  420. {
  421. int i;
  422. sprintbuf(pb, "[");
  423. for(i=0; i < json_object_array_length(jso); i++) {
  424. struct json_object *val;
  425. if(i) { sprintbuf(pb, ", "); }
  426. else { sprintbuf(pb, " "); }
  427. val = json_object_array_get_idx(jso, i);
  428. if(val == NULL) { sprintbuf(pb, "null"); }
  429. else { val->_to_json_string(val, pb); }
  430. }
  431. return sprintbuf(pb, " ]");
  432. }
  433. static void json_object_array_entry_free(void *data)
  434. {
  435. json_object_put((struct json_object*)data);
  436. }
  437. static void json_object_array_delete(struct json_object* jso)
  438. {
  439. array_list_free(jso->o.c_array);
  440. json_object_generic_delete(jso);
  441. }
  442. struct json_object* json_object_new_array(void)
  443. {
  444. struct json_object *jso = json_object_new(json_type_array);
  445. if(!jso) return NULL;
  446. jso->_delete = &json_object_array_delete;
  447. jso->_to_json_string = &json_object_array_to_json_string;
  448. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  449. return jso;
  450. }
  451. struct array_list* json_object_get_array(struct json_object *jso)
  452. {
  453. if(!jso) return NULL;
  454. switch(jso->o_type) {
  455. case json_type_array:
  456. return jso->o.c_array;
  457. default:
  458. return NULL;
  459. }
  460. }
  461. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  462. {
  463. array_list_sort(jso->o.c_array, sort_fn);
  464. }
  465. int json_object_array_length(struct json_object *jso)
  466. {
  467. return array_list_length(jso->o.c_array);
  468. }
  469. int json_object_array_add(struct json_object *jso,struct json_object *val)
  470. {
  471. return array_list_add(jso->o.c_array, val);
  472. }
  473. int json_object_array_put_idx(struct json_object *jso, int idx,
  474. struct json_object *val)
  475. {
  476. return array_list_put_idx(jso->o.c_array, idx, val);
  477. }
  478. struct json_object* json_object_array_get_idx(struct json_object *jso,
  479. int idx)
  480. {
  481. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  482. }