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

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