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 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #include "config.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include "debug.h"
  18. #include "printbuf.h"
  19. #include "linkhash.h"
  20. #include "arraylist.h"
  21. #include "json_inttypes.h"
  22. #include "json_object.h"
  23. #include "json_object_private.h"
  24. #include "json_util.h"
  25. #if !defined(HAVE_STRDUP) && defined(_MSC_VER)
  26. /* MSC has the version as _strdup */
  27. # define strdup _strdup
  28. #elif !defined(HAVE_STRDUP)
  29. # error You do not have strdup on your system.
  30. #endif /* HAVE_STRDUP */
  31. #if !defined(HAVE_STRNDUP)
  32. char* strndup(const char* str, size_t n);
  33. #endif /* !HAVE_STRNDUP */
  34. // Don't define this. It's not thread-safe.
  35. /* #define REFCOUNT_DEBUG 1 */
  36. const char *json_number_chars = "0123456789.+-eE";
  37. const char *json_hex_chars = "0123456789abcdefABCDEF";
  38. static void json_object_generic_delete(struct json_object* jso);
  39. static struct json_object* json_object_new(enum json_type o_type);
  40. static json_object_to_json_string_fn json_object_object_to_json_string;
  41. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  42. static json_object_to_json_string_fn json_object_int_to_json_string;
  43. static json_object_to_json_string_fn json_object_double_to_json_string;
  44. static json_object_to_json_string_fn json_object_string_to_json_string;
  45. static json_object_to_json_string_fn json_object_array_to_json_string;
  46. /* ref count debugging */
  47. #ifdef REFCOUNT_DEBUG
  48. static struct lh_table *json_object_table;
  49. static void json_object_init(void) __attribute__ ((constructor));
  50. static void json_object_init(void) {
  51. MC_DEBUG("json_object_init: creating object table\n");
  52. json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
  53. }
  54. static void json_object_fini(void) __attribute__ ((destructor));
  55. static void json_object_fini(void) {
  56. struct lh_entry *ent;
  57. if(MC_GET_DEBUG()) {
  58. if (json_object_table->count) {
  59. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  60. json_object_table->count);
  61. lh_foreach(json_object_table, ent) {
  62. struct json_object* obj = (struct json_object*)ent->v;
  63. MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
  64. }
  65. }
  66. }
  67. MC_DEBUG("json_object_fini: freeing object table\n");
  68. lh_table_free(json_object_table);
  69. }
  70. #endif /* REFCOUNT_DEBUG */
  71. /* string escaping */
  72. static int json_escape_str(struct printbuf *pb, char *str, int len)
  73. {
  74. int pos = 0, start_offset = 0;
  75. unsigned char c;
  76. while (len--) {
  77. c = str[pos];
  78. switch(c) {
  79. case '\b':
  80. case '\n':
  81. case '\r':
  82. case '\t':
  83. case '\f':
  84. case '"':
  85. case '\\':
  86. case '/':
  87. if(pos - start_offset > 0)
  88. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  89. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  90. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  91. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  92. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  93. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  94. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  95. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  96. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  97. start_offset = ++pos;
  98. break;
  99. default:
  100. if(c < ' ') {
  101. if(pos - start_offset > 0)
  102. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  103. sprintbuf(pb, "\\u00%c%c",
  104. json_hex_chars[c >> 4],
  105. json_hex_chars[c & 0xf]);
  106. start_offset = ++pos;
  107. } else pos++;
  108. }
  109. }
  110. if(pos - start_offset > 0)
  111. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  112. return 0;
  113. }
  114. /* reference counting */
  115. extern struct json_object* json_object_get(struct json_object *jso)
  116. {
  117. if(jso) {
  118. jso->_ref_count++;
  119. }
  120. return jso;
  121. }
  122. int json_object_put(struct json_object *jso)
  123. {
  124. if(jso)
  125. {
  126. jso->_ref_count--;
  127. if(!jso->_ref_count)
  128. {
  129. if (jso->_user_delete)
  130. jso->_user_delete(jso, jso->_userdata);
  131. jso->_delete(jso);
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. /* generic object construction and destruction parts */
  138. static void json_object_generic_delete(struct json_object* jso)
  139. {
  140. #ifdef REFCOUNT_DEBUG
  141. MC_DEBUG("json_object_delete_%s: %p\n",
  142. json_type_to_name(jso->o_type), jso);
  143. lh_table_delete(json_object_table, jso);
  144. #endif /* REFCOUNT_DEBUG */
  145. printbuf_free(jso->_pb);
  146. free(jso);
  147. }
  148. static struct json_object* json_object_new(enum json_type o_type)
  149. {
  150. struct json_object *jso;
  151. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  152. if(!jso) return NULL;
  153. jso->o_type = o_type;
  154. jso->_ref_count = 1;
  155. jso->_delete = &json_object_generic_delete;
  156. #ifdef REFCOUNT_DEBUG
  157. lh_table_insert(json_object_table, jso, jso);
  158. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  159. #endif /* REFCOUNT_DEBUG */
  160. return jso;
  161. }
  162. /* type checking functions */
  163. int json_object_is_type(struct json_object *jso, enum json_type type)
  164. {
  165. if (!jso)
  166. return (type == json_type_null);
  167. return (jso->o_type == type);
  168. }
  169. enum json_type json_object_get_type(struct json_object *jso)
  170. {
  171. if (!jso)
  172. return json_type_null;
  173. return jso->o_type;
  174. }
  175. /* set a custom conversion to string */
  176. void json_object_set_serializer(json_object *jso,
  177. json_object_to_json_string_fn to_string_func,
  178. void *userdata,
  179. json_object_delete_fn *user_delete)
  180. {
  181. // First, clean up any previously existing user info
  182. if (jso->_user_delete)
  183. {
  184. jso->_user_delete(jso, jso->_userdata);
  185. }
  186. jso->_userdata = NULL;
  187. jso->_user_delete = NULL;
  188. if (to_string_func == NULL)
  189. {
  190. // Reset to the standard serialization function
  191. switch(jso->o_type)
  192. {
  193. case json_type_null:
  194. jso->_to_json_string = NULL;
  195. break;
  196. case json_type_boolean:
  197. jso->_to_json_string = &json_object_boolean_to_json_string;
  198. break;
  199. case json_type_double:
  200. jso->_to_json_string = &json_object_double_to_json_string;
  201. break;
  202. case json_type_int:
  203. jso->_to_json_string = &json_object_int_to_json_string;
  204. break;
  205. case json_type_object:
  206. jso->_to_json_string = &json_object_object_to_json_string;
  207. break;
  208. case json_type_array:
  209. jso->_to_json_string = &json_object_array_to_json_string;
  210. break;
  211. case json_type_string:
  212. jso->_to_json_string = &json_object_string_to_json_string;
  213. break;
  214. }
  215. return;
  216. }
  217. jso->_to_json_string = to_string_func;
  218. jso->_userdata = userdata;
  219. jso->_user_delete = user_delete;
  220. }
  221. /* extended conversion to string */
  222. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  223. {
  224. if (!jso)
  225. return "null";
  226. if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
  227. return NULL;
  228. printbuf_reset(jso->_pb);
  229. if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
  230. return NULL;
  231. return jso->_pb->buf;
  232. }
  233. /* backwards-compatible conversion to string */
  234. const char* json_object_to_json_string(struct json_object *jso)
  235. {
  236. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  237. }
  238. static void indent(struct printbuf *pb, int level, int flags)
  239. {
  240. if (flags & JSON_C_TO_STRING_PRETTY)
  241. {
  242. printbuf_memset(pb, -1, ' ', level * 2);
  243. }
  244. }
  245. /* json_object_object */
  246. static int json_object_object_to_json_string(struct json_object* jso,
  247. struct printbuf *pb,
  248. int level,
  249. int flags)
  250. {
  251. int had_children = 0;
  252. struct json_object_iter iter;
  253. sprintbuf(pb, "{" /*}*/);
  254. if (flags & JSON_C_TO_STRING_PRETTY)
  255. sprintbuf(pb, "\n");
  256. json_object_object_foreachC(jso, iter)
  257. {
  258. if (had_children)
  259. {
  260. sprintbuf(pb, ",");
  261. if (flags & JSON_C_TO_STRING_PRETTY)
  262. sprintbuf(pb, "\n");
  263. }
  264. had_children = 1;
  265. if (flags & JSON_C_TO_STRING_SPACED)
  266. sprintbuf(pb, " ");
  267. indent(pb, level+1, flags);
  268. sprintbuf(pb, "\"");
  269. json_escape_str(pb, iter.key, strlen(iter.key));
  270. if (flags & JSON_C_TO_STRING_SPACED)
  271. sprintbuf(pb, "\": ");
  272. else
  273. sprintbuf(pb, "\":");
  274. if(iter.val == NULL)
  275. sprintbuf(pb, "null");
  276. else
  277. iter.val->_to_json_string(iter.val, pb, level+1,flags);
  278. }
  279. if (flags & JSON_C_TO_STRING_PRETTY)
  280. {
  281. if (had_children)
  282. sprintbuf(pb, "\n");
  283. indent(pb,level,flags);
  284. }
  285. if (flags & JSON_C_TO_STRING_SPACED)
  286. return sprintbuf(pb, /*{*/ " }");
  287. else
  288. return sprintbuf(pb, /*{*/ "}");
  289. }
  290. static void json_object_lh_entry_free(struct lh_entry *ent)
  291. {
  292. free(ent->k);
  293. json_object_put((struct json_object*)ent->v);
  294. }
  295. static void json_object_object_delete(struct json_object* jso)
  296. {
  297. lh_table_free(jso->o.c_object);
  298. json_object_generic_delete(jso);
  299. }
  300. struct json_object* json_object_new_object(void)
  301. {
  302. struct json_object *jso = json_object_new(json_type_object);
  303. if(!jso) return NULL;
  304. jso->_delete = &json_object_object_delete;
  305. jso->_to_json_string = &json_object_object_to_json_string;
  306. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  307. NULL, &json_object_lh_entry_free);
  308. return jso;
  309. }
  310. struct lh_table* json_object_get_object(struct json_object *jso)
  311. {
  312. if(!jso) return NULL;
  313. switch(jso->o_type) {
  314. case json_type_object:
  315. return jso->o.c_object;
  316. default:
  317. return NULL;
  318. }
  319. }
  320. void json_object_object_add(struct json_object* jso, const char *key,
  321. struct json_object *val)
  322. {
  323. // We lookup the entry and replace the value, rather than just deleting
  324. // and re-adding it, so the existing key remains valid.
  325. json_object *existing_value = NULL;
  326. struct lh_entry *existing_entry;
  327. existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
  328. if (!existing_entry)
  329. {
  330. lh_table_insert(jso->o.c_object, strdup(key), val);
  331. return;
  332. }
  333. existing_value = (void *)existing_entry->v;
  334. if (existing_value)
  335. json_object_put(existing_value);
  336. existing_entry->v = val;
  337. }
  338. int json_object_object_length(struct json_object *jso)
  339. {
  340. return lh_table_length(jso->o.c_object);
  341. }
  342. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  343. {
  344. struct json_object *result = NULL;
  345. json_object_object_get_ex(jso, key, &result);
  346. return result;
  347. }
  348. json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
  349. {
  350. if (value != NULL)
  351. *value = NULL;
  352. if (NULL == jso)
  353. return FALSE;
  354. switch(jso->o_type)
  355. {
  356. case json_type_object:
  357. return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
  358. default:
  359. if (value != NULL)
  360. *value = NULL;
  361. return FALSE;
  362. }
  363. }
  364. void json_object_object_del(struct json_object* jso, const char *key)
  365. {
  366. lh_table_delete(jso->o.c_object, key);
  367. }
  368. /* json_object_boolean */
  369. static int json_object_boolean_to_json_string(struct json_object* jso,
  370. struct printbuf *pb,
  371. int level,
  372. int flags)
  373. {
  374. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  375. else return sprintbuf(pb, "false");
  376. }
  377. struct json_object* json_object_new_boolean(json_bool b)
  378. {
  379. struct json_object *jso = json_object_new(json_type_boolean);
  380. if(!jso) return NULL;
  381. jso->_to_json_string = &json_object_boolean_to_json_string;
  382. jso->o.c_boolean = b;
  383. return jso;
  384. }
  385. json_bool json_object_get_boolean(struct json_object *jso)
  386. {
  387. if(!jso) return FALSE;
  388. switch(jso->o_type) {
  389. case json_type_boolean:
  390. return jso->o.c_boolean;
  391. case json_type_int:
  392. return (jso->o.c_int64 != 0);
  393. case json_type_double:
  394. return (jso->o.c_double != 0);
  395. case json_type_string:
  396. return (jso->o.c_string.len != 0);
  397. default:
  398. return FALSE;
  399. }
  400. }
  401. /* json_object_int */
  402. static int json_object_int_to_json_string(struct json_object* jso,
  403. struct printbuf *pb,
  404. int level,
  405. int flags)
  406. {
  407. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  408. }
  409. struct json_object* json_object_new_int(int32_t i)
  410. {
  411. struct json_object *jso = json_object_new(json_type_int);
  412. if(!jso) return NULL;
  413. jso->_to_json_string = &json_object_int_to_json_string;
  414. jso->o.c_int64 = i;
  415. return jso;
  416. }
  417. int32_t json_object_get_int(struct json_object *jso)
  418. {
  419. int64_t cint64;
  420. enum json_type o_type;
  421. if(!jso) return 0;
  422. o_type = jso->o_type;
  423. cint64 = jso->o.c_int64;
  424. if (o_type == json_type_string)
  425. {
  426. /*
  427. * Parse strings into 64-bit numbers, then use the
  428. * 64-to-32-bit number handling below.
  429. */
  430. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  431. return 0; /* whoops, it didn't work. */
  432. o_type = json_type_int;
  433. }
  434. switch(o_type) {
  435. case json_type_int:
  436. /* Make sure we return the correct values for out of range numbers. */
  437. if (cint64 <= INT32_MIN)
  438. return INT32_MIN;
  439. else if (cint64 >= INT32_MAX)
  440. return INT32_MAX;
  441. else
  442. return (int32_t)cint64;
  443. case json_type_double:
  444. return (int32_t)jso->o.c_double;
  445. case json_type_boolean:
  446. return jso->o.c_boolean;
  447. default:
  448. return 0;
  449. }
  450. }
  451. struct json_object* json_object_new_int64(int64_t i)
  452. {
  453. struct json_object *jso = json_object_new(json_type_int);
  454. if(!jso) return NULL;
  455. jso->_to_json_string = &json_object_int_to_json_string;
  456. jso->o.c_int64 = i;
  457. return jso;
  458. }
  459. int64_t json_object_get_int64(struct json_object *jso)
  460. {
  461. int64_t cint;
  462. if(!jso) return 0;
  463. switch(jso->o_type) {
  464. case json_type_int:
  465. return jso->o.c_int64;
  466. case json_type_double:
  467. return (int64_t)jso->o.c_double;
  468. case json_type_boolean:
  469. return jso->o.c_boolean;
  470. case json_type_string:
  471. if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
  472. default:
  473. return 0;
  474. }
  475. }
  476. /* json_object_double */
  477. static int json_object_double_to_json_string(struct json_object* jso,
  478. struct printbuf *pb,
  479. int level,
  480. int flags)
  481. {
  482. char buf[128], *p, *q;
  483. int size;
  484. size = snprintf(buf, 128, "%f", jso->o.c_double);
  485. p = strchr(buf, ',');
  486. if (p) {
  487. *p = '.';
  488. } else {
  489. p = strchr(buf, '.');
  490. }
  491. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  492. /* last useful digit, always keep 1 zero */
  493. p++;
  494. for (q=p ; *q ; q++) {
  495. if (*q!='0') p=q;
  496. }
  497. /* drop trailing zeroes */
  498. *(++p) = 0;
  499. size = p-buf;
  500. }
  501. printbuf_memappend(pb, buf, size);
  502. return size;
  503. }
  504. struct json_object* json_object_new_double(double d)
  505. {
  506. struct json_object *jso = json_object_new(json_type_double);
  507. if(!jso) return NULL;
  508. jso->_to_json_string = &json_object_double_to_json_string;
  509. jso->o.c_double = d;
  510. return jso;
  511. }
  512. double json_object_get_double(struct json_object *jso)
  513. {
  514. double cdouble;
  515. if(!jso) return 0.0;
  516. switch(jso->o_type) {
  517. case json_type_double:
  518. return jso->o.c_double;
  519. case json_type_int:
  520. return jso->o.c_int64;
  521. case json_type_boolean:
  522. return jso->o.c_boolean;
  523. case json_type_string:
  524. if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
  525. default:
  526. return 0.0;
  527. }
  528. }
  529. /* json_object_string */
  530. static int json_object_string_to_json_string(struct json_object* jso,
  531. struct printbuf *pb,
  532. int level,
  533. int flags)
  534. {
  535. sprintbuf(pb, "\"");
  536. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  537. sprintbuf(pb, "\"");
  538. return 0;
  539. }
  540. static void json_object_string_delete(struct json_object* jso)
  541. {
  542. free(jso->o.c_string.str);
  543. json_object_generic_delete(jso);
  544. }
  545. struct json_object* json_object_new_string(const char *s)
  546. {
  547. struct json_object *jso = json_object_new(json_type_string);
  548. if(!jso) return NULL;
  549. jso->_delete = &json_object_string_delete;
  550. jso->_to_json_string = &json_object_string_to_json_string;
  551. jso->o.c_string.str = strdup(s);
  552. jso->o.c_string.len = strlen(s);
  553. return jso;
  554. }
  555. struct json_object* json_object_new_string_len(const char *s, int len)
  556. {
  557. struct json_object *jso = json_object_new(json_type_string);
  558. if(!jso) return NULL;
  559. jso->_delete = &json_object_string_delete;
  560. jso->_to_json_string = &json_object_string_to_json_string;
  561. jso->o.c_string.str = (char*)malloc(len + 1);
  562. memcpy(jso->o.c_string.str, (void *)s, len);
  563. jso->o.c_string.str[len] = '\0';
  564. jso->o.c_string.len = len;
  565. return jso;
  566. }
  567. const char* json_object_get_string(struct json_object *jso)
  568. {
  569. if(!jso) return NULL;
  570. switch(jso->o_type) {
  571. case json_type_string:
  572. return jso->o.c_string.str;
  573. default:
  574. return json_object_to_json_string(jso);
  575. }
  576. }
  577. int json_object_get_string_len(struct json_object *jso) {
  578. if(!jso) return 0;
  579. switch(jso->o_type) {
  580. case json_type_string:
  581. return jso->o.c_string.len;
  582. default:
  583. return 0;
  584. }
  585. }
  586. /* json_object_array */
  587. static int json_object_array_to_json_string(struct json_object* jso,
  588. struct printbuf *pb,
  589. int level,
  590. int flags)
  591. {
  592. int had_children = 0;
  593. int ii;
  594. sprintbuf(pb, "[");
  595. if (flags & JSON_C_TO_STRING_PRETTY)
  596. sprintbuf(pb, "\n");
  597. for(ii=0; ii < json_object_array_length(jso); ii++)
  598. {
  599. struct json_object *val;
  600. if (had_children)
  601. {
  602. sprintbuf(pb, ",");
  603. if (flags & JSON_C_TO_STRING_PRETTY)
  604. sprintbuf(pb, "\n");
  605. }
  606. had_children = 1;
  607. if (flags & JSON_C_TO_STRING_SPACED)
  608. sprintbuf(pb, " ");
  609. indent(pb, level + 1, flags);
  610. val = json_object_array_get_idx(jso, ii);
  611. if(val == NULL)
  612. sprintbuf(pb, "null");
  613. else
  614. val->_to_json_string(val, pb, level+1, flags);
  615. }
  616. if (flags & JSON_C_TO_STRING_PRETTY)
  617. {
  618. if (had_children)
  619. sprintbuf(pb, "\n");
  620. indent(pb,level,flags);
  621. }
  622. if (flags & JSON_C_TO_STRING_SPACED)
  623. return sprintbuf(pb, " ]");
  624. else
  625. return sprintbuf(pb, "]");
  626. }
  627. static void json_object_array_entry_free(void *data)
  628. {
  629. json_object_put((struct json_object*)data);
  630. }
  631. static void json_object_array_delete(struct json_object* jso)
  632. {
  633. array_list_free(jso->o.c_array);
  634. json_object_generic_delete(jso);
  635. }
  636. struct json_object* json_object_new_array(void)
  637. {
  638. struct json_object *jso = json_object_new(json_type_array);
  639. if(!jso) return NULL;
  640. jso->_delete = &json_object_array_delete;
  641. jso->_to_json_string = &json_object_array_to_json_string;
  642. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  643. return jso;
  644. }
  645. struct array_list* json_object_get_array(struct json_object *jso)
  646. {
  647. if(!jso) return NULL;
  648. switch(jso->o_type) {
  649. case json_type_array:
  650. return jso->o.c_array;
  651. default:
  652. return NULL;
  653. }
  654. }
  655. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  656. {
  657. array_list_sort(jso->o.c_array, sort_fn);
  658. }
  659. int json_object_array_length(struct json_object *jso)
  660. {
  661. return array_list_length(jso->o.c_array);
  662. }
  663. int json_object_array_add(struct json_object *jso,struct json_object *val)
  664. {
  665. return array_list_add(jso->o.c_array, val);
  666. }
  667. int json_object_array_put_idx(struct json_object *jso, int idx,
  668. struct json_object *val)
  669. {
  670. return array_list_put_idx(jso->o.c_array, idx, val);
  671. }
  672. struct json_object* json_object_array_get_idx(struct json_object *jso,
  673. int idx)
  674. {
  675. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  676. }