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

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