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

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