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

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

No Description

Contributors (1)