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

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