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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. // fallthrough
  479. default:
  480. return 0;
  481. }
  482. }
  483. /* json_object_double */
  484. static int json_object_double_to_json_string(struct json_object* jso,
  485. struct printbuf *pb,
  486. int level,
  487. int flags)
  488. {
  489. char buf[128], *p, *q;
  490. int size;
  491. /* Although JSON RFC does not support
  492. NaN or Infinity as numeric values
  493. ECMA 262 section 9.8.1 defines
  494. how to handle these cases as strings */
  495. if(isnan(jso->o.c_double))
  496. size = snprintf(buf, sizeof(buf), "NaN");
  497. else if(isinf(jso->o.c_double))
  498. if(jso->o.c_double > 0)
  499. size = snprintf(buf, sizeof(buf), "Infinity");
  500. else
  501. size = snprintf(buf, sizeof(buf), "-Infinity");
  502. else
  503. size = snprintf(buf, sizeof(buf), "%.17g", jso->o.c_double);
  504. p = strchr(buf, ',');
  505. if (p) {
  506. *p = '.';
  507. } else {
  508. p = strchr(buf, '.');
  509. }
  510. if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
  511. /* last useful digit, always keep 1 zero */
  512. p++;
  513. for (q=p ; *q ; q++) {
  514. if (*q!='0') p=q;
  515. }
  516. /* drop trailing zeroes */
  517. *(++p) = 0;
  518. size = p-buf;
  519. }
  520. printbuf_memappend(pb, buf, size);
  521. return size;
  522. }
  523. struct json_object* json_object_new_double(double d)
  524. {
  525. struct json_object *jso = json_object_new(json_type_double);
  526. if (!jso)
  527. return NULL;
  528. jso->_to_json_string = &json_object_double_to_json_string;
  529. jso->o.c_double = d;
  530. return jso;
  531. }
  532. struct json_object* json_object_new_double_s(double d, const char *ds)
  533. {
  534. struct json_object *jso = json_object_new_double(d);
  535. if (!jso)
  536. return NULL;
  537. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  538. strdup(ds), json_object_free_userdata);
  539. return jso;
  540. }
  541. int json_object_userdata_to_json_string(struct json_object *jso,
  542. struct printbuf *pb, int level, int flags)
  543. {
  544. int userdata_len = strlen(jso->_userdata);
  545. printbuf_memappend(pb, jso->_userdata, userdata_len);
  546. return userdata_len;
  547. }
  548. void json_object_free_userdata(struct json_object *jso, void *userdata)
  549. {
  550. free(userdata);
  551. }
  552. double json_object_get_double(struct json_object *jso)
  553. {
  554. double cdouble;
  555. char *errPtr = NULL;
  556. if(!jso) return 0.0;
  557. switch(jso->o_type) {
  558. case json_type_double:
  559. return jso->o.c_double;
  560. case json_type_int:
  561. return jso->o.c_int64;
  562. case json_type_boolean:
  563. return jso->o.c_boolean;
  564. case json_type_string:
  565. errno = 0;
  566. cdouble = strtod(jso->o.c_string.str,&errPtr);
  567. /* if conversion stopped at the first character, return 0.0 */
  568. if (errPtr == jso->o.c_string.str)
  569. return 0.0;
  570. /*
  571. * Check that the conversion terminated on something sensible
  572. *
  573. * For example, { "pay" : 123AB } would parse as 123.
  574. */
  575. if (*errPtr != '\0')
  576. return 0.0;
  577. /*
  578. * If strtod encounters a string which would exceed the
  579. * capacity of a double, it returns +/- HUGE_VAL and sets
  580. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  581. * from a conversion, so we need to check errno.
  582. *
  583. * Underflow also sets errno to ERANGE, but it returns 0 in
  584. * that case, which is what we will return anyway.
  585. *
  586. * See CERT guideline ERR30-C
  587. */
  588. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  589. (ERANGE == errno))
  590. cdouble = 0.0;
  591. return cdouble;
  592. default:
  593. return 0.0;
  594. }
  595. }
  596. /* json_object_string */
  597. static int json_object_string_to_json_string(struct json_object* jso,
  598. struct printbuf *pb,
  599. int level,
  600. int flags)
  601. {
  602. sprintbuf(pb, "\"");
  603. json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len);
  604. sprintbuf(pb, "\"");
  605. return 0;
  606. }
  607. static void json_object_string_delete(struct json_object* jso)
  608. {
  609. free(jso->o.c_string.str);
  610. json_object_generic_delete(jso);
  611. }
  612. struct json_object* json_object_new_string(const char *s)
  613. {
  614. struct json_object *jso = json_object_new(json_type_string);
  615. if(!jso) return NULL;
  616. jso->_delete = &json_object_string_delete;
  617. jso->_to_json_string = &json_object_string_to_json_string;
  618. jso->o.c_string.str = strdup(s);
  619. jso->o.c_string.len = strlen(s);
  620. return jso;
  621. }
  622. struct json_object* json_object_new_string_len(const char *s, int len)
  623. {
  624. struct json_object *jso = json_object_new(json_type_string);
  625. if(!jso) return NULL;
  626. jso->_delete = &json_object_string_delete;
  627. jso->_to_json_string = &json_object_string_to_json_string;
  628. jso->o.c_string.str = (char*)malloc(len + 1);
  629. memcpy(jso->o.c_string.str, (void *)s, len);
  630. jso->o.c_string.str[len] = '\0';
  631. jso->o.c_string.len = len;
  632. return jso;
  633. }
  634. const char* json_object_get_string(struct json_object *jso)
  635. {
  636. if(!jso) return NULL;
  637. switch(jso->o_type) {
  638. case json_type_string:
  639. return jso->o.c_string.str;
  640. default:
  641. return json_object_to_json_string(jso);
  642. }
  643. }
  644. int json_object_get_string_len(struct json_object *jso) {
  645. if(!jso) return 0;
  646. switch(jso->o_type) {
  647. case json_type_string:
  648. return jso->o.c_string.len;
  649. default:
  650. return 0;
  651. }
  652. }
  653. /* json_object_array */
  654. static int json_object_array_to_json_string(struct json_object* jso,
  655. struct printbuf *pb,
  656. int level,
  657. int flags)
  658. {
  659. int had_children = 0;
  660. int ii;
  661. sprintbuf(pb, "[");
  662. if (flags & JSON_C_TO_STRING_PRETTY)
  663. sprintbuf(pb, "\n");
  664. for(ii=0; ii < json_object_array_length(jso); ii++)
  665. {
  666. struct json_object *val;
  667. if (had_children)
  668. {
  669. sprintbuf(pb, ",");
  670. if (flags & JSON_C_TO_STRING_PRETTY)
  671. sprintbuf(pb, "\n");
  672. }
  673. had_children = 1;
  674. if (flags & JSON_C_TO_STRING_SPACED)
  675. sprintbuf(pb, " ");
  676. indent(pb, level + 1, flags);
  677. val = json_object_array_get_idx(jso, ii);
  678. if(val == NULL)
  679. sprintbuf(pb, "null");
  680. else
  681. val->_to_json_string(val, pb, level+1, flags);
  682. }
  683. if (flags & JSON_C_TO_STRING_PRETTY)
  684. {
  685. if (had_children)
  686. sprintbuf(pb, "\n");
  687. indent(pb,level,flags);
  688. }
  689. if (flags & JSON_C_TO_STRING_SPACED)
  690. return sprintbuf(pb, " ]");
  691. else
  692. return sprintbuf(pb, "]");
  693. }
  694. static void json_object_array_entry_free(void *data)
  695. {
  696. json_object_put((struct json_object*)data);
  697. }
  698. static void json_object_array_delete(struct json_object* jso)
  699. {
  700. array_list_free(jso->o.c_array);
  701. json_object_generic_delete(jso);
  702. }
  703. struct json_object* json_object_new_array(void)
  704. {
  705. struct json_object *jso = json_object_new(json_type_array);
  706. if(!jso) return NULL;
  707. jso->_delete = &json_object_array_delete;
  708. jso->_to_json_string = &json_object_array_to_json_string;
  709. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  710. return jso;
  711. }
  712. struct array_list* json_object_get_array(struct json_object *jso)
  713. {
  714. if(!jso) return NULL;
  715. switch(jso->o_type) {
  716. case json_type_array:
  717. return jso->o.c_array;
  718. default:
  719. return NULL;
  720. }
  721. }
  722. void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
  723. {
  724. array_list_sort(jso->o.c_array, sort_fn);
  725. }
  726. int json_object_array_length(struct json_object *jso)
  727. {
  728. return array_list_length(jso->o.c_array);
  729. }
  730. int json_object_array_add(struct json_object *jso,struct json_object *val)
  731. {
  732. return array_list_add(jso->o.c_array, val);
  733. }
  734. int json_object_array_put_idx(struct json_object *jso, int idx,
  735. struct json_object *val)
  736. {
  737. return array_list_put_idx(jso->o.c_array, idx, val);
  738. }
  739. struct json_object* json_object_array_get_idx(struct json_object *jso,
  740. int idx)
  741. {
  742. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  743. }