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

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

No Description

Contributors (1)