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

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