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

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