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

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