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

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

No Description

Contributors (1)