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

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