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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. lh_table_delete(jso->o.c_object, key);
  262. lh_table_insert(jso->o.c_object, strdup(key), val);
  263. }
  264. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  265. {
  266. struct json_object *result;
  267. json_object_object_get_ex(jso, key, &result);
  268. return result;
  269. }
  270. json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
  271. {
  272. if (NULL == jso) return FALSE;
  273. switch(jso->o_type) {
  274. case json_type_object:
  275. return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
  276. default:
  277. if (value != NULL) {
  278. *value = NULL;
  279. }
  280. return FALSE;
  281. }
  282. }
  283. void json_object_object_del(struct json_object* jso, const char *key)
  284. {
  285. lh_table_delete(jso->o.c_object, key);
  286. }
  287. /* json_object_boolean */
  288. static int json_object_boolean_to_json_string(struct json_object* jso,
  289. struct printbuf *pb,
  290. int level,
  291. int flags)
  292. {
  293. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  294. else return sprintbuf(pb, "false");
  295. }
  296. struct json_object* json_object_new_boolean(json_bool b)
  297. {
  298. struct json_object *jso = json_object_new(json_type_boolean);
  299. if(!jso) return NULL;
  300. jso->_to_json_string = &json_object_boolean_to_json_string;
  301. jso->o.c_boolean = b;
  302. return jso;
  303. }
  304. json_bool json_object_get_boolean(struct json_object *jso)
  305. {
  306. if(!jso) return FALSE;
  307. switch(jso->o_type) {
  308. case json_type_boolean:
  309. return jso->o.c_boolean;
  310. case json_type_int:
  311. return (jso->o.c_int64 != 0);
  312. case json_type_double:
  313. return (jso->o.c_double != 0);
  314. case json_type_string:
  315. return (jso->o.c_string.len != 0);
  316. default:
  317. return FALSE;
  318. }
  319. }
  320. /* json_object_int */
  321. static int json_object_int_to_json_string(struct json_object* jso,
  322. struct printbuf *pb,
  323. int level,
  324. int flags)
  325. {
  326. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  327. }
  328. struct json_object* json_object_new_int(int32_t i)
  329. {
  330. struct json_object *jso = json_object_new(json_type_int);
  331. if(!jso) return NULL;
  332. jso->_to_json_string = &json_object_int_to_json_string;
  333. jso->o.c_int64 = i;
  334. return jso;
  335. }
  336. int32_t json_object_get_int(struct json_object *jso)
  337. {
  338. int64_t cint64;
  339. enum json_type o_type;
  340. if(!jso) return 0;
  341. o_type = jso->o_type;
  342. cint64 = jso->o.c_int64;
  343. if (o_type == json_type_string)
  344. {
  345. /*
  346. * Parse strings into 64-bit numbers, then use the
  347. * 64-to-32-bit number handling below.
  348. */
  349. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  350. return 0; /* whoops, it didn't work. */
  351. o_type = json_type_int;
  352. }
  353. switch(o_type) {
  354. case json_type_int:
  355. /* Make sure we return the correct values for out of range numbers. */
  356. if (cint64 <= INT32_MIN)
  357. return INT32_MIN;
  358. else if (cint64 >= INT32_MAX)
  359. return INT32_MAX;
  360. else
  361. return (int32_t)cint64;
  362. case json_type_double:
  363. return (int32_t)jso->o.c_double;
  364. case json_type_boolean:
  365. return jso->o.c_boolean;
  366. default:
  367. return 0;
  368. }
  369. }
  370. struct json_object* json_object_new_int64(int64_t i)
  371. {
  372. struct json_object *jso = json_object_new(json_type_int);
  373. if(!jso) return NULL;
  374. jso->_to_json_string = &json_object_int_to_json_string;
  375. jso->o.c_int64 = i;
  376. return jso;
  377. }
  378. int64_t json_object_get_int64(struct json_object *jso)
  379. {
  380. int64_t cint;
  381. if(!jso) return 0;
  382. switch(jso->o_type) {
  383. case json_type_int:
  384. return jso->o.c_int64;
  385. case json_type_double:
  386. return (int64_t)jso->o.c_double;
  387. case json_type_boolean:
  388. return jso->o.c_boolean;
  389. case json_type_string:
  390. if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
  391. default:
  392. return 0;
  393. }
  394. }
  395. /* json_object_double */
  396. static int json_object_double_to_json_string(struct json_object* jso,
  397. struct printbuf *pb,
  398. int level,
  399. int flags)
  400. {
  401. return sprintbuf(pb, "%f", jso->o.c_double);
  402. }
  403. struct json_object* json_object_new_double(double d)
  404. {
  405. struct json_object *jso = json_object_new(json_type_double);
  406. if(!jso) return NULL;
  407. jso->_to_json_string = &json_object_double_to_json_string;
  408. jso->o.c_double = d;
  409. return jso;
  410. }
  411. double json_object_get_double(struct json_object *jso)
  412. {
  413. double cdouble;
  414. if(!jso) return 0.0;
  415. switch(jso->o_type) {
  416. case json_type_double:
  417. return jso->o.c_double;
  418. case json_type_int:
  419. return jso->o.c_int64;
  420. case json_type_boolean:
  421. return jso->o.c_boolean;
  422. case json_type_string:
  423. if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
  424. default:
  425. return 0.0;
  426. }
  427. }
  428. /* json_object_string */
  429. static int json_object_string_to_json_string(struct json_object* jso,
  430. struct printbuf *pb,
  431. int level,
  432. int flags)
  433. {
  434. sprintbuf(pb, "\"");
  435. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  436. sprintbuf(pb, "\"");
  437. return 0;
  438. }
  439. static void json_object_string_delete(struct json_object* jso)
  440. {
  441. free(jso->o.c_string.str);
  442. json_object_generic_delete(jso);
  443. }
  444. struct json_object* json_object_new_string(const char *s)
  445. {
  446. struct json_object *jso = json_object_new(json_type_string);
  447. if(!jso) return NULL;
  448. jso->_delete = &json_object_string_delete;
  449. jso->_to_json_string = &json_object_string_to_json_string;
  450. jso->o.c_string.str = strdup(s);
  451. jso->o.c_string.len = strlen(s);
  452. return jso;
  453. }
  454. struct json_object* json_object_new_string_len(const char *s, int len)
  455. {
  456. struct json_object *jso = json_object_new(json_type_string);
  457. if(!jso) return NULL;
  458. jso->_delete = &json_object_string_delete;
  459. jso->_to_json_string = &json_object_string_to_json_string;
  460. jso->o.c_string.str = (char*)malloc(len);
  461. memcpy(jso->o.c_string.str, (void *)s, len);
  462. jso->o.c_string.len = len;
  463. return jso;
  464. }
  465. const char* json_object_get_string(struct json_object *jso)
  466. {
  467. if(!jso) return NULL;
  468. switch(jso->o_type) {
  469. case json_type_string:
  470. return jso->o.c_string.str;
  471. default:
  472. return json_object_to_json_string(jso);
  473. }
  474. }
  475. int json_object_get_string_len(struct json_object *jso) {
  476. if(!jso) return 0;
  477. switch(jso->o_type) {
  478. case json_type_string:
  479. return jso->o.c_string.len;
  480. default:
  481. return 0;
  482. }
  483. }
  484. /* json_object_array */
  485. static int json_object_array_to_json_string(struct json_object* jso,
  486. struct printbuf *pb,
  487. int level,
  488. int flags)
  489. {
  490. int had_children = 0;
  491. int ii;
  492. sprintbuf(pb, "[");
  493. if (flags & JSON_C_TO_STRING_PRETTY)
  494. sprintbuf(pb, "\n");
  495. for(ii=0; ii < json_object_array_length(jso); ii++)
  496. {
  497. struct json_object *val;
  498. if (had_children)
  499. {
  500. sprintbuf(pb, ",");
  501. if (flags & JSON_C_TO_STRING_PRETTY)
  502. sprintbuf(pb, "\n");
  503. }
  504. had_children = 1;
  505. if (flags & JSON_C_TO_STRING_SPACED)
  506. sprintbuf(pb, " ");
  507. indent(pb, level + 1, flags);
  508. val = json_object_array_get_idx(jso, ii);
  509. if(val == NULL)
  510. sprintbuf(pb, "null");
  511. else
  512. val->_to_json_string(val, pb, level+1, flags);
  513. }
  514. if (flags & JSON_C_TO_STRING_PRETTY)
  515. {
  516. if (had_children)
  517. sprintbuf(pb, "\n");
  518. indent(pb,level,flags);
  519. }
  520. if (flags & JSON_C_TO_STRING_SPACED)
  521. return sprintbuf(pb, " ]");
  522. else
  523. return sprintbuf(pb, "]");
  524. }
  525. static void json_object_array_entry_free(void *data)
  526. {
  527. json_object_put((struct json_object*)data);
  528. }
  529. static void json_object_array_delete(struct json_object* jso)
  530. {
  531. array_list_free(jso->o.c_array);
  532. json_object_generic_delete(jso);
  533. }
  534. struct json_object* json_object_new_array(void)
  535. {
  536. struct json_object *jso = json_object_new(json_type_array);
  537. if(!jso) return NULL;
  538. jso->_delete = &json_object_array_delete;
  539. jso->_to_json_string = &json_object_array_to_json_string;
  540. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  541. return jso;
  542. }
  543. struct array_list* json_object_get_array(struct json_object *jso)
  544. {
  545. if(!jso) return NULL;
  546. switch(jso->o_type) {
  547. case json_type_array:
  548. return jso->o.c_array;
  549. default:
  550. return NULL;
  551. }
  552. }
  553. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  554. {
  555. array_list_sort(jso->o.c_array, sort_fn);
  556. }
  557. int json_object_array_length(struct json_object *jso)
  558. {
  559. return array_list_length(jso->o.c_array);
  560. }
  561. int json_object_array_add(struct json_object *jso,struct json_object *val)
  562. {
  563. return array_list_add(jso->o.c_array, val);
  564. }
  565. int json_object_array_put_idx(struct json_object *jso, int idx,
  566. struct json_object *val)
  567. {
  568. return array_list_put_idx(jso->o.c_array, idx, val);
  569. }
  570. struct json_object* json_object_array_get_idx(struct json_object *jso,
  571. int idx)
  572. {
  573. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  574. }