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

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