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

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