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

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