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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. 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. return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
  218. }
  219. void json_object_object_del(struct json_object* jso, const char *key)
  220. {
  221. lh_table_delete(jso->o.c_object, key);
  222. }
  223. /* json_object_boolean */
  224. static int json_object_boolean_to_json_string(struct json_object* jso,
  225. struct printbuf *pb)
  226. {
  227. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  228. else return sprintbuf(pb, "false");
  229. }
  230. struct json_object* json_object_new_boolean(boolean b)
  231. {
  232. struct json_object *jso = json_object_new(json_type_boolean);
  233. if(!jso) return NULL;
  234. jso->_to_json_string = &json_object_boolean_to_json_string;
  235. jso->o.c_boolean = b;
  236. return jso;
  237. }
  238. boolean json_object_get_boolean(struct json_object *jso)
  239. {
  240. if(!jso) return FALSE;
  241. switch(jso->o_type) {
  242. case json_type_boolean:
  243. return jso->o.c_boolean;
  244. case json_type_int:
  245. return (jso->o.c_int64 != 0);
  246. case json_type_double:
  247. return (jso->o.c_double != 0);
  248. case json_type_string:
  249. return (jso->o.c_string.len != 0);
  250. default:
  251. return FALSE;
  252. }
  253. }
  254. /* json_object_int */
  255. static int json_object_int_to_json_string(struct json_object* jso,
  256. struct printbuf *pb)
  257. {
  258. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  259. }
  260. struct json_object* json_object_new_int(int32_t i)
  261. {
  262. struct json_object *jso = json_object_new(json_type_int);
  263. if(!jso) return NULL;
  264. jso->_to_json_string = &json_object_int_to_json_string;
  265. jso->o.c_int64 = i;
  266. return jso;
  267. }
  268. int32_t json_object_get_int(struct json_object *jso)
  269. {
  270. if(!jso) return 0;
  271. enum json_type o_type = jso->o_type;
  272. int64_t cint64 = jso->o.c_int64;
  273. if (o_type == json_type_string)
  274. {
  275. /*
  276. * Parse strings into 64-bit numbers, then use the
  277. * 64-to-32-bit number handling below.
  278. */
  279. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  280. return 0; /* whoops, it didn't work. */
  281. o_type = json_type_int;
  282. }
  283. switch(o_type) {
  284. case json_type_int:
  285. /* Make sure we return the correct values for out of range numbers. */
  286. if (cint64 <= INT32_MIN)
  287. return INT32_MIN;
  288. else if (cint64 >= INT32_MAX)
  289. return INT32_MAX;
  290. else
  291. return (int32_t)cint64;
  292. case json_type_double:
  293. return (int32_t)jso->o.c_double;
  294. case json_type_boolean:
  295. return jso->o.c_boolean;
  296. default:
  297. return 0;
  298. }
  299. }
  300. struct json_object* json_object_new_int64(int64_t i)
  301. {
  302. struct json_object *jso = json_object_new(json_type_int);
  303. if(!jso) return NULL;
  304. jso->_to_json_string = &json_object_int_to_json_string;
  305. jso->o.c_int64 = i;
  306. return jso;
  307. }
  308. int64_t json_object_get_int64(struct json_object *jso)
  309. {
  310. int64_t cint;
  311. if(!jso) return 0;
  312. switch(jso->o_type) {
  313. case json_type_int:
  314. return jso->o.c_int64;
  315. case json_type_double:
  316. return (int64_t)jso->o.c_double;
  317. case json_type_boolean:
  318. return jso->o.c_boolean;
  319. case json_type_string:
  320. if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
  321. default:
  322. return 0;
  323. }
  324. }
  325. /* json_object_double */
  326. static int json_object_double_to_json_string(struct json_object* jso,
  327. struct printbuf *pb)
  328. {
  329. return sprintbuf(pb, "%lf", jso->o.c_double);
  330. }
  331. struct json_object* json_object_new_double(double d)
  332. {
  333. struct json_object *jso = json_object_new(json_type_double);
  334. if(!jso) return NULL;
  335. jso->_to_json_string = &json_object_double_to_json_string;
  336. jso->o.c_double = d;
  337. return jso;
  338. }
  339. double json_object_get_double(struct json_object *jso)
  340. {
  341. double cdouble;
  342. if(!jso) return 0.0;
  343. switch(jso->o_type) {
  344. case json_type_double:
  345. return jso->o.c_double;
  346. case json_type_int:
  347. return jso->o.c_int64;
  348. case json_type_boolean:
  349. return jso->o.c_boolean;
  350. case json_type_string:
  351. if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
  352. default:
  353. return 0.0;
  354. }
  355. }
  356. /* json_object_string */
  357. static int json_object_string_to_json_string(struct json_object* jso,
  358. struct printbuf *pb)
  359. {
  360. sprintbuf(pb, "\"");
  361. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  362. sprintbuf(pb, "\"");
  363. return 0;
  364. }
  365. static void json_object_string_delete(struct json_object* jso)
  366. {
  367. free(jso->o.c_string.str);
  368. json_object_generic_delete(jso);
  369. }
  370. struct json_object* json_object_new_string(const char *s)
  371. {
  372. struct json_object *jso = json_object_new(json_type_string);
  373. if(!jso) return NULL;
  374. jso->_delete = &json_object_string_delete;
  375. jso->_to_json_string = &json_object_string_to_json_string;
  376. jso->o.c_string.str = strdup(s);
  377. jso->o.c_string.len = strlen(s);
  378. return jso;
  379. }
  380. struct json_object* json_object_new_string_len(const char *s, int len)
  381. {
  382. struct json_object *jso = json_object_new(json_type_string);
  383. if(!jso) return NULL;
  384. jso->_delete = &json_object_string_delete;
  385. jso->_to_json_string = &json_object_string_to_json_string;
  386. jso->o.c_string.str = malloc(len);
  387. memcpy(jso->o.c_string.str, (void *)s, len);
  388. jso->o.c_string.len = len;
  389. return jso;
  390. }
  391. const char* json_object_get_string(struct json_object *jso)
  392. {
  393. if(!jso) return NULL;
  394. switch(jso->o_type) {
  395. case json_type_string:
  396. return jso->o.c_string.str;
  397. default:
  398. return json_object_to_json_string(jso);
  399. }
  400. }
  401. int json_object_get_string_len(struct json_object *jso) {
  402. if(!jso) return 0;
  403. switch(jso->o_type) {
  404. case json_type_string:
  405. return jso->o.c_string.len;
  406. default:
  407. return 0;
  408. }
  409. }
  410. /* json_object_array */
  411. static int json_object_array_to_json_string(struct json_object* jso,
  412. struct printbuf *pb)
  413. {
  414. int i;
  415. sprintbuf(pb, "[");
  416. for(i=0; i < json_object_array_length(jso); i++) {
  417. struct json_object *val;
  418. if(i) { sprintbuf(pb, ", "); }
  419. else { sprintbuf(pb, " "); }
  420. val = json_object_array_get_idx(jso, i);
  421. if(val == NULL) { sprintbuf(pb, "null"); }
  422. else { val->_to_json_string(val, pb); }
  423. }
  424. return sprintbuf(pb, " ]");
  425. }
  426. static void json_object_array_entry_free(void *data)
  427. {
  428. json_object_put((struct json_object*)data);
  429. }
  430. static void json_object_array_delete(struct json_object* jso)
  431. {
  432. array_list_free(jso->o.c_array);
  433. json_object_generic_delete(jso);
  434. }
  435. struct json_object* json_object_new_array(void)
  436. {
  437. struct json_object *jso = json_object_new(json_type_array);
  438. if(!jso) return NULL;
  439. jso->_delete = &json_object_array_delete;
  440. jso->_to_json_string = &json_object_array_to_json_string;
  441. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  442. return jso;
  443. }
  444. struct array_list* json_object_get_array(struct json_object *jso)
  445. {
  446. if(!jso) return NULL;
  447. switch(jso->o_type) {
  448. case json_type_array:
  449. return jso->o.c_array;
  450. default:
  451. return NULL;
  452. }
  453. }
  454. int json_object_array_length(struct json_object *jso)
  455. {
  456. return array_list_length(jso->o.c_array);
  457. }
  458. int json_object_array_add(struct json_object *jso,struct json_object *val)
  459. {
  460. return array_list_add(jso->o.c_array, val);
  461. }
  462. int json_object_array_put_idx(struct json_object *jso, int idx,
  463. struct json_object *val)
  464. {
  465. return array_list_put_idx(jso->o.c_array, idx, val);
  466. }
  467. struct json_object* json_object_array_get_idx(struct json_object *jso,
  468. int idx)
  469. {
  470. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  471. }