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

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