value.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #define _GNU_SOURCE
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include "jansson.h"
  13. #include "hashtable.h"
  14. #include "jansson_private.h"
  15. #include "utf.h"
  16. /* Work around nonstandard isnan() and isinf() implementations */
  17. #ifndef isnan
  18. static JSON_INLINE int isnan(double x) { return x != x; }
  19. #endif
  20. #ifndef isinf
  21. static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
  22. #endif
  23. static JSON_INLINE void json_init(json_t *json, json_type type)
  24. {
  25. json->type = type;
  26. json->refcount = 1;
  27. }
  28. /*** object ***/
  29. json_t *json_object(void)
  30. {
  31. json_object_t *object = jsonp_malloc(sizeof(json_object_t));
  32. if(!object)
  33. return NULL;
  34. json_init(&object->json, JSON_OBJECT);
  35. if(hashtable_init(&object->hashtable))
  36. {
  37. jsonp_free(object);
  38. return NULL;
  39. }
  40. object->serial = 0;
  41. object->visited = 0;
  42. return &object->json;
  43. }
  44. static void json_delete_object(json_object_t *object)
  45. {
  46. hashtable_close(&object->hashtable);
  47. jsonp_free(object);
  48. }
  49. size_t json_object_size(const json_t *json)
  50. {
  51. json_object_t *object;
  52. if(!json_is_object(json))
  53. return 0;
  54. object = json_to_object(json);
  55. return object->hashtable.size;
  56. }
  57. json_t *json_object_get(const json_t *json, const char *key)
  58. {
  59. json_object_t *object;
  60. if(!json_is_object(json))
  61. return NULL;
  62. object = json_to_object(json);
  63. return hashtable_get(&object->hashtable, key);
  64. }
  65. int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
  66. {
  67. json_object_t *object;
  68. if(!value)
  69. return -1;
  70. if(!key || !json_is_object(json) || json == value)
  71. {
  72. json_decref(value);
  73. return -1;
  74. }
  75. object = json_to_object(json);
  76. if(hashtable_set(&object->hashtable, key, object->serial++, value))
  77. {
  78. json_decref(value);
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. int json_object_set_new(json_t *json, const char *key, json_t *value)
  84. {
  85. if(!key || !utf8_check_string(key, -1))
  86. {
  87. json_decref(value);
  88. return -1;
  89. }
  90. return json_object_set_new_nocheck(json, key, value);
  91. }
  92. int json_object_del(json_t *json, const char *key)
  93. {
  94. json_object_t *object;
  95. if(!json_is_object(json))
  96. return -1;
  97. object = json_to_object(json);
  98. return hashtable_del(&object->hashtable, key);
  99. }
  100. int json_object_clear(json_t *json)
  101. {
  102. json_object_t *object;
  103. if(!json_is_object(json))
  104. return -1;
  105. object = json_to_object(json);
  106. hashtable_clear(&object->hashtable);
  107. object->serial = 0;
  108. return 0;
  109. }
  110. int json_object_update(json_t *object, json_t *other)
  111. {
  112. const char *key;
  113. json_t *value;
  114. if(!json_is_object(object) || !json_is_object(other))
  115. return -1;
  116. json_object_foreach(other, key, value) {
  117. if(json_object_set_nocheck(object, key, value))
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. int json_object_update_existing(json_t *object, json_t *other)
  123. {
  124. const char *key;
  125. json_t *value;
  126. if(!json_is_object(object) || !json_is_object(other))
  127. return -1;
  128. json_object_foreach(other, key, value) {
  129. if(json_object_get(object, key))
  130. json_object_set_nocheck(object, key, value);
  131. }
  132. return 0;
  133. }
  134. int json_object_update_missing(json_t *object, json_t *other)
  135. {
  136. const char *key;
  137. json_t *value;
  138. if(!json_is_object(object) || !json_is_object(other))
  139. return -1;
  140. json_object_foreach(other, key, value) {
  141. if(!json_object_get(object, key))
  142. json_object_set_nocheck(object, key, value);
  143. }
  144. return 0;
  145. }
  146. void *json_object_iter(json_t *json)
  147. {
  148. json_object_t *object;
  149. if(!json_is_object(json))
  150. return NULL;
  151. object = json_to_object(json);
  152. return hashtable_iter(&object->hashtable);
  153. }
  154. void *json_object_iter_at(json_t *json, const char *key)
  155. {
  156. json_object_t *object;
  157. if(!key || !json_is_object(json))
  158. return NULL;
  159. object = json_to_object(json);
  160. return hashtable_iter_at(&object->hashtable, key);
  161. }
  162. void *json_object_iter_next(json_t *json, void *iter)
  163. {
  164. json_object_t *object;
  165. if(!json_is_object(json) || iter == NULL)
  166. return NULL;
  167. object = json_to_object(json);
  168. return hashtable_iter_next(&object->hashtable, iter);
  169. }
  170. const char *json_object_iter_key(void *iter)
  171. {
  172. if(!iter)
  173. return NULL;
  174. return hashtable_iter_key(iter);
  175. }
  176. json_t *json_object_iter_value(void *iter)
  177. {
  178. if(!iter)
  179. return NULL;
  180. return (json_t *)hashtable_iter_value(iter);
  181. }
  182. int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
  183. {
  184. if(!json_is_object(json) || !iter || !value)
  185. return -1;
  186. hashtable_iter_set(iter, value);
  187. return 0;
  188. }
  189. void *json_object_key_to_iter(const char *key)
  190. {
  191. if(!key)
  192. return NULL;
  193. return hashtable_key_to_iter(key);
  194. }
  195. static int json_object_equal(json_t *object1, json_t *object2)
  196. {
  197. const char *key;
  198. json_t *value1, *value2;
  199. if(json_object_size(object1) != json_object_size(object2))
  200. return 0;
  201. json_object_foreach(object1, key, value1) {
  202. value2 = json_object_get(object2, key);
  203. if(!json_equal(value1, value2))
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. static json_t *json_object_copy(json_t *object)
  209. {
  210. json_t *result;
  211. const char *key;
  212. json_t *value;
  213. result = json_object();
  214. if(!result)
  215. return NULL;
  216. json_object_foreach(object, key, value)
  217. json_object_set_nocheck(result, key, value);
  218. return result;
  219. }
  220. static json_t *json_object_deep_copy(json_t *object)
  221. {
  222. json_t *result;
  223. const char *key;
  224. json_t *value;
  225. result = json_object();
  226. if(!result)
  227. return NULL;
  228. json_object_foreach(object, key, value)
  229. json_object_set_new_nocheck(result, key, json_deep_copy(value));
  230. return result;
  231. }
  232. /*** array ***/
  233. json_t *json_array(void)
  234. {
  235. json_array_t *array = jsonp_malloc(sizeof(json_array_t));
  236. if(!array)
  237. return NULL;
  238. json_init(&array->json, JSON_ARRAY);
  239. array->entries = 0;
  240. array->size = 8;
  241. array->table = jsonp_malloc(array->size * sizeof(json_t *));
  242. if(!array->table) {
  243. jsonp_free(array);
  244. return NULL;
  245. }
  246. array->visited = 0;
  247. return &array->json;
  248. }
  249. static void json_delete_array(json_array_t *array)
  250. {
  251. size_t i;
  252. for(i = 0; i < array->entries; i++)
  253. json_decref(array->table[i]);
  254. jsonp_free(array->table);
  255. jsonp_free(array);
  256. }
  257. size_t json_array_size(const json_t *json)
  258. {
  259. if(!json_is_array(json))
  260. return 0;
  261. return json_to_array(json)->entries;
  262. }
  263. json_t *json_array_get(const json_t *json, size_t index)
  264. {
  265. json_array_t *array;
  266. if(!json_is_array(json))
  267. return NULL;
  268. array = json_to_array(json);
  269. if(index >= array->entries)
  270. return NULL;
  271. return array->table[index];
  272. }
  273. int json_array_set_new(json_t *json, size_t index, json_t *value)
  274. {
  275. json_array_t *array;
  276. if(!value)
  277. return -1;
  278. if(!json_is_array(json) || json == value)
  279. {
  280. json_decref(value);
  281. return -1;
  282. }
  283. array = json_to_array(json);
  284. if(index >= array->entries)
  285. {
  286. json_decref(value);
  287. return -1;
  288. }
  289. json_decref(array->table[index]);
  290. array->table[index] = value;
  291. return 0;
  292. }
  293. static void array_move(json_array_t *array, size_t dest,
  294. size_t src, size_t count)
  295. {
  296. memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
  297. }
  298. static void array_copy(json_t **dest, size_t dpos,
  299. json_t **src, size_t spos,
  300. size_t count)
  301. {
  302. memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
  303. }
  304. static json_t **json_array_grow(json_array_t *array,
  305. size_t amount,
  306. int copy)
  307. {
  308. size_t new_size;
  309. json_t **old_table, **new_table;
  310. if(array->entries + amount <= array->size)
  311. return array->table;
  312. old_table = array->table;
  313. new_size = max(array->size + amount, array->size * 2);
  314. new_table = jsonp_malloc(new_size * sizeof(json_t *));
  315. if(!new_table)
  316. return NULL;
  317. array->size = new_size;
  318. array->table = new_table;
  319. if(copy) {
  320. array_copy(array->table, 0, old_table, 0, array->entries);
  321. jsonp_free(old_table);
  322. return array->table;
  323. }
  324. return old_table;
  325. }
  326. int json_array_append_new(json_t *json, json_t *value)
  327. {
  328. json_array_t *array;
  329. if(!value)
  330. return -1;
  331. if(!json_is_array(json) || json == value)
  332. {
  333. json_decref(value);
  334. return -1;
  335. }
  336. array = json_to_array(json);
  337. if(!json_array_grow(array, 1, 1)) {
  338. json_decref(value);
  339. return -1;
  340. }
  341. array->table[array->entries] = value;
  342. array->entries++;
  343. return 0;
  344. }
  345. int json_array_insert_new(json_t *json, size_t index, json_t *value)
  346. {
  347. json_array_t *array;
  348. json_t **old_table;
  349. if(!value)
  350. return -1;
  351. if(!json_is_array(json) || json == value) {
  352. json_decref(value);
  353. return -1;
  354. }
  355. array = json_to_array(json);
  356. if(index > array->entries) {
  357. json_decref(value);
  358. return -1;
  359. }
  360. old_table = json_array_grow(array, 1, 0);
  361. if(!old_table) {
  362. json_decref(value);
  363. return -1;
  364. }
  365. if(old_table != array->table) {
  366. array_copy(array->table, 0, old_table, 0, index);
  367. array_copy(array->table, index + 1, old_table, index,
  368. array->entries - index);
  369. jsonp_free(old_table);
  370. }
  371. else
  372. array_move(array, index + 1, index, array->entries - index);
  373. array->table[index] = value;
  374. array->entries++;
  375. return 0;
  376. }
  377. int json_array_remove(json_t *json, size_t index)
  378. {
  379. json_array_t *array;
  380. if(!json_is_array(json))
  381. return -1;
  382. array = json_to_array(json);
  383. if(index >= array->entries)
  384. return -1;
  385. json_decref(array->table[index]);
  386. array_move(array, index, index + 1, array->entries - index);
  387. array->entries--;
  388. return 0;
  389. }
  390. int json_array_clear(json_t *json)
  391. {
  392. json_array_t *array;
  393. size_t i;
  394. if(!json_is_array(json))
  395. return -1;
  396. array = json_to_array(json);
  397. for(i = 0; i < array->entries; i++)
  398. json_decref(array->table[i]);
  399. array->entries = 0;
  400. return 0;
  401. }
  402. int json_array_extend(json_t *json, json_t *other_json)
  403. {
  404. json_array_t *array, *other;
  405. size_t i;
  406. if(!json_is_array(json) || !json_is_array(other_json))
  407. return -1;
  408. array = json_to_array(json);
  409. other = json_to_array(other_json);
  410. if(!json_array_grow(array, other->entries, 1))
  411. return -1;
  412. for(i = 0; i < other->entries; i++)
  413. json_incref(other->table[i]);
  414. array_copy(array->table, array->entries, other->table, 0, other->entries);
  415. array->entries += other->entries;
  416. return 0;
  417. }
  418. static int json_array_equal(json_t *array1, json_t *array2)
  419. {
  420. size_t i, size;
  421. size = json_array_size(array1);
  422. if(size != json_array_size(array2))
  423. return 0;
  424. for(i = 0; i < size; i++)
  425. {
  426. json_t *value1, *value2;
  427. value1 = json_array_get(array1, i);
  428. value2 = json_array_get(array2, i);
  429. if(!json_equal(value1, value2))
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. static json_t *json_array_copy(json_t *array)
  435. {
  436. json_t *result;
  437. size_t i;
  438. result = json_array();
  439. if(!result)
  440. return NULL;
  441. for(i = 0; i < json_array_size(array); i++)
  442. json_array_append(result, json_array_get(array, i));
  443. return result;
  444. }
  445. static json_t *json_array_deep_copy(json_t *array)
  446. {
  447. json_t *result;
  448. size_t i;
  449. result = json_array();
  450. if(!result)
  451. return NULL;
  452. for(i = 0; i < json_array_size(array); i++)
  453. json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
  454. return result;
  455. }
  456. /*** string ***/
  457. json_t *json_string_nocheck(const char *value)
  458. {
  459. json_string_t *string;
  460. if(!value)
  461. return NULL;
  462. string = jsonp_malloc(sizeof(json_string_t));
  463. if(!string)
  464. return NULL;
  465. json_init(&string->json, JSON_STRING);
  466. string->value = jsonp_strdup(value);
  467. if(!string->value) {
  468. jsonp_free(string);
  469. return NULL;
  470. }
  471. return &string->json;
  472. }
  473. json_t *json_string(const char *value)
  474. {
  475. if(!value || !utf8_check_string(value, -1))
  476. return NULL;
  477. return json_string_nocheck(value);
  478. }
  479. const char *json_string_value(const json_t *json)
  480. {
  481. if(!json_is_string(json))
  482. return NULL;
  483. return json_to_string(json)->value;
  484. }
  485. int json_string_set_nocheck(json_t *json, const char *value)
  486. {
  487. char *dup;
  488. json_string_t *string;
  489. if(!json_is_string(json) || !value)
  490. return -1;
  491. dup = jsonp_strdup(value);
  492. if(!dup)
  493. return -1;
  494. string = json_to_string(json);
  495. jsonp_free(string->value);
  496. string->value = dup;
  497. return 0;
  498. }
  499. int json_string_set(json_t *json, const char *value)
  500. {
  501. if(!value || !utf8_check_string(value, -1))
  502. return -1;
  503. return json_string_set_nocheck(json, value);
  504. }
  505. static void json_delete_string(json_string_t *string)
  506. {
  507. jsonp_free(string->value);
  508. jsonp_free(string);
  509. }
  510. static int json_string_equal(json_t *string1, json_t *string2)
  511. {
  512. return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
  513. }
  514. static json_t *json_string_copy(json_t *string)
  515. {
  516. return json_string_nocheck(json_string_value(string));
  517. }
  518. /*** integer ***/
  519. json_t *json_integer(json_int_t value)
  520. {
  521. json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
  522. if(!integer)
  523. return NULL;
  524. json_init(&integer->json, JSON_INTEGER);
  525. integer->value = value;
  526. return &integer->json;
  527. }
  528. json_int_t json_integer_value(const json_t *json)
  529. {
  530. if(!json_is_integer(json))
  531. return 0;
  532. return json_to_integer(json)->value;
  533. }
  534. int json_integer_set(json_t *json, json_int_t value)
  535. {
  536. if(!json_is_integer(json))
  537. return -1;
  538. json_to_integer(json)->value = value;
  539. return 0;
  540. }
  541. static void json_delete_integer(json_integer_t *integer)
  542. {
  543. jsonp_free(integer);
  544. }
  545. static int json_integer_equal(json_t *integer1, json_t *integer2)
  546. {
  547. return json_integer_value(integer1) == json_integer_value(integer2);
  548. }
  549. static json_t *json_integer_copy(json_t *integer)
  550. {
  551. return json_integer(json_integer_value(integer));
  552. }
  553. /*** real ***/
  554. json_t *json_real(double value)
  555. {
  556. json_real_t *real;
  557. if(isnan(value) || isinf(value))
  558. return NULL;
  559. real = jsonp_malloc(sizeof(json_real_t));
  560. if(!real)
  561. return NULL;
  562. json_init(&real->json, JSON_REAL);
  563. real->value = value;
  564. return &real->json;
  565. }
  566. double json_real_value(const json_t *json)
  567. {
  568. if(!json_is_real(json))
  569. return 0;
  570. return json_to_real(json)->value;
  571. }
  572. int json_real_set(json_t *json, double value)
  573. {
  574. if(!json_is_real(json) || isnan(value) || isinf(value))
  575. return -1;
  576. json_to_real(json)->value = value;
  577. return 0;
  578. }
  579. static void json_delete_real(json_real_t *real)
  580. {
  581. jsonp_free(real);
  582. }
  583. static int json_real_equal(json_t *real1, json_t *real2)
  584. {
  585. return json_real_value(real1) == json_real_value(real2);
  586. }
  587. static json_t *json_real_copy(json_t *real)
  588. {
  589. return json_real(json_real_value(real));
  590. }
  591. /*** number ***/
  592. double json_number_value(const json_t *json)
  593. {
  594. if(json_is_integer(json))
  595. return (double)json_integer_value(json);
  596. else if(json_is_real(json))
  597. return json_real_value(json);
  598. else
  599. return 0.0;
  600. }
  601. /*** simple values ***/
  602. json_t *json_true(void)
  603. {
  604. static json_t the_true = {JSON_TRUE, (size_t)-1};
  605. return &the_true;
  606. }
  607. json_t *json_false(void)
  608. {
  609. static json_t the_false = {JSON_FALSE, (size_t)-1};
  610. return &the_false;
  611. }
  612. json_t *json_null(void)
  613. {
  614. static json_t the_null = {JSON_NULL, (size_t)-1};
  615. return &the_null;
  616. }
  617. /*** deletion ***/
  618. void json_delete(json_t *json)
  619. {
  620. if(json_is_object(json))
  621. json_delete_object(json_to_object(json));
  622. else if(json_is_array(json))
  623. json_delete_array(json_to_array(json));
  624. else if(json_is_string(json))
  625. json_delete_string(json_to_string(json));
  626. else if(json_is_integer(json))
  627. json_delete_integer(json_to_integer(json));
  628. else if(json_is_real(json))
  629. json_delete_real(json_to_real(json));
  630. /* json_delete is not called for true, false or null */
  631. }
  632. /*** equality ***/
  633. int json_equal(json_t *json1, json_t *json2)
  634. {
  635. if(!json1 || !json2)
  636. return 0;
  637. if(json_typeof(json1) != json_typeof(json2))
  638. return 0;
  639. /* this covers true, false and null as they are singletons */
  640. if(json1 == json2)
  641. return 1;
  642. if(json_is_object(json1))
  643. return json_object_equal(json1, json2);
  644. if(json_is_array(json1))
  645. return json_array_equal(json1, json2);
  646. if(json_is_string(json1))
  647. return json_string_equal(json1, json2);
  648. if(json_is_integer(json1))
  649. return json_integer_equal(json1, json2);
  650. if(json_is_real(json1))
  651. return json_real_equal(json1, json2);
  652. return 0;
  653. }
  654. /*** copying ***/
  655. json_t *json_copy(json_t *json)
  656. {
  657. if(!json)
  658. return NULL;
  659. if(json_is_object(json))
  660. return json_object_copy(json);
  661. if(json_is_array(json))
  662. return json_array_copy(json);
  663. if(json_is_string(json))
  664. return json_string_copy(json);
  665. if(json_is_integer(json))
  666. return json_integer_copy(json);
  667. if(json_is_real(json))
  668. return json_real_copy(json);
  669. if(json_is_true(json) || json_is_false(json) || json_is_null(json))
  670. return json;
  671. return NULL;
  672. }
  673. json_t *json_deep_copy(json_t *json)
  674. {
  675. if(!json)
  676. return NULL;
  677. if(json_is_object(json))
  678. return json_object_deep_copy(json);
  679. if(json_is_array(json))
  680. return json_array_deep_copy(json);
  681. /* for the rest of the types, deep copying doesn't differ from
  682. shallow copying */
  683. if(json_is_string(json))
  684. return json_string_copy(json);
  685. if(json_is_integer(json))
  686. return json_integer_copy(json);
  687. if(json_is_real(json))
  688. return json_real_copy(json);
  689. if(json_is_true(json) || json_is_false(json) || json_is_null(json))
  690. return json;
  691. return NULL;
  692. }