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

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