value.c 19 KB

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