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

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