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 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_object.h"
  21. #include "json_object_private.h"
  22. #include "json_tokener.h"
  23. #if !HAVE_STRNDUP
  24. char* strndup(const char* str, size_t n);
  25. #endif /* !HAVE_STRNDUP */
  26. #define REFCOUNT_DEBUG 1
  27. const char *json_number_chars = "0123456789.+-eE";
  28. const char *json_hex_chars = "0123456789abcdef";
  29. #ifdef REFCOUNT_DEBUG
  30. static const char* json_type_name[] = {
  31. "null",
  32. "boolean",
  33. "double",
  34. "int",
  35. "object",
  36. "array",
  37. "string",
  38. };
  39. #endif /* REFCOUNT_DEBUG */
  40. static void json_object_generic_delete(struct json_object* this);
  41. static struct json_object* json_object_new(enum json_type o_type);
  42. /* ref count debugging */
  43. #ifdef REFCOUNT_DEBUG
  44. static struct lh_table *json_object_table;
  45. static void json_object_init(void) __attribute__ ((constructor));
  46. static void json_object_init(void) {
  47. MC_DEBUG("json_object_init: creating object table\n");
  48. json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
  49. }
  50. static void json_object_fini(void) __attribute__ ((destructor));
  51. static void json_object_fini(void) {
  52. struct lh_entry *ent;
  53. if(MC_GET_DEBUG()) {
  54. if (json_object_table->count) {
  55. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  56. json_object_table->count);
  57. lh_foreach(json_object_table, ent) {
  58. struct json_object* obj = (struct json_object*)ent->v;
  59. MC_DEBUG("\t%s:%p\n", json_type_name[obj->o_type], obj);
  60. }
  61. }
  62. }
  63. MC_DEBUG("json_object_fini: freeing object table\n");
  64. lh_table_free(json_object_table);
  65. }
  66. #endif /* REFCOUNT_DEBUG */
  67. /* string escaping */
  68. static int json_escape_str(struct printbuf *pb, char *str)
  69. {
  70. int pos = 0, start_offset = 0;
  71. unsigned char c;
  72. do {
  73. c = str[pos];
  74. switch(c) {
  75. case '\0':
  76. break;
  77. case '\b':
  78. case '\n':
  79. case '\r':
  80. case '\t':
  81. case '"':
  82. case '\\':
  83. case '/':
  84. if(pos - start_offset > 0)
  85. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  86. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  87. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  88. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  89. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  90. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  91. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  92. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  93. start_offset = ++pos;
  94. break;
  95. default:
  96. if(c < ' ') {
  97. if(pos - start_offset > 0)
  98. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  99. sprintbuf(pb, "\\u00%c%c",
  100. json_hex_chars[c >> 4],
  101. json_hex_chars[c & 0xf]);
  102. start_offset = ++pos;
  103. } else pos++;
  104. }
  105. } while(c);
  106. if(pos - start_offset > 0)
  107. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  108. return 0;
  109. }
  110. /* reference counting */
  111. extern struct json_object* json_object_get(struct json_object *this)
  112. {
  113. if(this) {
  114. this->_ref_count++;
  115. }
  116. return this;
  117. }
  118. extern void json_object_put(struct json_object *this)
  119. {
  120. if(this) {
  121. this->_ref_count--;
  122. if(!this->_ref_count) this->_delete(this);
  123. }
  124. }
  125. /* generic object construction and destruction parts */
  126. static void json_object_generic_delete(struct json_object* this)
  127. {
  128. #ifdef REFCOUNT_DEBUG
  129. MC_DEBUG("json_object_delete_%s: %p\n",
  130. json_type_name[this->o_type], this);
  131. lh_table_delete(json_object_table, this);
  132. #endif /* REFCOUNT_DEBUG */
  133. printbuf_free(this->_pb);
  134. free(this);
  135. }
  136. static struct json_object* json_object_new(enum json_type o_type)
  137. {
  138. struct json_object *this = calloc(sizeof(struct json_object), 1);
  139. if(!this) return NULL;
  140. this->o_type = o_type;
  141. this->_ref_count = 1;
  142. this->_delete = &json_object_generic_delete;
  143. #ifdef REFCOUNT_DEBUG
  144. lh_table_insert(json_object_table, this, this);
  145. MC_DEBUG("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
  146. #endif /* REFCOUNT_DEBUG */
  147. return this;
  148. }
  149. /* type checking functions */
  150. int json_object_is_type(struct json_object *this, enum json_type type)
  151. {
  152. return (this->o_type == type);
  153. }
  154. enum json_type json_object_get_type(struct json_object *this)
  155. {
  156. return this->o_type;
  157. }
  158. /* json_object_to_json_string */
  159. const char* json_object_to_json_string(struct json_object *this)
  160. {
  161. if(!this) return "null";
  162. if(!this->_pb) {
  163. if(!(this->_pb = printbuf_new())) return NULL;
  164. } else {
  165. printbuf_reset(this->_pb);
  166. }
  167. if(this->_to_json_string(this, this->_pb) < 0) return NULL;
  168. return this->_pb->buf;
  169. }
  170. /* json_object_object */
  171. static int json_object_object_to_json_string(struct json_object* this,
  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(this, iter) {
  180. if(i) sprintbuf(pb, ",");
  181. sprintbuf(pb, " \"");
  182. json_escape_str(pb, 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* this)
  196. {
  197. lh_table_free(this->o.c_object);
  198. json_object_generic_delete(this);
  199. }
  200. struct json_object* json_object_new_object()
  201. {
  202. struct json_object *this = json_object_new(json_type_object);
  203. if(!this) return NULL;
  204. this->_delete = &json_object_object_delete;
  205. this->_to_json_string = &json_object_object_to_json_string;
  206. this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
  207. NULL, &json_object_lh_entry_free);
  208. return this;
  209. }
  210. struct lh_table* json_object_get_object(struct json_object *this)
  211. {
  212. if(!this) return NULL;
  213. switch(this->o_type) {
  214. case json_type_object:
  215. return this->o.c_object;
  216. default:
  217. return NULL;
  218. }
  219. }
  220. void json_object_object_add(struct json_object* this, const char *key,
  221. struct json_object *val)
  222. {
  223. lh_table_delete(this->o.c_object, key);
  224. lh_table_insert(this->o.c_object, strdup(key), val);
  225. }
  226. struct json_object* json_object_object_get(struct json_object* this, const char *key)
  227. {
  228. return (struct json_object*) lh_table_lookup(this->o.c_object, key);
  229. }
  230. void json_object_object_del(struct json_object* this, const char *key)
  231. {
  232. lh_table_delete(this->o.c_object, key);
  233. }
  234. /* json_object_boolean */
  235. static int json_object_boolean_to_json_string(struct json_object* this,
  236. struct printbuf *pb)
  237. {
  238. if(this->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 *this = json_object_new(json_type_boolean);
  244. if(!this) return NULL;
  245. this->_to_json_string = &json_object_boolean_to_json_string;
  246. this->o.c_boolean = b;
  247. return this;
  248. }
  249. boolean json_object_get_boolean(struct json_object *this)
  250. {
  251. if(!this) return FALSE;
  252. switch(this->o_type) {
  253. case json_type_boolean:
  254. return this->o.c_boolean;
  255. case json_type_int:
  256. return (this->o.c_int != 0);
  257. case json_type_double:
  258. return (this->o.c_double != 0);
  259. case json_type_string:
  260. if(strlen(this->o.c_string)) return TRUE;
  261. default:
  262. return TRUE;
  263. }
  264. }
  265. /* json_object_int */
  266. static int json_object_int_to_json_string(struct json_object* this,
  267. struct printbuf *pb)
  268. {
  269. return sprintbuf(pb, "%d", this->o.c_int);
  270. }
  271. struct json_object* json_object_new_int(int i)
  272. {
  273. struct json_object *this = json_object_new(json_type_int);
  274. if(!this) return NULL;
  275. this->_to_json_string = &json_object_int_to_json_string;
  276. this->o.c_int = i;
  277. return this;
  278. }
  279. int json_object_get_int(struct json_object *this)
  280. {
  281. int cint;
  282. if(!this) return 0;
  283. switch(this->o_type) {
  284. case json_type_int:
  285. return this->o.c_int;
  286. case json_type_double:
  287. return (int)this->o.c_double;
  288. case json_type_boolean:
  289. return this->o.c_boolean;
  290. case json_type_string:
  291. if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint;
  292. default:
  293. return 0;
  294. }
  295. }
  296. /* json_object_double */
  297. static int json_object_double_to_json_string(struct json_object* this,
  298. struct printbuf *pb)
  299. {
  300. return sprintbuf(pb, "%lf", this->o.c_double);
  301. }
  302. struct json_object* json_object_new_double(double d)
  303. {
  304. struct json_object *this = json_object_new(json_type_double);
  305. if(!this) return NULL;
  306. this->_to_json_string = &json_object_double_to_json_string;
  307. this->o.c_double = d;
  308. return this;
  309. }
  310. double json_object_get_double(struct json_object *this)
  311. {
  312. double cdouble;
  313. if(!this) return 0.0;
  314. switch(this->o_type) {
  315. case json_type_double:
  316. return this->o.c_double;
  317. case json_type_int:
  318. return this->o.c_int;
  319. case json_type_boolean:
  320. return this->o.c_boolean;
  321. case json_type_string:
  322. if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble;
  323. default:
  324. return 0.0;
  325. }
  326. }
  327. /* json_object_string */
  328. static int json_object_string_to_json_string(struct json_object* this,
  329. struct printbuf *pb)
  330. {
  331. sprintbuf(pb, "\"");
  332. json_escape_str(pb, this->o.c_string);
  333. sprintbuf(pb, "\"");
  334. return 0;
  335. }
  336. static void json_object_string_delete(struct json_object* this)
  337. {
  338. free(this->o.c_string);
  339. json_object_generic_delete(this);
  340. }
  341. struct json_object* json_object_new_string(const char *s)
  342. {
  343. struct json_object *this = json_object_new(json_type_string);
  344. if(!this) return NULL;
  345. this->_delete = &json_object_string_delete;
  346. this->_to_json_string = &json_object_string_to_json_string;
  347. this->o.c_string = strdup(s);
  348. return this;
  349. }
  350. struct json_object* json_object_new_string_len(const char *s, int len)
  351. {
  352. struct json_object *this = json_object_new(json_type_string);
  353. if(!this) return NULL;
  354. this->_delete = &json_object_string_delete;
  355. this->_to_json_string = &json_object_string_to_json_string;
  356. this->o.c_string = strndup(s, len);
  357. return this;
  358. }
  359. const char* json_object_get_string(struct json_object *this)
  360. {
  361. if(!this) return NULL;
  362. switch(this->o_type) {
  363. case json_type_string:
  364. return this->o.c_string;
  365. default:
  366. return json_object_to_json_string(this);
  367. }
  368. }
  369. /* json_object_array */
  370. static int json_object_array_to_json_string(struct json_object* this,
  371. struct printbuf *pb)
  372. {
  373. int i;
  374. sprintbuf(pb, "[");
  375. for(i=0; i < json_object_array_length(this); i++) {
  376. struct json_object *val;
  377. if(i) { sprintbuf(pb, ", "); }
  378. else { sprintbuf(pb, " "); }
  379. val = json_object_array_get_idx(this, i);
  380. if(val == NULL) { sprintbuf(pb, "null"); }
  381. else { val->_to_json_string(val, pb); }
  382. }
  383. return sprintbuf(pb, " ]");
  384. }
  385. static void json_object_array_entry_free(void *data)
  386. {
  387. json_object_put((struct json_object*)data);
  388. }
  389. static void json_object_array_delete(struct json_object* this)
  390. {
  391. array_list_free(this->o.c_array);
  392. json_object_generic_delete(this);
  393. }
  394. struct json_object* json_object_new_array()
  395. {
  396. struct json_object *this = json_object_new(json_type_array);
  397. if(!this) return NULL;
  398. this->_delete = &json_object_array_delete;
  399. this->_to_json_string = &json_object_array_to_json_string;
  400. this->o.c_array = array_list_new(&json_object_array_entry_free);
  401. return this;
  402. }
  403. struct array_list* json_object_get_array(struct json_object *this)
  404. {
  405. if(!this) return NULL;
  406. switch(this->o_type) {
  407. case json_type_array:
  408. return this->o.c_array;
  409. default:
  410. return NULL;
  411. }
  412. }
  413. int json_object_array_length(struct json_object *this)
  414. {
  415. return array_list_length(this->o.c_array);
  416. }
  417. int json_object_array_add(struct json_object *this,struct json_object *val)
  418. {
  419. return array_list_add(this->o.c_array, val);
  420. }
  421. int json_object_array_put_idx(struct json_object *this, int idx,
  422. struct json_object *val)
  423. {
  424. return array_list_put_idx(this->o.c_array, idx, val);
  425. }
  426. struct json_object* json_object_array_get_idx(struct json_object *this,
  427. int idx)
  428. {
  429. return (struct json_object*)array_list_get_idx(this->o.c_array, idx);
  430. }

No Description

Contributors (1)