value.c 19 KB

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