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

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