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