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

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