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

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

No Description

Contributors (1)