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