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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 "strerror_override.h"
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include "debug.h"
  21. #include "printbuf.h"
  22. #include "linkhash.h"
  23. #include "arraylist.h"
  24. #include "json_inttypes.h"
  25. #include "json_object.h"
  26. #include "json_object_private.h"
  27. #include "json_util.h"
  28. #include "math_compat.h"
  29. #include "strdup_compat.h"
  30. #include "snprintf_compat.h"
  31. // Don't define this. It's not thread-safe.
  32. /* #define REFCOUNT_DEBUG 1 */
  33. const char *json_number_chars = "0123456789.+-eE";
  34. const char *json_hex_chars = "0123456789abcdefABCDEF";
  35. static void json_object_generic_delete(struct json_object* jso);
  36. static struct json_object* json_object_new(enum json_type o_type);
  37. static json_object_to_json_string_fn json_object_object_to_json_string;
  38. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  39. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  40. static json_object_to_json_string_fn json_object_int_to_json_string;
  41. static json_object_to_json_string_fn json_object_string_to_json_string;
  42. static json_object_to_json_string_fn json_object_array_to_json_string;
  43. /* ref count debugging */
  44. #ifdef REFCOUNT_DEBUG
  45. static struct lh_table *json_object_table;
  46. static void json_object_init(void) __attribute__ ((constructor));
  47. static void json_object_init(void) {
  48. MC_DEBUG("json_object_init: creating object table\n");
  49. json_object_table = lh_kptr_table_new(128, NULL);
  50. }
  51. static void json_object_fini(void) __attribute__ ((destructor));
  52. static void json_object_fini(void)
  53. {
  54. struct lh_entry *ent;
  55. if (MC_GET_DEBUG())
  56. {
  57. if (json_object_table->count)
  58. {
  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. {
  63. struct json_object* obj =
  64. (struct json_object*) lh_entry_v(ent);
  65. MC_DEBUG("\t%s:%p\n",
  66. json_type_to_name(obj->o_type), obj);
  67. }
  68. }
  69. }
  70. MC_DEBUG("json_object_fini: freeing object table\n");
  71. lh_table_free(json_object_table);
  72. }
  73. #endif /* REFCOUNT_DEBUG */
  74. /* helper for accessing the optimized string data component in json_object
  75. */
  76. static const char *
  77. get_string_component(const struct json_object *jso)
  78. {
  79. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  80. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  81. }
  82. /* string escaping */
  83. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  84. {
  85. int pos = 0, start_offset = 0;
  86. unsigned char c;
  87. while (len--)
  88. {
  89. c = str[pos];
  90. switch(c)
  91. {
  92. case '\b':
  93. case '\n':
  94. case '\r':
  95. case '\t':
  96. case '\f':
  97. case '"':
  98. case '\\':
  99. case '/':
  100. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  101. {
  102. pos++;
  103. break;
  104. }
  105. if(pos - start_offset > 0)
  106. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  107. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  108. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  109. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  110. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  111. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  112. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  113. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  114. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  115. start_offset = ++pos;
  116. break;
  117. default:
  118. if(c < ' ')
  119. {
  120. char sbuf[7];
  121. if(pos - start_offset > 0)
  122. printbuf_memappend(pb,
  123. str + start_offset,
  124. pos - start_offset);
  125. snprintf(sbuf, sizeof(sbuf),
  126. "\\u00%c%c",
  127. json_hex_chars[c >> 4],
  128. json_hex_chars[c & 0xf]);
  129. printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1);
  130. start_offset = ++pos;
  131. } else
  132. pos++;
  133. }
  134. }
  135. if (pos - start_offset > 0)
  136. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  137. return 0;
  138. }
  139. /* reference counting */
  140. extern struct json_object* json_object_get(struct json_object *jso)
  141. {
  142. if (!jso) return jso;
  143. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  144. __sync_add_and_fetch(&jso->_ref_count, 1);
  145. #else
  146. ++jso->_ref_count;
  147. #endif
  148. return jso;
  149. }
  150. int json_object_put(struct json_object *jso)
  151. {
  152. if(!jso) return 0;
  153. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  154. /* Note: this only allow the refcount to remain correct
  155. * when multiple threads are adjusting it. It is still an error
  156. * for a thread to decrement the refcount if it doesn't "own" it,
  157. * as that can result in the thread that loses the race to 0
  158. * operating on an already-freed object.
  159. */
  160. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
  161. #else
  162. if (--jso->_ref_count > 0) return 0;
  163. #endif
  164. if (jso->_user_delete)
  165. jso->_user_delete(jso, jso->_userdata);
  166. jso->_delete(jso);
  167. return 1;
  168. }
  169. /* generic object construction and destruction parts */
  170. static void json_object_generic_delete(struct json_object* jso)
  171. {
  172. #ifdef REFCOUNT_DEBUG
  173. MC_DEBUG("json_object_delete_%s: %p\n",
  174. json_type_to_name(jso->o_type), jso);
  175. lh_table_delete(json_object_table, jso);
  176. #endif /* REFCOUNT_DEBUG */
  177. printbuf_free(jso->_pb);
  178. free(jso);
  179. }
  180. static struct json_object* json_object_new(enum json_type o_type)
  181. {
  182. struct json_object *jso;
  183. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  184. if (!jso)
  185. return NULL;
  186. jso->o_type = o_type;
  187. jso->_ref_count = 1;
  188. jso->_delete = &json_object_generic_delete;
  189. #ifdef REFCOUNT_DEBUG
  190. lh_table_insert(json_object_table, jso, jso);
  191. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  192. #endif /* REFCOUNT_DEBUG */
  193. return jso;
  194. }
  195. /* type checking functions */
  196. int json_object_is_type(const struct json_object *jso, enum json_type type)
  197. {
  198. if (!jso)
  199. return (type == json_type_null);
  200. return (jso->o_type == type);
  201. }
  202. enum json_type json_object_get_type(const struct json_object *jso)
  203. {
  204. if (!jso)
  205. return json_type_null;
  206. return jso->o_type;
  207. }
  208. void* json_object_get_userdata(json_object *jso) {
  209. return jso ? jso->_userdata : NULL;
  210. }
  211. void json_object_set_userdata(json_object *jso, void *userdata,
  212. json_object_delete_fn *user_delete)
  213. {
  214. // Can't return failure, so abort if we can't perform the operation.
  215. assert(jso != NULL);
  216. // First, clean up any previously existing user info
  217. if (jso->_user_delete)
  218. jso->_user_delete(jso, jso->_userdata);
  219. jso->_userdata = userdata;
  220. jso->_user_delete = user_delete;
  221. }
  222. /* set a custom conversion to string */
  223. void json_object_set_serializer(json_object *jso,
  224. json_object_to_json_string_fn to_string_func,
  225. void *userdata,
  226. json_object_delete_fn *user_delete)
  227. {
  228. json_object_set_userdata(jso, userdata, user_delete);
  229. if (to_string_func == NULL)
  230. {
  231. // Reset to the standard serialization function
  232. switch(jso->o_type)
  233. {
  234. case json_type_null:
  235. jso->_to_json_string = NULL;
  236. break;
  237. case json_type_boolean:
  238. jso->_to_json_string = &json_object_boolean_to_json_string;
  239. break;
  240. case json_type_double:
  241. jso->_to_json_string = &json_object_double_to_json_string_default;
  242. break;
  243. case json_type_int:
  244. jso->_to_json_string = &json_object_int_to_json_string;
  245. break;
  246. case json_type_object:
  247. jso->_to_json_string = &json_object_object_to_json_string;
  248. break;
  249. case json_type_array:
  250. jso->_to_json_string = &json_object_array_to_json_string;
  251. break;
  252. case json_type_string:
  253. jso->_to_json_string = &json_object_string_to_json_string;
  254. break;
  255. }
  256. return;
  257. }
  258. jso->_to_json_string = to_string_func;
  259. }
  260. /* extended conversion to string */
  261. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  262. {
  263. const char *r = NULL;
  264. size_t s = 0;
  265. if (!jso)
  266. {
  267. s = 4;
  268. r = "null";
  269. }
  270. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  271. {
  272. printbuf_reset(jso->_pb);
  273. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  274. {
  275. s = (size_t)jso->_pb->bpos;
  276. r = jso->_pb->buf;
  277. }
  278. }
  279. if (length)
  280. *length = s;
  281. return r;
  282. }
  283. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  284. {
  285. return json_object_to_json_string_length(jso, flags, NULL);
  286. }
  287. /* backwards-compatible conversion to string */
  288. const char* json_object_to_json_string(struct json_object *jso)
  289. {
  290. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  291. }
  292. static void indent(struct printbuf *pb, int level, int flags)
  293. {
  294. if (flags & JSON_C_TO_STRING_PRETTY)
  295. {
  296. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  297. {
  298. printbuf_memset(pb, -1, '\t', level);
  299. }
  300. else
  301. {
  302. printbuf_memset(pb, -1, ' ', level * 2);
  303. }
  304. }
  305. }
  306. /* json_object_object */
  307. static int json_object_object_to_json_string(struct json_object* jso,
  308. struct printbuf *pb,
  309. int level,
  310. int flags)
  311. {
  312. int had_children = 0;
  313. struct json_object_iter iter;
  314. printbuf_strappend(pb, "{" /*}*/);
  315. if (flags & JSON_C_TO_STRING_PRETTY)
  316. printbuf_strappend(pb, "\n");
  317. json_object_object_foreachC(jso, iter)
  318. {
  319. if (had_children)
  320. {
  321. printbuf_strappend(pb, ",");
  322. if (flags & JSON_C_TO_STRING_PRETTY)
  323. printbuf_strappend(pb, "\n");
  324. }
  325. had_children = 1;
  326. if (flags & JSON_C_TO_STRING_SPACED)
  327. printbuf_strappend(pb, " ");
  328. indent(pb, level+1, flags);
  329. printbuf_strappend(pb, "\"");
  330. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  331. if (flags & JSON_C_TO_STRING_SPACED)
  332. printbuf_strappend(pb, "\": ");
  333. else
  334. printbuf_strappend(pb, "\":");
  335. if(iter.val == NULL)
  336. printbuf_strappend(pb, "null");
  337. else
  338. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  339. return -1;
  340. }
  341. if (flags & JSON_C_TO_STRING_PRETTY)
  342. {
  343. if (had_children)
  344. printbuf_strappend(pb, "\n");
  345. indent(pb,level,flags);
  346. }
  347. if (flags & JSON_C_TO_STRING_SPACED)
  348. return printbuf_strappend(pb, /*{*/ " }");
  349. else
  350. return printbuf_strappend(pb, /*{*/ "}");
  351. }
  352. static void json_object_lh_entry_free(struct lh_entry *ent)
  353. {
  354. if (!ent->k_is_constant)
  355. free(lh_entry_k(ent));
  356. json_object_put((struct json_object*)lh_entry_v(ent));
  357. }
  358. static void json_object_object_delete(struct json_object* jso)
  359. {
  360. lh_table_free(jso->o.c_object);
  361. json_object_generic_delete(jso);
  362. }
  363. struct json_object* json_object_new_object(void)
  364. {
  365. struct json_object *jso = json_object_new(json_type_object);
  366. if (!jso)
  367. return NULL;
  368. jso->_delete = &json_object_object_delete;
  369. jso->_to_json_string = &json_object_object_to_json_string;
  370. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  371. &json_object_lh_entry_free);
  372. if (!jso->o.c_object)
  373. {
  374. json_object_generic_delete(jso);
  375. errno = ENOMEM;
  376. return NULL;
  377. }
  378. return jso;
  379. }
  380. struct lh_table* json_object_get_object(const struct json_object *jso)
  381. {
  382. if (!jso)
  383. return NULL;
  384. switch(jso->o_type)
  385. {
  386. case json_type_object:
  387. return jso->o.c_object;
  388. default:
  389. return NULL;
  390. }
  391. }
  392. int json_object_object_add_ex(struct json_object* jso,
  393. const char *const key,
  394. struct json_object *const val,
  395. const unsigned opts)
  396. {
  397. struct json_object *existing_value = NULL;
  398. struct lh_entry *existing_entry;
  399. unsigned long hash;
  400. assert(json_object_get_type(jso) == json_type_object);
  401. // We lookup the entry and replace the value, rather than just deleting
  402. // and re-adding it, so the existing key remains valid.
  403. hash = lh_get_hash(jso->o.c_object, (const void *)key);
  404. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  405. lh_table_lookup_entry_w_hash(jso->o.c_object,
  406. (const void *)key, hash);
  407. // The caller must avoid creating loops in the object tree, but do a
  408. // quick check anyway to make sure we're not creating a trivial loop.
  409. if (jso == val)
  410. return -1;
  411. if (!existing_entry)
  412. {
  413. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  414. (const void *)key : strdup(key);
  415. if (k == NULL)
  416. return -1;
  417. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  418. }
  419. existing_value = (json_object *) lh_entry_v(existing_entry);
  420. if (existing_value)
  421. json_object_put(existing_value);
  422. existing_entry->v = val;
  423. return 0;
  424. }
  425. int json_object_object_add(struct json_object* jso, const char *key,
  426. struct json_object *val)
  427. {
  428. return json_object_object_add_ex(jso, key, val, 0);
  429. }
  430. int json_object_object_length(const struct json_object *jso)
  431. {
  432. assert(json_object_get_type(jso) == json_type_object);
  433. return lh_table_length(jso->o.c_object);
  434. }
  435. struct json_object* json_object_object_get(const struct json_object* jso,
  436. const char *key)
  437. {
  438. struct json_object *result = NULL;
  439. json_object_object_get_ex(jso, key, &result);
  440. return result;
  441. }
  442. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  443. struct json_object **value)
  444. {
  445. if (value != NULL)
  446. *value = NULL;
  447. if (NULL == jso)
  448. return FALSE;
  449. switch(jso->o_type)
  450. {
  451. case json_type_object:
  452. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  453. (void**) value);
  454. default:
  455. if (value != NULL)
  456. *value = NULL;
  457. return FALSE;
  458. }
  459. }
  460. void json_object_object_del(struct json_object* jso, const char *key)
  461. {
  462. assert(json_object_get_type(jso) == json_type_object);
  463. lh_table_delete(jso->o.c_object, key);
  464. }
  465. /* json_object_boolean */
  466. static int json_object_boolean_to_json_string(struct json_object* jso,
  467. struct printbuf *pb,
  468. int level,
  469. int flags)
  470. {
  471. if (jso->o.c_boolean)
  472. return printbuf_strappend(pb, "true");
  473. return printbuf_strappend(pb, "false");
  474. }
  475. struct json_object* json_object_new_boolean(json_bool b)
  476. {
  477. struct json_object *jso = json_object_new(json_type_boolean);
  478. if (!jso)
  479. return NULL;
  480. jso->_to_json_string = &json_object_boolean_to_json_string;
  481. jso->o.c_boolean = b;
  482. return jso;
  483. }
  484. json_bool json_object_get_boolean(const struct json_object *jso)
  485. {
  486. if (!jso)
  487. return FALSE;
  488. switch(jso->o_type)
  489. {
  490. case json_type_boolean:
  491. return jso->o.c_boolean;
  492. case json_type_int:
  493. return (jso->o.c_int64 != 0);
  494. case json_type_double:
  495. return (jso->o.c_double != 0);
  496. case json_type_string:
  497. return (jso->o.c_string.len != 0);
  498. default:
  499. return FALSE;
  500. }
  501. }
  502. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  503. if (!jso || jso->o_type!=json_type_boolean)
  504. return 0;
  505. jso->o.c_boolean=new_value;
  506. return 1;
  507. }
  508. /* json_object_int */
  509. static int json_object_int_to_json_string(struct json_object* jso,
  510. struct printbuf *pb,
  511. int level,
  512. int flags)
  513. {
  514. /* room for 19 digits, the sign char, and a null term */
  515. char sbuf[21];
  516. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
  517. return printbuf_memappend (pb, sbuf, strlen(sbuf));
  518. }
  519. struct json_object* json_object_new_int(int32_t i)
  520. {
  521. struct json_object *jso = json_object_new(json_type_int);
  522. if (!jso)
  523. return NULL;
  524. jso->_to_json_string = &json_object_int_to_json_string;
  525. jso->o.c_int64 = i;
  526. return jso;
  527. }
  528. int32_t json_object_get_int(const struct json_object *jso)
  529. {
  530. int64_t cint64;
  531. enum json_type o_type;
  532. if(!jso) return 0;
  533. o_type = jso->o_type;
  534. cint64 = jso->o.c_int64;
  535. if (o_type == json_type_string)
  536. {
  537. /*
  538. * Parse strings into 64-bit numbers, then use the
  539. * 64-to-32-bit number handling below.
  540. */
  541. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  542. return 0; /* whoops, it didn't work. */
  543. o_type = json_type_int;
  544. }
  545. switch(o_type) {
  546. case json_type_int:
  547. /* Make sure we return the correct values for out of range numbers. */
  548. if (cint64 <= INT32_MIN)
  549. return INT32_MIN;
  550. if (cint64 >= INT32_MAX)
  551. return INT32_MAX;
  552. return (int32_t) cint64;
  553. case json_type_double:
  554. if (jso->o.c_double <= INT32_MIN)
  555. return INT32_MIN;
  556. if (jso->o.c_double >= INT32_MAX)
  557. return INT32_MAX;
  558. return (int32_t)jso->o.c_double;
  559. case json_type_boolean:
  560. return jso->o.c_boolean;
  561. default:
  562. return 0;
  563. }
  564. }
  565. int json_object_set_int(struct json_object *jso,int new_value){
  566. if (!jso || jso->o_type!=json_type_int)
  567. return 0;
  568. jso->o.c_int64=new_value;
  569. return 1;
  570. }
  571. struct json_object* json_object_new_int64(int64_t i)
  572. {
  573. struct json_object *jso = json_object_new(json_type_int);
  574. if (!jso)
  575. return NULL;
  576. jso->_to_json_string = &json_object_int_to_json_string;
  577. jso->o.c_int64 = i;
  578. return jso;
  579. }
  580. int64_t json_object_get_int64(const struct json_object *jso)
  581. {
  582. int64_t cint;
  583. if (!jso)
  584. return 0;
  585. switch(jso->o_type)
  586. {
  587. case json_type_int:
  588. return jso->o.c_int64;
  589. case json_type_double:
  590. return (int64_t)jso->o.c_double;
  591. case json_type_boolean:
  592. return jso->o.c_boolean;
  593. case json_type_string:
  594. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  595. return cint;
  596. /* FALLTHRU */
  597. default:
  598. return 0;
  599. }
  600. }
  601. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  602. if (!jso || jso->o_type!=json_type_int)
  603. return 0;
  604. jso->o.c_int64=new_value;
  605. return 1;
  606. }
  607. /* json_object_double */
  608. #if defined(HAVE___THREAD)
  609. // i.e. __thread or __declspec(thread)
  610. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  611. #endif
  612. static char *global_serialization_float_format = NULL;
  613. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  614. {
  615. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  616. {
  617. #if defined(HAVE___THREAD)
  618. if (tls_serialization_float_format)
  619. {
  620. free(tls_serialization_float_format);
  621. tls_serialization_float_format = NULL;
  622. }
  623. #endif
  624. if (global_serialization_float_format)
  625. free(global_serialization_float_format);
  626. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  627. }
  628. else if (global_or_thread == JSON_C_OPTION_THREAD)
  629. {
  630. #if defined(HAVE___THREAD)
  631. if (tls_serialization_float_format)
  632. {
  633. free(tls_serialization_float_format);
  634. tls_serialization_float_format = NULL;
  635. }
  636. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  637. #else
  638. _set_last_err("json_c_set_option: not compiled with __thread support\n");
  639. return -1;
  640. #endif
  641. }
  642. else
  643. {
  644. _set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  645. return -1;
  646. }
  647. return 0;
  648. }
  649. static int json_object_double_to_json_string_format(struct json_object* jso,
  650. struct printbuf *pb,
  651. int level,
  652. int flags,
  653. const char *format)
  654. {
  655. char buf[128], *p, *q;
  656. int size;
  657. double dummy; /* needed for modf() */
  658. /* Although JSON RFC does not support
  659. NaN or Infinity as numeric values
  660. ECMA 262 section 9.8.1 defines
  661. how to handle these cases as strings */
  662. if (isnan(jso->o.c_double))
  663. {
  664. size = snprintf(buf, sizeof(buf), "NaN");
  665. }
  666. else if (isinf(jso->o.c_double))
  667. {
  668. if(jso->o.c_double > 0)
  669. size = snprintf(buf, sizeof(buf), "Infinity");
  670. else
  671. size = snprintf(buf, sizeof(buf), "-Infinity");
  672. }
  673. else
  674. {
  675. const char *std_format = "%.17g";
  676. #if defined(HAVE___THREAD)
  677. if (tls_serialization_float_format)
  678. std_format = tls_serialization_float_format;
  679. else
  680. #endif
  681. if (global_serialization_float_format)
  682. std_format = global_serialization_float_format;
  683. if (!format)
  684. format = std_format;
  685. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  686. if (modf(jso->o.c_double, &dummy) == 0 && size >= 0 && size < (int)sizeof(buf) - 2)
  687. {
  688. // Ensure it looks like a float, even if snprintf didn't.
  689. strcat(buf, ".0");
  690. size += 2;
  691. }
  692. }
  693. // although unlikely, snprintf can fail
  694. if (size < 0)
  695. return -1;
  696. p = strchr(buf, ',');
  697. if (p)
  698. *p = '.';
  699. else
  700. p = strchr(buf, '.');
  701. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  702. {
  703. /* last useful digit, always keep 1 zero */
  704. p++;
  705. for (q=p ; *q ; q++) {
  706. if (*q!='0') p=q;
  707. }
  708. /* drop trailing zeroes */
  709. *(++p) = 0;
  710. size = p-buf;
  711. }
  712. if (size >= (int)sizeof(buf))
  713. // The standard formats are guaranteed not to overrun the buffer,
  714. // but if a custom one happens to do so, just silently truncate.
  715. size = sizeof(buf) - 1;
  716. printbuf_memappend(pb, buf, size);
  717. return size;
  718. }
  719. static int json_object_double_to_json_string_default(struct json_object* jso,
  720. struct printbuf *pb,
  721. int level,
  722. int flags)
  723. {
  724. return json_object_double_to_json_string_format(jso, pb, level, flags,
  725. NULL);
  726. }
  727. int json_object_double_to_json_string(struct json_object* jso,
  728. struct printbuf *pb,
  729. int level,
  730. int flags)
  731. {
  732. return json_object_double_to_json_string_format(jso, pb, level, flags,
  733. (const char *)jso->_userdata);
  734. }
  735. struct json_object* json_object_new_double(double d)
  736. {
  737. struct json_object *jso = json_object_new(json_type_double);
  738. if (!jso)
  739. return NULL;
  740. jso->_to_json_string = &json_object_double_to_json_string_default;
  741. jso->o.c_double = d;
  742. return jso;
  743. }
  744. struct json_object* json_object_new_double_s(double d, const char *ds)
  745. {
  746. char *new_ds;
  747. struct json_object *jso = json_object_new_double(d);
  748. if (!jso)
  749. return NULL;
  750. new_ds = strdup(ds);
  751. if (!new_ds)
  752. {
  753. json_object_generic_delete(jso);
  754. errno = ENOMEM;
  755. return NULL;
  756. }
  757. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  758. new_ds, json_object_free_userdata);
  759. return jso;
  760. }
  761. int json_object_userdata_to_json_string(struct json_object *jso,
  762. struct printbuf *pb, int level, int flags)
  763. {
  764. int userdata_len = strlen((const char *)jso->_userdata);
  765. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  766. return userdata_len;
  767. }
  768. void json_object_free_userdata(struct json_object *jso, void *userdata)
  769. {
  770. free(userdata);
  771. }
  772. double json_object_get_double(const struct json_object *jso)
  773. {
  774. double cdouble;
  775. char *errPtr = NULL;
  776. if(!jso) return 0.0;
  777. switch(jso->o_type) {
  778. case json_type_double:
  779. return jso->o.c_double;
  780. case json_type_int:
  781. return jso->o.c_int64;
  782. case json_type_boolean:
  783. return jso->o.c_boolean;
  784. case json_type_string:
  785. errno = 0;
  786. cdouble = strtod(get_string_component(jso), &errPtr);
  787. /* if conversion stopped at the first character, return 0.0 */
  788. if (errPtr == get_string_component(jso))
  789. return 0.0;
  790. /*
  791. * Check that the conversion terminated on something sensible
  792. *
  793. * For example, { "pay" : 123AB } would parse as 123.
  794. */
  795. if (*errPtr != '\0')
  796. return 0.0;
  797. /*
  798. * If strtod encounters a string which would exceed the
  799. * capacity of a double, it returns +/- HUGE_VAL and sets
  800. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  801. * from a conversion, so we need to check errno.
  802. *
  803. * Underflow also sets errno to ERANGE, but it returns 0 in
  804. * that case, which is what we will return anyway.
  805. *
  806. * See CERT guideline ERR30-C
  807. */
  808. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  809. (ERANGE == errno))
  810. cdouble = 0.0;
  811. return cdouble;
  812. default:
  813. return 0.0;
  814. }
  815. }
  816. int json_object_set_double(struct json_object *jso,double new_value){
  817. if (!jso || jso->o_type!=json_type_double)
  818. return 0;
  819. jso->o.c_double=new_value;
  820. return 1;
  821. }
  822. /* json_object_string */
  823. static int json_object_string_to_json_string(struct json_object* jso,
  824. struct printbuf *pb,
  825. int level,
  826. int flags)
  827. {
  828. printbuf_strappend(pb, "\"");
  829. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  830. printbuf_strappend(pb, "\"");
  831. return 0;
  832. }
  833. static void json_object_string_delete(struct json_object* jso)
  834. {
  835. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  836. free(jso->o.c_string.str.ptr);
  837. json_object_generic_delete(jso);
  838. }
  839. struct json_object* json_object_new_string(const char *s)
  840. {
  841. struct json_object *jso = json_object_new(json_type_string);
  842. if (!jso)
  843. return NULL;
  844. jso->_delete = &json_object_string_delete;
  845. jso->_to_json_string = &json_object_string_to_json_string;
  846. jso->o.c_string.len = strlen(s);
  847. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  848. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  849. } else {
  850. jso->o.c_string.str.ptr = strdup(s);
  851. if (!jso->o.c_string.str.ptr)
  852. {
  853. json_object_generic_delete(jso);
  854. errno = ENOMEM;
  855. return NULL;
  856. }
  857. }
  858. return jso;
  859. }
  860. struct json_object* json_object_new_string_len(const char *s, int len)
  861. {
  862. char *dstbuf;
  863. struct json_object *jso = json_object_new(json_type_string);
  864. if (!jso)
  865. return NULL;
  866. jso->_delete = &json_object_string_delete;
  867. jso->_to_json_string = &json_object_string_to_json_string;
  868. if(len < LEN_DIRECT_STRING_DATA) {
  869. dstbuf = jso->o.c_string.str.data;
  870. } else {
  871. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  872. if (!jso->o.c_string.str.ptr)
  873. {
  874. json_object_generic_delete(jso);
  875. errno = ENOMEM;
  876. return NULL;
  877. }
  878. dstbuf = jso->o.c_string.str.ptr;
  879. }
  880. memcpy(dstbuf, (const void *)s, len);
  881. dstbuf[len] = '\0';
  882. jso->o.c_string.len = len;
  883. return jso;
  884. }
  885. const char* json_object_get_string(struct json_object *jso)
  886. {
  887. if (!jso)
  888. return NULL;
  889. switch(jso->o_type)
  890. {
  891. case json_type_string:
  892. return get_string_component(jso);
  893. default:
  894. return json_object_to_json_string(jso);
  895. }
  896. }
  897. int json_object_get_string_len(const struct json_object *jso)
  898. {
  899. if (!jso)
  900. return 0;
  901. switch(jso->o_type)
  902. {
  903. case json_type_string:
  904. return jso->o.c_string.len;
  905. default:
  906. return 0;
  907. }
  908. }
  909. int json_object_set_string(json_object* jso, const char* s) {
  910. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  911. }
  912. int json_object_set_string_len(json_object* jso, const char* s, int len){
  913. char *dstbuf;
  914. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  915. if (len<LEN_DIRECT_STRING_DATA) {
  916. dstbuf=jso->o.c_string.str.data;
  917. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  918. } else {
  919. dstbuf=(char *)malloc(len+1);
  920. if (dstbuf==NULL) return 0;
  921. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  922. jso->o.c_string.str.ptr=dstbuf;
  923. }
  924. jso->o.c_string.len=len;
  925. memcpy(dstbuf, (const void *)s, len);
  926. dstbuf[len] = '\0';
  927. return 1;
  928. }
  929. /* json_object_array */
  930. static int json_object_array_to_json_string(struct json_object* jso,
  931. struct printbuf *pb,
  932. int level,
  933. int flags)
  934. {
  935. int had_children = 0;
  936. size_t ii;
  937. printbuf_strappend(pb, "[");
  938. if (flags & JSON_C_TO_STRING_PRETTY)
  939. printbuf_strappend(pb, "\n");
  940. for(ii=0; ii < json_object_array_length(jso); ii++)
  941. {
  942. struct json_object *val;
  943. if (had_children)
  944. {
  945. printbuf_strappend(pb, ",");
  946. if (flags & JSON_C_TO_STRING_PRETTY)
  947. printbuf_strappend(pb, "\n");
  948. }
  949. had_children = 1;
  950. if (flags & JSON_C_TO_STRING_SPACED)
  951. printbuf_strappend(pb, " ");
  952. indent(pb, level + 1, flags);
  953. val = json_object_array_get_idx(jso, ii);
  954. if(val == NULL)
  955. printbuf_strappend(pb, "null");
  956. else
  957. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  958. return -1;
  959. }
  960. if (flags & JSON_C_TO_STRING_PRETTY)
  961. {
  962. if (had_children)
  963. printbuf_strappend(pb, "\n");
  964. indent(pb,level,flags);
  965. }
  966. if (flags & JSON_C_TO_STRING_SPACED)
  967. return printbuf_strappend(pb, " ]");
  968. return printbuf_strappend(pb, "]");
  969. }
  970. static void json_object_array_entry_free(void *data)
  971. {
  972. json_object_put((struct json_object*)data);
  973. }
  974. static void json_object_array_delete(struct json_object* jso)
  975. {
  976. array_list_free(jso->o.c_array);
  977. json_object_generic_delete(jso);
  978. }
  979. struct json_object* json_object_new_array(void)
  980. {
  981. struct json_object *jso = json_object_new(json_type_array);
  982. if (!jso)
  983. return NULL;
  984. jso->_delete = &json_object_array_delete;
  985. jso->_to_json_string = &json_object_array_to_json_string;
  986. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  987. if(jso->o.c_array == NULL)
  988. {
  989. free(jso);
  990. return NULL;
  991. }
  992. return jso;
  993. }
  994. struct array_list* json_object_get_array(const struct json_object *jso)
  995. {
  996. if (!jso)
  997. return NULL;
  998. switch(jso->o_type)
  999. {
  1000. case json_type_array:
  1001. return jso->o.c_array;
  1002. default:
  1003. return NULL;
  1004. }
  1005. }
  1006. void json_object_array_sort(struct json_object *jso,
  1007. int(*sort_fn)(const void *, const void *))
  1008. {
  1009. assert(json_object_get_type(jso) == json_type_array);
  1010. array_list_sort(jso->o.c_array, sort_fn);
  1011. }
  1012. struct json_object* json_object_array_bsearch(
  1013. const struct json_object *key,
  1014. const struct json_object *jso,
  1015. int (*sort_fn)(const void *, const void *))
  1016. {
  1017. struct json_object **result;
  1018. assert(json_object_get_type(jso) == json_type_array);
  1019. result = (struct json_object **)array_list_bsearch(
  1020. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1021. if (!result)
  1022. return NULL;
  1023. return *result;
  1024. }
  1025. size_t json_object_array_length(const struct json_object *jso)
  1026. {
  1027. assert(json_object_get_type(jso) == json_type_array);
  1028. return array_list_length(jso->o.c_array);
  1029. }
  1030. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1031. {
  1032. assert(json_object_get_type(jso) == json_type_array);
  1033. return array_list_add(jso->o.c_array, val);
  1034. }
  1035. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1036. struct json_object *val)
  1037. {
  1038. assert(json_object_get_type(jso) == json_type_array);
  1039. return array_list_put_idx(jso->o.c_array, idx, val);
  1040. }
  1041. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1042. {
  1043. assert(json_object_get_type(jso) == json_type_array);
  1044. return array_list_del_idx(jso->o.c_array, idx, count);
  1045. }
  1046. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1047. size_t idx)
  1048. {
  1049. assert(json_object_get_type(jso) == json_type_array);
  1050. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1051. }
  1052. static int json_array_equal(struct json_object* jso1,
  1053. struct json_object* jso2)
  1054. {
  1055. size_t len, i;
  1056. len = json_object_array_length(jso1);
  1057. if (len != json_object_array_length(jso2))
  1058. return 0;
  1059. for (i = 0; i < len; i++) {
  1060. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1061. json_object_array_get_idx(jso2, i)))
  1062. return 0;
  1063. }
  1064. return 1;
  1065. }
  1066. static int json_object_all_values_equal(struct json_object* jso1,
  1067. struct json_object* jso2)
  1068. {
  1069. struct json_object_iter iter;
  1070. struct json_object *sub;
  1071. assert(json_object_get_type(jso1) == json_type_object);
  1072. assert(json_object_get_type(jso2) == json_type_object);
  1073. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1074. json_object_object_foreachC(jso1, iter) {
  1075. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1076. (void**)(void *)&sub))
  1077. return 0;
  1078. if (!json_object_equal(iter.val, sub))
  1079. return 0;
  1080. }
  1081. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1082. json_object_object_foreachC(jso2, iter) {
  1083. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1084. (void**)(void *)&sub))
  1085. return 0;
  1086. }
  1087. return 1;
  1088. }
  1089. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1090. {
  1091. if (jso1 == jso2)
  1092. return 1;
  1093. if (!jso1 || !jso2)
  1094. return 0;
  1095. if (jso1->o_type != jso2->o_type)
  1096. return 0;
  1097. switch(jso1->o_type) {
  1098. case json_type_boolean:
  1099. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1100. case json_type_double:
  1101. return (jso1->o.c_double == jso2->o.c_double);
  1102. case json_type_int:
  1103. return (jso1->o.c_int64 == jso2->o.c_int64);
  1104. case json_type_string:
  1105. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1106. memcmp(get_string_component(jso1),
  1107. get_string_component(jso2),
  1108. jso1->o.c_string.len) == 0);
  1109. case json_type_object:
  1110. return json_object_all_values_equal(jso1, jso2);
  1111. case json_type_array:
  1112. return json_array_equal(jso1, jso2);
  1113. case json_type_null:
  1114. return 1;
  1115. };
  1116. return 0;
  1117. }