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