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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. struct json_object* json_object_object_get(struct json_object* jso, const char *key)
  339. {
  340. struct json_object *result = NULL;
  341. json_object_object_get_ex(jso, key, &result);
  342. return result;
  343. }
  344. json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value)
  345. {
  346. if (value != NULL)
  347. *value = NULL;
  348. if (NULL == jso)
  349. return FALSE;
  350. switch(jso->o_type)
  351. {
  352. case json_type_object:
  353. return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value);
  354. default:
  355. if (value != NULL)
  356. *value = NULL;
  357. return FALSE;
  358. }
  359. }
  360. void json_object_object_del(struct json_object* jso, const char *key)
  361. {
  362. lh_table_delete(jso->o.c_object, key);
  363. }
  364. /* json_object_boolean */
  365. static int json_object_boolean_to_json_string(struct json_object* jso,
  366. struct printbuf *pb,
  367. int level,
  368. int flags)
  369. {
  370. if(jso->o.c_boolean) return sprintbuf(pb, "true");
  371. else return sprintbuf(pb, "false");
  372. }
  373. struct json_object* json_object_new_boolean(json_bool b)
  374. {
  375. struct json_object *jso = json_object_new(json_type_boolean);
  376. if(!jso) return NULL;
  377. jso->_to_json_string = &json_object_boolean_to_json_string;
  378. jso->o.c_boolean = b;
  379. return jso;
  380. }
  381. json_bool json_object_get_boolean(struct json_object *jso)
  382. {
  383. if(!jso) return FALSE;
  384. switch(jso->o_type) {
  385. case json_type_boolean:
  386. return jso->o.c_boolean;
  387. case json_type_int:
  388. return (jso->o.c_int64 != 0);
  389. case json_type_double:
  390. return (jso->o.c_double != 0);
  391. case json_type_string:
  392. return (jso->o.c_string.len != 0);
  393. default:
  394. return FALSE;
  395. }
  396. }
  397. /* json_object_int */
  398. static int json_object_int_to_json_string(struct json_object* jso,
  399. struct printbuf *pb,
  400. int level,
  401. int flags)
  402. {
  403. return sprintbuf(pb, "%"PRId64, jso->o.c_int64);
  404. }
  405. struct json_object* json_object_new_int(int32_t i)
  406. {
  407. struct json_object *jso = json_object_new(json_type_int);
  408. if(!jso) return NULL;
  409. jso->_to_json_string = &json_object_int_to_json_string;
  410. jso->o.c_int64 = i;
  411. return jso;
  412. }
  413. int32_t json_object_get_int(struct json_object *jso)
  414. {
  415. int64_t cint64;
  416. enum json_type o_type;
  417. if(!jso) return 0;
  418. o_type = jso->o_type;
  419. cint64 = jso->o.c_int64;
  420. if (o_type == json_type_string)
  421. {
  422. /*
  423. * Parse strings into 64-bit numbers, then use the
  424. * 64-to-32-bit number handling below.
  425. */
  426. if (json_parse_int64(jso->o.c_string.str, &cint64) != 0)
  427. return 0; /* whoops, it didn't work. */
  428. o_type = json_type_int;
  429. }
  430. switch(o_type) {
  431. case json_type_int:
  432. /* Make sure we return the correct values for out of range numbers. */
  433. if (cint64 <= INT32_MIN)
  434. return INT32_MIN;
  435. else if (cint64 >= INT32_MAX)
  436. return INT32_MAX;
  437. else
  438. return (int32_t)cint64;
  439. case json_type_double:
  440. return (int32_t)jso->o.c_double;
  441. case json_type_boolean:
  442. return jso->o.c_boolean;
  443. default:
  444. return 0;
  445. }
  446. }
  447. struct json_object* json_object_new_int64(int64_t i)
  448. {
  449. struct json_object *jso = json_object_new(json_type_int);
  450. if(!jso) return NULL;
  451. jso->_to_json_string = &json_object_int_to_json_string;
  452. jso->o.c_int64 = i;
  453. return jso;
  454. }
  455. int64_t json_object_get_int64(struct json_object *jso)
  456. {
  457. int64_t cint;
  458. if(!jso) return 0;
  459. switch(jso->o_type) {
  460. case json_type_int:
  461. return jso->o.c_int64;
  462. case json_type_double:
  463. return (int64_t)jso->o.c_double;
  464. case json_type_boolean:
  465. return jso->o.c_boolean;
  466. case json_type_string:
  467. if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint;
  468. default:
  469. return 0;
  470. }
  471. }
  472. /* json_object_double */
  473. static int json_object_double_to_json_string(struct json_object* jso,
  474. struct printbuf *pb,
  475. int level,
  476. int flags)
  477. {
  478. return sprintbuf(pb, "%f", jso->o.c_double);
  479. }
  480. struct json_object* json_object_new_double(double d)
  481. {
  482. struct json_object *jso = json_object_new(json_type_double);
  483. if(!jso) return NULL;
  484. jso->_to_json_string = &json_object_double_to_json_string;
  485. jso->o.c_double = d;
  486. return jso;
  487. }
  488. double json_object_get_double(struct json_object *jso)
  489. {
  490. double cdouble;
  491. if(!jso) return 0.0;
  492. switch(jso->o_type) {
  493. case json_type_double:
  494. return jso->o.c_double;
  495. case json_type_int:
  496. return jso->o.c_int64;
  497. case json_type_boolean:
  498. return jso->o.c_boolean;
  499. case json_type_string:
  500. if(sscanf(jso->o.c_string.str, "%lf", &cdouble) == 1) return cdouble;
  501. default:
  502. return 0.0;
  503. }
  504. }
  505. /* json_object_string */
  506. static int json_object_string_to_json_string(struct json_object* jso,
  507. struct printbuf *pb,
  508. int level,
  509. int flags)
  510. {
  511. sprintbuf(pb, "\"");
  512. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  513. sprintbuf(pb, "\"");
  514. return 0;
  515. }
  516. static void json_object_string_delete(struct json_object* jso)
  517. {
  518. free(jso->o.c_string.str);
  519. json_object_generic_delete(jso);
  520. }
  521. struct json_object* json_object_new_string(const char *s)
  522. {
  523. struct json_object *jso = json_object_new(json_type_string);
  524. if(!jso) return NULL;
  525. jso->_delete = &json_object_string_delete;
  526. jso->_to_json_string = &json_object_string_to_json_string;
  527. jso->o.c_string.str = strdup(s);
  528. jso->o.c_string.len = strlen(s);
  529. return jso;
  530. }
  531. struct json_object* json_object_new_string_len(const char *s, int len)
  532. {
  533. struct json_object *jso = json_object_new(json_type_string);
  534. if(!jso) return NULL;
  535. jso->_delete = &json_object_string_delete;
  536. jso->_to_json_string = &json_object_string_to_json_string;
  537. jso->o.c_string.str = (char*)malloc(len + 1);
  538. memcpy(jso->o.c_string.str, (void *)s, len);
  539. jso->o.c_string.str[len] = '\0';
  540. jso->o.c_string.len = len;
  541. return jso;
  542. }
  543. const char* json_object_get_string(struct json_object *jso)
  544. {
  545. if(!jso) return NULL;
  546. switch(jso->o_type) {
  547. case json_type_string:
  548. return jso->o.c_string.str;
  549. default:
  550. return json_object_to_json_string(jso);
  551. }
  552. }
  553. int json_object_get_string_len(struct json_object *jso) {
  554. if(!jso) return 0;
  555. switch(jso->o_type) {
  556. case json_type_string:
  557. return jso->o.c_string.len;
  558. default:
  559. return 0;
  560. }
  561. }
  562. /* json_object_array */
  563. static int json_object_array_to_json_string(struct json_object* jso,
  564. struct printbuf *pb,
  565. int level,
  566. int flags)
  567. {
  568. int had_children = 0;
  569. int ii;
  570. sprintbuf(pb, "[");
  571. if (flags & JSON_C_TO_STRING_PRETTY)
  572. sprintbuf(pb, "\n");
  573. for(ii=0; ii < json_object_array_length(jso); ii++)
  574. {
  575. struct json_object *val;
  576. if (had_children)
  577. {
  578. sprintbuf(pb, ",");
  579. if (flags & JSON_C_TO_STRING_PRETTY)
  580. sprintbuf(pb, "\n");
  581. }
  582. had_children = 1;
  583. if (flags & JSON_C_TO_STRING_SPACED)
  584. sprintbuf(pb, " ");
  585. indent(pb, level + 1, flags);
  586. val = json_object_array_get_idx(jso, ii);
  587. if(val == NULL)
  588. sprintbuf(pb, "null");
  589. else
  590. val->_to_json_string(val, pb, level+1, flags);
  591. }
  592. if (flags & JSON_C_TO_STRING_PRETTY)
  593. {
  594. if (had_children)
  595. sprintbuf(pb, "\n");
  596. indent(pb,level,flags);
  597. }
  598. if (flags & JSON_C_TO_STRING_SPACED)
  599. return sprintbuf(pb, " ]");
  600. else
  601. return sprintbuf(pb, "]");
  602. }
  603. static void json_object_array_entry_free(void *data)
  604. {
  605. json_object_put((struct json_object*)data);
  606. }
  607. static void json_object_array_delete(struct json_object* jso)
  608. {
  609. array_list_free(jso->o.c_array);
  610. json_object_generic_delete(jso);
  611. }
  612. struct json_object* json_object_new_array(void)
  613. {
  614. struct json_object *jso = json_object_new(json_type_array);
  615. if(!jso) return NULL;
  616. jso->_delete = &json_object_array_delete;
  617. jso->_to_json_string = &json_object_array_to_json_string;
  618. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  619. return jso;
  620. }
  621. struct array_list* json_object_get_array(struct json_object *jso)
  622. {
  623. if(!jso) return NULL;
  624. switch(jso->o_type) {
  625. case json_type_array:
  626. return jso->o.c_array;
  627. default:
  628. return NULL;
  629. }
  630. }
  631. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  632. {
  633. array_list_sort(jso->o.c_array, sort_fn);
  634. }
  635. int json_object_array_length(struct json_object *jso)
  636. {
  637. return array_list_length(jso->o.c_array);
  638. }
  639. int json_object_array_add(struct json_object *jso,struct json_object *val)
  640. {
  641. return array_list_add(jso->o.c_array, val);
  642. }
  643. int json_object_array_put_idx(struct json_object *jso, int idx,
  644. struct json_object *val)
  645. {
  646. return array_list_put_idx(jso->o.c_array, idx, val);
  647. }
  648. struct json_object* json_object_array_get_idx(struct json_object *jso,
  649. int idx)
  650. {
  651. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  652. }