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

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

No Description

Contributors (1)