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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. #ifdef HAVE_ATOMIC_BUILTINS
  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. #ifdef HAVE_ATOMIC_BUILTINS
  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. assert(json_object_get_type(jso) == json_type_object);
  398. // We lookup the entry and replace the value, rather than just deleting
  399. // and re-adding it, so the existing key remains valid.
  400. json_object *existing_value = NULL;
  401. struct lh_entry *existing_entry;
  402. const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
  403. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  404. lh_table_lookup_entry_w_hash(jso->o.c_object,
  405. (const void *)key, hash);
  406. // The caller must avoid creating loops in the object tree, but do a
  407. // quick check anyway to make sure we're not creating a trivial loop.
  408. if (jso == val)
  409. return -1;
  410. if (!existing_entry)
  411. {
  412. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  413. (const void *)key : strdup(key);
  414. if (k == NULL)
  415. return -1;
  416. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  417. }
  418. existing_value = (json_object *) lh_entry_v(existing_entry);
  419. if (existing_value)
  420. json_object_put(existing_value);
  421. existing_entry->v = val;
  422. return 0;
  423. }
  424. int json_object_object_add(struct json_object* jso, const char *key,
  425. struct json_object *val)
  426. {
  427. return json_object_object_add_ex(jso, key, val, 0);
  428. }
  429. int json_object_object_length(const struct json_object *jso)
  430. {
  431. assert(json_object_get_type(jso) == json_type_object);
  432. return lh_table_length(jso->o.c_object);
  433. }
  434. struct json_object* json_object_object_get(const struct json_object* jso,
  435. const char *key)
  436. {
  437. struct json_object *result = NULL;
  438. json_object_object_get_ex(jso, key, &result);
  439. return result;
  440. }
  441. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  442. struct json_object **value)
  443. {
  444. if (value != NULL)
  445. *value = NULL;
  446. if (NULL == jso)
  447. return FALSE;
  448. switch(jso->o_type)
  449. {
  450. case json_type_object:
  451. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  452. (void**) value);
  453. default:
  454. if (value != NULL)
  455. *value = NULL;
  456. return FALSE;
  457. }
  458. }
  459. void json_object_object_del(struct json_object* jso, const char *key)
  460. {
  461. assert(json_object_get_type(jso) == json_type_object);
  462. lh_table_delete(jso->o.c_object, key);
  463. }
  464. /* json_object_boolean */
  465. static int json_object_boolean_to_json_string(struct json_object* jso,
  466. struct printbuf *pb,
  467. int level,
  468. int flags)
  469. {
  470. if (jso->o.c_boolean)
  471. return printbuf_strappend(pb, "true");
  472. return printbuf_strappend(pb, "false");
  473. }
  474. struct json_object* json_object_new_boolean(json_bool b)
  475. {
  476. struct json_object *jso = json_object_new(json_type_boolean);
  477. if (!jso)
  478. return NULL;
  479. jso->_to_json_string = &json_object_boolean_to_json_string;
  480. jso->o.c_boolean = b;
  481. return jso;
  482. }
  483. json_bool json_object_get_boolean(const struct json_object *jso)
  484. {
  485. if (!jso)
  486. return FALSE;
  487. switch(jso->o_type)
  488. {
  489. case json_type_boolean:
  490. return jso->o.c_boolean;
  491. case json_type_int:
  492. return (jso->o.c_int64 != 0);
  493. case json_type_double:
  494. return (jso->o.c_double != 0);
  495. case json_type_string:
  496. return (jso->o.c_string.len != 0);
  497. default:
  498. return FALSE;
  499. }
  500. }
  501. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  502. if (!jso || jso->o_type!=json_type_boolean)
  503. return 0;
  504. jso->o.c_boolean=new_value;
  505. return 1;
  506. }
  507. /* json_object_int */
  508. static int json_object_int_to_json_string(struct json_object* jso,
  509. struct printbuf *pb,
  510. int level,
  511. int flags)
  512. {
  513. /* room for 19 digits, the sign char, and a null term */
  514. char sbuf[21];
  515. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
  516. return printbuf_memappend (pb, sbuf, strlen(sbuf));
  517. }
  518. struct json_object* json_object_new_int(int32_t i)
  519. {
  520. struct json_object *jso = json_object_new(json_type_int);
  521. if (!jso)
  522. return NULL;
  523. jso->_to_json_string = &json_object_int_to_json_string;
  524. jso->o.c_int64 = i;
  525. return jso;
  526. }
  527. int32_t json_object_get_int(const struct json_object *jso)
  528. {
  529. int64_t cint64;
  530. enum json_type o_type;
  531. if(!jso) return 0;
  532. o_type = jso->o_type;
  533. cint64 = jso->o.c_int64;
  534. if (o_type == json_type_string)
  535. {
  536. /*
  537. * Parse strings into 64-bit numbers, then use the
  538. * 64-to-32-bit number handling below.
  539. */
  540. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  541. return 0; /* whoops, it didn't work. */
  542. o_type = json_type_int;
  543. }
  544. switch(o_type) {
  545. case json_type_int:
  546. /* Make sure we return the correct values for out of range numbers. */
  547. if (cint64 <= INT32_MIN)
  548. return INT32_MIN;
  549. if (cint64 >= INT32_MAX)
  550. return INT32_MAX;
  551. return (int32_t) cint64;
  552. case json_type_double:
  553. if (jso->o.c_double <= INT32_MIN)
  554. return INT32_MIN;
  555. if (jso->o.c_double >= INT32_MAX)
  556. return INT32_MAX;
  557. return (int32_t)jso->o.c_double;
  558. case json_type_boolean:
  559. return jso->o.c_boolean;
  560. default:
  561. return 0;
  562. }
  563. }
  564. int json_object_set_int(struct json_object *jso,int new_value){
  565. if (!jso || jso->o_type!=json_type_int)
  566. return 0;
  567. jso->o.c_int64=new_value;
  568. return 1;
  569. }
  570. struct json_object* json_object_new_int64(int64_t i)
  571. {
  572. struct json_object *jso = json_object_new(json_type_int);
  573. if (!jso)
  574. return NULL;
  575. jso->_to_json_string = &json_object_int_to_json_string;
  576. jso->o.c_int64 = i;
  577. return jso;
  578. }
  579. int64_t json_object_get_int64(const struct json_object *jso)
  580. {
  581. int64_t cint;
  582. if (!jso)
  583. return 0;
  584. switch(jso->o_type)
  585. {
  586. case json_type_int:
  587. return jso->o.c_int64;
  588. case json_type_double:
  589. return (int64_t)jso->o.c_double;
  590. case json_type_boolean:
  591. return jso->o.c_boolean;
  592. case json_type_string:
  593. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  594. return cint;
  595. /* FALLTHRU */
  596. default:
  597. return 0;
  598. }
  599. }
  600. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  601. if (!jso || jso->o_type!=json_type_int)
  602. return 0;
  603. jso->o.c_int64=new_value;
  604. return 1;
  605. }
  606. /* json_object_double */
  607. #ifdef HAVE___THREAD
  608. // i.e. __thread or __declspec(thread)
  609. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  610. #endif
  611. static char *global_serialization_float_format = NULL;
  612. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  613. {
  614. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  615. {
  616. #ifdef HAVE___THREAD
  617. if (tls_serialization_float_format)
  618. {
  619. free(tls_serialization_float_format);
  620. tls_serialization_float_format = NULL;
  621. }
  622. #endif
  623. if (global_serialization_float_format)
  624. free(global_serialization_float_format);
  625. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  626. }
  627. else if (global_or_thread == JSON_C_OPTION_THREAD)
  628. {
  629. #ifdef HAVE___THREAD
  630. if (tls_serialization_float_format)
  631. {
  632. free(tls_serialization_float_format);
  633. tls_serialization_float_format = NULL;
  634. }
  635. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  636. #else
  637. _set_last_err("json_c_set_option: not compiled with __thread support\n");
  638. return -1;
  639. #endif
  640. }
  641. else
  642. {
  643. _set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  644. return -1;
  645. }
  646. return 0;
  647. }
  648. static int json_object_double_to_json_string_format(struct json_object* jso,
  649. struct printbuf *pb,
  650. int level,
  651. int flags,
  652. const char *format)
  653. {
  654. char buf[128], *p, *q;
  655. int size;
  656. double dummy; /* needed for modf() */
  657. /* Although JSON RFC does not support
  658. NaN or Infinity as numeric values
  659. ECMA 262 section 9.8.1 defines
  660. how to handle these cases as strings */
  661. if (isnan(jso->o.c_double))
  662. {
  663. size = snprintf(buf, sizeof(buf), "NaN");
  664. }
  665. else if (isinf(jso->o.c_double))
  666. {
  667. if(jso->o.c_double > 0)
  668. size = snprintf(buf, sizeof(buf), "Infinity");
  669. else
  670. size = snprintf(buf, sizeof(buf), "-Infinity");
  671. }
  672. else
  673. {
  674. const char *std_format = "%.17g";
  675. #ifdef HAVE___THREAD
  676. if (tls_serialization_float_format)
  677. std_format = tls_serialization_float_format;
  678. else
  679. #endif
  680. if (global_serialization_float_format)
  681. std_format = global_serialization_float_format;
  682. if (!format)
  683. format = std_format;
  684. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  685. if (modf(jso->o.c_double, &dummy) == 0 && size >= 0 && size < (int)sizeof(buf) - 2)
  686. {
  687. // Ensure it looks like a float, even if snprintf didn't.
  688. strcat(buf, ".0");
  689. size += 2;
  690. }
  691. }
  692. // although unlikely, snprintf can fail
  693. if (size < 0)
  694. return -1;
  695. p = strchr(buf, ',');
  696. if (p)
  697. *p = '.';
  698. else
  699. p = strchr(buf, '.');
  700. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  701. {
  702. /* last useful digit, always keep 1 zero */
  703. p++;
  704. for (q=p ; *q ; q++) {
  705. if (*q!='0') p=q;
  706. }
  707. /* drop trailing zeroes */
  708. *(++p) = 0;
  709. size = p-buf;
  710. }
  711. if (size >= (int)sizeof(buf))
  712. // The standard formats are guaranteed not to overrun the buffer,
  713. // but if a custom one happens to do so, just silently truncate.
  714. size = sizeof(buf) - 1;
  715. printbuf_memappend(pb, buf, size);
  716. return size;
  717. }
  718. static int json_object_double_to_json_string_default(struct json_object* jso,
  719. struct printbuf *pb,
  720. int level,
  721. int flags)
  722. {
  723. return json_object_double_to_json_string_format(jso, pb, level, flags,
  724. NULL);
  725. }
  726. int json_object_double_to_json_string(struct json_object* jso,
  727. struct printbuf *pb,
  728. int level,
  729. int flags)
  730. {
  731. return json_object_double_to_json_string_format(jso, pb, level, flags,
  732. (const char *)jso->_userdata);
  733. }
  734. struct json_object* json_object_new_double(double d)
  735. {
  736. struct json_object *jso = json_object_new(json_type_double);
  737. if (!jso)
  738. return NULL;
  739. jso->_to_json_string = &json_object_double_to_json_string_default;
  740. jso->o.c_double = d;
  741. return jso;
  742. }
  743. struct json_object* json_object_new_double_s(double d, const char *ds)
  744. {
  745. struct json_object *jso = json_object_new_double(d);
  746. if (!jso)
  747. return NULL;
  748. char *new_ds = strdup(ds);
  749. if (!new_ds)
  750. {
  751. json_object_generic_delete(jso);
  752. errno = ENOMEM;
  753. return NULL;
  754. }
  755. json_object_set_serializer(jso, json_object_userdata_to_json_string,
  756. new_ds, json_object_free_userdata);
  757. return jso;
  758. }
  759. int json_object_userdata_to_json_string(struct json_object *jso,
  760. struct printbuf *pb, int level, int flags)
  761. {
  762. int userdata_len = strlen((const char *)jso->_userdata);
  763. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  764. return userdata_len;
  765. }
  766. void json_object_free_userdata(struct json_object *jso, void *userdata)
  767. {
  768. free(userdata);
  769. }
  770. double json_object_get_double(const struct json_object *jso)
  771. {
  772. double cdouble;
  773. char *errPtr = NULL;
  774. if(!jso) return 0.0;
  775. switch(jso->o_type) {
  776. case json_type_double:
  777. return jso->o.c_double;
  778. case json_type_int:
  779. return jso->o.c_int64;
  780. case json_type_boolean:
  781. return jso->o.c_boolean;
  782. case json_type_string:
  783. errno = 0;
  784. cdouble = strtod(get_string_component(jso), &errPtr);
  785. /* if conversion stopped at the first character, return 0.0 */
  786. if (errPtr == get_string_component(jso))
  787. return 0.0;
  788. /*
  789. * Check that the conversion terminated on something sensible
  790. *
  791. * For example, { "pay" : 123AB } would parse as 123.
  792. */
  793. if (*errPtr != '\0')
  794. return 0.0;
  795. /*
  796. * If strtod encounters a string which would exceed the
  797. * capacity of a double, it returns +/- HUGE_VAL and sets
  798. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  799. * from a conversion, so we need to check errno.
  800. *
  801. * Underflow also sets errno to ERANGE, but it returns 0 in
  802. * that case, which is what we will return anyway.
  803. *
  804. * See CERT guideline ERR30-C
  805. */
  806. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  807. (ERANGE == errno))
  808. cdouble = 0.0;
  809. return cdouble;
  810. default:
  811. return 0.0;
  812. }
  813. }
  814. int json_object_set_double(struct json_object *jso,double new_value){
  815. if (!jso || jso->o_type!=json_type_double)
  816. return 0;
  817. jso->o.c_double=new_value;
  818. return 1;
  819. }
  820. /* json_object_string */
  821. static int json_object_string_to_json_string(struct json_object* jso,
  822. struct printbuf *pb,
  823. int level,
  824. int flags)
  825. {
  826. printbuf_strappend(pb, "\"");
  827. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  828. printbuf_strappend(pb, "\"");
  829. return 0;
  830. }
  831. static void json_object_string_delete(struct json_object* jso)
  832. {
  833. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  834. free(jso->o.c_string.str.ptr);
  835. json_object_generic_delete(jso);
  836. }
  837. struct json_object* json_object_new_string(const char *s)
  838. {
  839. struct json_object *jso = json_object_new(json_type_string);
  840. if (!jso)
  841. return NULL;
  842. jso->_delete = &json_object_string_delete;
  843. jso->_to_json_string = &json_object_string_to_json_string;
  844. jso->o.c_string.len = strlen(s);
  845. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  846. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  847. } else {
  848. jso->o.c_string.str.ptr = strdup(s);
  849. if (!jso->o.c_string.str.ptr)
  850. {
  851. json_object_generic_delete(jso);
  852. errno = ENOMEM;
  853. return NULL;
  854. }
  855. }
  856. return jso;
  857. }
  858. struct json_object* json_object_new_string_len(const char *s, int len)
  859. {
  860. char *dstbuf;
  861. struct json_object *jso = json_object_new(json_type_string);
  862. if (!jso)
  863. return NULL;
  864. jso->_delete = &json_object_string_delete;
  865. jso->_to_json_string = &json_object_string_to_json_string;
  866. if(len < LEN_DIRECT_STRING_DATA) {
  867. dstbuf = jso->o.c_string.str.data;
  868. } else {
  869. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  870. if (!jso->o.c_string.str.ptr)
  871. {
  872. json_object_generic_delete(jso);
  873. errno = ENOMEM;
  874. return NULL;
  875. }
  876. dstbuf = jso->o.c_string.str.ptr;
  877. }
  878. memcpy(dstbuf, (const void *)s, len);
  879. dstbuf[len] = '\0';
  880. jso->o.c_string.len = len;
  881. return jso;
  882. }
  883. const char* json_object_get_string(struct json_object *jso)
  884. {
  885. if (!jso)
  886. return NULL;
  887. switch(jso->o_type)
  888. {
  889. case json_type_string:
  890. return get_string_component(jso);
  891. default:
  892. return json_object_to_json_string(jso);
  893. }
  894. }
  895. int json_object_get_string_len(const struct json_object *jso)
  896. {
  897. if (!jso)
  898. return 0;
  899. switch(jso->o_type)
  900. {
  901. case json_type_string:
  902. return jso->o.c_string.len;
  903. default:
  904. return 0;
  905. }
  906. }
  907. int json_object_set_string(json_object* jso, const char* s) {
  908. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  909. }
  910. int json_object_set_string_len(json_object* jso, const char* s, int len){
  911. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  912. char *dstbuf;
  913. if (len<LEN_DIRECT_STRING_DATA) {
  914. dstbuf=jso->o.c_string.str.data;
  915. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  916. } else {
  917. dstbuf=(char *)malloc(len+1);
  918. if (dstbuf==NULL) return 0;
  919. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  920. jso->o.c_string.str.ptr=dstbuf;
  921. }
  922. jso->o.c_string.len=len;
  923. memcpy(dstbuf, (const void *)s, len);
  924. dstbuf[len] = '\0';
  925. return 1;
  926. }
  927. /* json_object_array */
  928. static int json_object_array_to_json_string(struct json_object* jso,
  929. struct printbuf *pb,
  930. int level,
  931. int flags)
  932. {
  933. int had_children = 0;
  934. size_t ii;
  935. printbuf_strappend(pb, "[");
  936. if (flags & JSON_C_TO_STRING_PRETTY)
  937. printbuf_strappend(pb, "\n");
  938. for(ii=0; ii < json_object_array_length(jso); ii++)
  939. {
  940. struct json_object *val;
  941. if (had_children)
  942. {
  943. printbuf_strappend(pb, ",");
  944. if (flags & JSON_C_TO_STRING_PRETTY)
  945. printbuf_strappend(pb, "\n");
  946. }
  947. had_children = 1;
  948. if (flags & JSON_C_TO_STRING_SPACED)
  949. printbuf_strappend(pb, " ");
  950. indent(pb, level + 1, flags);
  951. val = json_object_array_get_idx(jso, ii);
  952. if(val == NULL)
  953. printbuf_strappend(pb, "null");
  954. else
  955. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  956. return -1;
  957. }
  958. if (flags & JSON_C_TO_STRING_PRETTY)
  959. {
  960. if (had_children)
  961. printbuf_strappend(pb, "\n");
  962. indent(pb,level,flags);
  963. }
  964. if (flags & JSON_C_TO_STRING_SPACED)
  965. return printbuf_strappend(pb, " ]");
  966. return printbuf_strappend(pb, "]");
  967. }
  968. static void json_object_array_entry_free(void *data)
  969. {
  970. json_object_put((struct json_object*)data);
  971. }
  972. static void json_object_array_delete(struct json_object* jso)
  973. {
  974. array_list_free(jso->o.c_array);
  975. json_object_generic_delete(jso);
  976. }
  977. struct json_object* json_object_new_array(void)
  978. {
  979. struct json_object *jso = json_object_new(json_type_array);
  980. if (!jso)
  981. return NULL;
  982. jso->_delete = &json_object_array_delete;
  983. jso->_to_json_string = &json_object_array_to_json_string;
  984. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  985. if(jso->o.c_array == NULL)
  986. {
  987. free(jso);
  988. return NULL;
  989. }
  990. return jso;
  991. }
  992. struct array_list* json_object_get_array(const struct json_object *jso)
  993. {
  994. if (!jso)
  995. return NULL;
  996. switch(jso->o_type)
  997. {
  998. case json_type_array:
  999. return jso->o.c_array;
  1000. default:
  1001. return NULL;
  1002. }
  1003. }
  1004. void json_object_array_sort(struct json_object *jso,
  1005. int(*sort_fn)(const void *, const void *))
  1006. {
  1007. assert(json_object_get_type(jso) == json_type_array);
  1008. array_list_sort(jso->o.c_array, sort_fn);
  1009. }
  1010. struct json_object* json_object_array_bsearch(
  1011. const struct json_object *key,
  1012. const struct json_object *jso,
  1013. int (*sort_fn)(const void *, const void *))
  1014. {
  1015. struct json_object **result;
  1016. assert(json_object_get_type(jso) == json_type_array);
  1017. result = (struct json_object **)array_list_bsearch(
  1018. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1019. if (!result)
  1020. return NULL;
  1021. return *result;
  1022. }
  1023. size_t json_object_array_length(const struct json_object *jso)
  1024. {
  1025. assert(json_object_get_type(jso) == json_type_array);
  1026. return array_list_length(jso->o.c_array);
  1027. }
  1028. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1029. {
  1030. assert(json_object_get_type(jso) == json_type_array);
  1031. return array_list_add(jso->o.c_array, val);
  1032. }
  1033. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1034. struct json_object *val)
  1035. {
  1036. assert(json_object_get_type(jso) == json_type_array);
  1037. return array_list_put_idx(jso->o.c_array, idx, val);
  1038. }
  1039. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1040. {
  1041. assert(json_object_get_type(jso) == json_type_array);
  1042. return array_list_del_idx(jso->o.c_array, idx, count);
  1043. }
  1044. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1045. size_t idx)
  1046. {
  1047. assert(json_object_get_type(jso) == json_type_array);
  1048. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1049. }
  1050. static int json_array_equal(struct json_object* jso1,
  1051. struct json_object* jso2)
  1052. {
  1053. size_t len, i;
  1054. len = json_object_array_length(jso1);
  1055. if (len != json_object_array_length(jso2))
  1056. return 0;
  1057. for (i = 0; i < len; i++) {
  1058. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1059. json_object_array_get_idx(jso2, i)))
  1060. return 0;
  1061. }
  1062. return 1;
  1063. }
  1064. static int json_object_all_values_equal(struct json_object* jso1,
  1065. struct json_object* jso2)
  1066. {
  1067. struct json_object_iter iter;
  1068. struct json_object *sub;
  1069. assert(json_object_get_type(jso1) == json_type_object);
  1070. assert(json_object_get_type(jso2) == json_type_object);
  1071. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1072. json_object_object_foreachC(jso1, iter) {
  1073. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1074. (void**)(void *)&sub))
  1075. return 0;
  1076. if (!json_object_equal(iter.val, sub))
  1077. return 0;
  1078. }
  1079. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1080. json_object_object_foreachC(jso2, iter) {
  1081. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1082. (void**)(void *)&sub))
  1083. return 0;
  1084. }
  1085. return 1;
  1086. }
  1087. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1088. {
  1089. if (jso1 == jso2)
  1090. return 1;
  1091. if (!jso1 || !jso2)
  1092. return 0;
  1093. if (jso1->o_type != jso2->o_type)
  1094. return 0;
  1095. switch(jso1->o_type) {
  1096. case json_type_boolean:
  1097. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1098. case json_type_double:
  1099. return (jso1->o.c_double == jso2->o.c_double);
  1100. case json_type_int:
  1101. return (jso1->o.c_int64 == jso2->o.c_int64);
  1102. case json_type_string:
  1103. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1104. memcmp(get_string_component(jso1),
  1105. get_string_component(jso2),
  1106. jso1->o.c_string.len) == 0);
  1107. case json_type_object:
  1108. return json_object_all_values_equal(jso1, jso2);
  1109. case json_type_array:
  1110. return json_array_equal(jso1, jso2);
  1111. case json_type_null:
  1112. return 1;
  1113. };
  1114. return 0;
  1115. }