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

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