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

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