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

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