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
8#ifndef _GNU_SOURCE
9#define _GNU_SOURCE
10#endif
11
12#include <stddef.h>
13#include <stdlib.h>
14#include <string.h>
15#include <math.h>
16
17#include "jansson.h"
18#include "hashtable.h"
19#include "jansson_private.h"
20#include "utf.h"
21
22/* Work around nonstandard isnan() and isinf() implementations */
23#ifndef isnan
24static JSON_INLINE int isnan(double x) { return x != x; }
25#endif
26#ifndef isinf
27static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
28#endif
29
30static JSON_INLINE void json_init(json_t *json, json_type type)
31{
32 json->type = type;
33 json->refcount = 1;
34}
35
36
37/*** object ***/
38
39json_t *json_object(void)
40{
41 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
42 if(!object)
43 return NULL;
44 json_init(&object->json, JSON_OBJECT);
45
46 if(hashtable_init(&object->hashtable))
47 {
48 jsonp_free(object);
49 return NULL;
50 }
51
52 object->serial = 0;
53 object->visited = 0;
54
55 return &object->json;
56}
57
58static void json_delete_object(json_object_t *object)
59{
60 hashtable_close(&object->hashtable);
61 jsonp_free(object);
62}
63
64size_t json_object_size(const json_t *json)
65{
66 json_object_t *object;
67
68 if(!json_is_object(json))
69 return 0;
70
71 object = json_to_object(json);
72 return object->hashtable.size;
73}
74
75json_t *json_object_get(const json_t *json, const char *key)
76{
77 json_object_t *object;
78
79 if(!json_is_object(json))
80 return NULL;
81
82 object = json_to_object(json);
83 return hashtable_get(&object->hashtable, key);
84}
85
86int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
87{
88 json_object_t *object;
89
90 if(!value)
91 return -1;
92
93 if(!key || !json_is_object(json) || json == value)
94 {
95 json_decref(value);
96 return -1;
97 }
98 object = json_to_object(json);
99
100 if(hashtable_set(&object->hashtable, key, object->serial++, value))
101 {
102 json_decref(value);
103 return -1;
104 }
105
106 return 0;
107}
108
109int 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
117 return json_object_set_new_nocheck(json, key, value);
118}
119
120int json_object_del(json_t *json, const char *key)
121{
122 json_object_t *object;
123
124 if(!json_is_object(json))
125 return -1;
126
127 object = json_to_object(json);
128 return hashtable_del(&object->hashtable, key);
129}
130
131int json_object_clear(json_t *json)
132{
133 json_object_t *object;
134
135 if(!json_is_object(json))
136 return -1;
137
138 object = json_to_object(json);
139
140 hashtable_clear(&object->hashtable);
141 object->serial = 0;
142
143 return 0;
144}
145
146int json_object_update(json_t *object, json_t *other)
147{
148 const char *key;
149 json_t *value;
150
151 if(!json_is_object(object) || !json_is_object(other))
152 return -1;
153
154 json_object_foreach(other, key, value) {
155 if(json_object_set_nocheck(object, key, value))
156 return -1;
157 }
158
159 return 0;
160}
161
162int json_object_update_existing(json_t *object, json_t *other)
163{
164 const char *key;
165 json_t *value;
166
167 if(!json_is_object(object) || !json_is_object(other))
168 return -1;
169
170 json_object_foreach(other, key, value) {
171 if(json_object_get(object, key))
172 json_object_set_nocheck(object, key, value);
173 }
174
175 return 0;
176}
177
178int json_object_update_missing(json_t *object, json_t *other)
179{
180 const char *key;
181 json_t *value;
182
183 if(!json_is_object(object) || !json_is_object(other))
184 return -1;
185
186 json_object_foreach(other, key, value) {
187 if(!json_object_get(object, key))
188 json_object_set_nocheck(object, key, value);
189 }
190
191 return 0;
192}
193
194void *json_object_iter(json_t *json)
195{
196 json_object_t *object;
197
198 if(!json_is_object(json))
199 return NULL;
200
201 object = json_to_object(json);
202 return hashtable_iter(&object->hashtable);
203}
204
205void *json_object_iter_at(json_t *json, const char *key)
206{
207 json_object_t *object;
208
209 if(!key || !json_is_object(json))
210 return NULL;
211
212 object = json_to_object(json);
213 return hashtable_iter_at(&object->hashtable, key);
214}
215
216void *json_object_iter_next(json_t *json, void *iter)
217{
218 json_object_t *object;
219
220 if(!json_is_object(json) || iter == NULL)
221 return NULL;
222
223 object = json_to_object(json);
224 return hashtable_iter_next(&object->hashtable, iter);
225}
226
227const char *json_object_iter_key(void *iter)
228{
229 if(!iter)
230 return NULL;
231
232 return hashtable_iter_key(iter);
233}
234
235json_t *json_object_iter_value(void *iter)
236{
237 if(!iter)
238 return NULL;
239
240 return (json_t *)hashtable_iter_value(iter);
241}
242
243int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
244{
245 if(!json_is_object(json) || !iter || !value)
246 return -1;
247
248 hashtable_iter_set(iter, value);
249 return 0;
250}
251
252void *json_object_key_to_iter(const char *key)
253{
254 if(!key)
255 return NULL;
256
257 return hashtable_key_to_iter(key);
258}
259
260static int json_object_equal(json_t *object1, json_t *object2)
261{
262 const char *key;
263 json_t *value1, *value2;
264
265 if(json_object_size(object1) != json_object_size(object2))
266 return 0;
267
268 json_object_foreach(object1, key, value1) {
269 value2 = json_object_get(object2, key);
270
271 if(!json_equal(value1, value2))
272 return 0;
273 }
274
275 return 1;
276}
277
278static json_t *json_object_copy(json_t *object)
279{
280 json_t *result;
281
282 const char *key;
283 json_t *value;
284
285 result = json_object();
286 if(!result)
287 return NULL;
288
289 json_object_foreach(object, key, value)
290 json_object_set_nocheck(result, key, value);
291
292 return result;
293}
294
295static json_t *json_object_deep_copy(json_t *object)
296{
297 json_t *result;
298
299 const char *key;
300 json_t *value;
301
302 result = json_object();
303 if(!result)
304 return NULL;
305
306 json_object_foreach(object, key, value)
307 json_object_set_new_nocheck(result, key, json_deep_copy(value));
308
309 return result;
310}
311
312
313/*** array ***/
314
315json_t *json_array(void)
316{
317 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
318 if(!array)
319 return NULL;
320 json_init(&array->json, JSON_ARRAY);
321
322 array->entries = 0;
323 array->size = 8;
324
325 array->table = jsonp_malloc(array->size * sizeof(json_t *));
326 if(!array->table) {
327 jsonp_free(array);
328 return NULL;
329 }
330
331 array->visited = 0;
332
333 return &array->json;
334}
335
336static void json_delete_array(json_array_t *array)
337{
338 size_t i;
339
340 for(i = 0; i < array->entries; i++)
341 json_decref(array->table[i]);
342
343 jsonp_free(array->table);
344 jsonp_free(array);
345}
346
347size_t json_array_size(const json_t *json)
348{
349 if(!json_is_array(json))
350 return 0;
351
352 return json_to_array(json)->entries;
353}
354
355json_t *json_array_get(const json_t *json, size_t index)
356{
357 json_array_t *array;
358 if(!json_is_array(json))
359 return NULL;
360 array = json_to_array(json);
361
362 if(index >= array->entries)
363 return NULL;
364
365 return array->table[index];
366}
367
368int json_array_set_new(json_t *json, size_t index, json_t *value)
369{
370 json_array_t *array;
371
372 if(!value)
373 return -1;
374
375 if(!json_is_array(json) || json == value)
376 {
377 json_decref(value);
378 return -1;
379 }
380 array = json_to_array(json);
381
382 if(index >= array->entries)
383 {
384 json_decref(value);
385 return -1;
386 }
387
388 json_decref(array->table[index]);
389 array->table[index] = value;
390
391 return 0;
392}
393
394static void array_move(json_array_t *array, size_t dest,
395 size_t src, size_t count)
396{
397 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
398}
399
400static void array_copy(json_t **dest, size_t dpos,
401 json_t **src, size_t spos,
402 size_t count)
403{
404 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
405}
406
407static json_t **json_array_grow(json_array_t *array,
408 size_t amount,
409 int copy)
410{
411 size_t new_size;
412 json_t **old_table, **new_table;
413
414 if(array->entries + amount <= array->size)
415 return array->table;
416
417 old_table = array->table;
418
419 new_size = max(array->size + amount, array->size * 2);
420 new_table = jsonp_malloc(new_size * sizeof(json_t *));
421 if(!new_table)
422 return NULL;
423
424 array->size = new_size;
425 array->table = new_table;
426
427 if(copy) {
428 array_copy(array->table, 0, old_table, 0, array->entries);
429 jsonp_free(old_table);
430 return array->table;
431 }
432
433 return old_table;
434}
435
436int json_array_append_new(json_t *json, json_t *value)
437{
438 json_array_t *array;
439
440 if(!value)
441 return -1;
442
443 if(!json_is_array(json) || json == value)
444 {
445 json_decref(value);
446 return -1;
447 }
448 array = json_to_array(json);
449
450 if(!json_array_grow(array, 1, 1)) {
451 json_decref(value);
452 return -1;
453 }
454
455 array->table[array->entries] = value;
456 array->entries++;
457
458 return 0;
459}
460
461int json_array_insert_new(json_t *json, size_t index, json_t *value)
462{
463 json_array_t *array;
464 json_t **old_table;
465
466 if(!value)
467 return -1;
468
469 if(!json_is_array(json) || json == value) {
470 json_decref(value);
471 return -1;
472 }
473 array = json_to_array(json);
474
475 if(index > array->entries) {
476 json_decref(value);
477 return -1;
478 }
479
480 old_table = json_array_grow(array, 1, 0);
481 if(!old_table) {
482 json_decref(value);
483 return -1;
484 }
485
486 if(old_table != array->table) {
487 array_copy(array->table, 0, old_table, 0, index);
488 array_copy(array->table, index + 1, old_table, index,
489 array->entries - index);
490 jsonp_free(old_table);
491 }
492 else
493 array_move(array, index + 1, index, array->entries - index);
494
495 array->table[index] = value;
496 array->entries++;
497
498 return 0;
499}
500
501int json_array_remove(json_t *json, size_t index)
502{
503 json_array_t *array;
504
505 if(!json_is_array(json))
506 return -1;
507 array = json_to_array(json);
508
509 if(index >= array->entries)
510 return -1;
511
512 json_decref(array->table[index]);
513
514 /* If we're removing the last element, nothing has to be moved */
515 if(index < array->entries - 1)
516 array_move(array, index, index + 1, array->entries - index - 1);
517
518 array->entries--;
519
520 return 0;
521}
522
523int json_array_clear(json_t *json)
524{
525 json_array_t *array;
526 size_t i;
527
528 if(!json_is_array(json))
529 return -1;
530 array = json_to_array(json);
531
532 for(i = 0; i < array->entries; i++)
533 json_decref(array->table[i]);
534
535 array->entries = 0;
536 return 0;
537}
538
539int json_array_extend(json_t *json, json_t *other_json)
540{
541 json_array_t *array, *other;
542 size_t i;
543
544 if(!json_is_array(json) || !json_is_array(other_json))
545 return -1;
546 array = json_to_array(json);
547 other = json_to_array(other_json);
548
549 if(!json_array_grow(array, other->entries, 1))
550 return -1;
551
552 for(i = 0; i < other->entries; i++)
553 json_incref(other->table[i]);
554
555 array_copy(array->table, array->entries, other->table, 0, other->entries);
556
557 array->entries += other->entries;
558 return 0;
559}
560
561static int json_array_equal(json_t *array1, json_t *array2)
562{
563 size_t i, size;
564
565 size = json_array_size(array1);
566 if(size != json_array_size(array2))
567 return 0;
568
569 for(i = 0; i < size; i++)
570 {
571 json_t *value1, *value2;
572
573 value1 = json_array_get(array1, i);
574 value2 = json_array_get(array2, i);
575
576 if(!json_equal(value1, value2))
577 return 0;
578 }
579
580 return 1;
581}
582
583static json_t *json_array_copy(json_t *array)
584{
585 json_t *result;
586 size_t i;
587
588 result = json_array();
589 if(!result)
590 return NULL;
591
592 for(i = 0; i < json_array_size(array); i++)
593 json_array_append(result, json_array_get(array, i));
594
595 return result;
596}
597
598static json_t *json_array_deep_copy(json_t *array)
599{
600 json_t *result;
601 size_t i;
602
603 result = json_array();
604 if(!result)
605 return NULL;
606
607 for(i = 0; i < json_array_size(array); i++)
608 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
609
610 return result;
611}
612
613/*** string ***/
614
615json_t *json_string_nocheck(const char *value)
616{
617 json_string_t *string;
618
619 if(!value)
620 return NULL;
621
622 string = jsonp_malloc(sizeof(json_string_t));
623 if(!string)
624 return NULL;
625 json_init(&string->json, JSON_STRING);
626
627 string->value = jsonp_strdup(value);
628 if(!string->value) {
629 jsonp_free(string);
630 return NULL;
631 }
632
633 return &string->json;
634}
635
636json_t *json_string(const char *value)
637{
638 if(!value || !utf8_check_string(value, -1))
639 return NULL;
640
641 return json_string_nocheck(value);
642}
643
644const char *json_string_value(const json_t *json)
645{
646 if(!json_is_string(json))
647 return NULL;
648
649 return json_to_string(json)->value;
650}
651
652int json_string_set_nocheck(json_t *json, const char *value)
653{
654 char *dup;
655 json_string_t *string;
656
657 if(!json_is_string(json) || !value)
658 return -1;
659
660 dup = jsonp_strdup(value);
661 if(!dup)
662 return -1;
663
664 string = json_to_string(json);
665 jsonp_free(string->value);
666 string->value = dup;
667
668 return 0;
669}
670
671int json_string_set(json_t *json, const char *value)
672{
673 if(!value || !utf8_check_string(value, -1))
674 return -1;
675
676 return json_string_set_nocheck(json, value);
677}
678
679static void json_delete_string(json_string_t *string)
680{
681 jsonp_free(string->value);
682 jsonp_free(string);
683}
684
685static int json_string_equal(json_t *string1, json_t *string2)
686{
687 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
688}
689
690static json_t *json_string_copy(json_t *string)
691{
692 return json_string_nocheck(json_string_value(string));
693}
694
695
696/*** integer ***/
697
698json_t *json_integer(json_int_t value)
699{
700 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
701 if(!integer)
702 return NULL;
703 json_init(&integer->json, JSON_INTEGER);
704
705 integer->value = value;
706 return &integer->json;
707}
708
709json_int_t json_integer_value(const json_t *json)
710{
711 if(!json_is_integer(json))
712 return 0;
713
714 return json_to_integer(json)->value;
715}
716
717int json_integer_set(json_t *json, json_int_t value)
718{
719 if(!json_is_integer(json))
720 return -1;
721
722 json_to_integer(json)->value = value;
723
724 return 0;
725}
726
727static void json_delete_integer(json_integer_t *integer)
728{
729 jsonp_free(integer);
730}
731
732static int json_integer_equal(json_t *integer1, json_t *integer2)
733{
734 return json_integer_value(integer1) == json_integer_value(integer2);
735}
736
737static json_t *json_integer_copy(json_t *integer)
738{
739 return json_integer(json_integer_value(integer));
740}
741
742
743/*** real ***/
744
745json_t *json_real(double value)
746{
747 json_real_t *real;
748
749 if(isnan(value) || isinf(value))
750 return NULL;
751
752 real = jsonp_malloc(sizeof(json_real_t));
753 if(!real)
754 return NULL;
755 json_init(&real->json, JSON_REAL);
756
757 real->value = value;
758 return &real->json;
759}
760
761double json_real_value(const json_t *json)
762{
763 if(!json_is_real(json))
764 return 0;
765
766 return json_to_real(json)->value;
767}
768
769int json_real_set(json_t *json, double value)
770{
771 if(!json_is_real(json) || isnan(value) || isinf(value))
772 return -1;
773
774 json_to_real(json)->value = value;
775
776 return 0;
777}
778
779static void json_delete_real(json_real_t *real)
780{
781 jsonp_free(real);
782}
783
784static int json_real_equal(json_t *real1, json_t *real2)
785{
786 return json_real_value(real1) == json_real_value(real2);
787}
788
789static json_t *json_real_copy(json_t *real)
790{
791 return json_real(json_real_value(real));
792}
793
794
795/*** number ***/
796
797double json_number_value(const json_t *json)
798{
799 if(json_is_integer(json))
800 return (double)json_integer_value(json);
801 else if(json_is_real(json))
802 return json_real_value(json);
803 else
804 return 0.0;
805}
806
807
808/*** simple values ***/
809
810json_t *json_true(void)
811{
812 static json_t the_true = {JSON_TRUE, (size_t)-1};
813 return &the_true;
814}
815
816
817json_t *json_false(void)
818{
819 static json_t the_false = {JSON_FALSE, (size_t)-1};
820 return &the_false;
821}
822
823
824json_t *json_null(void)
825{
826 static json_t the_null = {JSON_NULL, (size_t)-1};
827 return &the_null;
828}
829
830
831/*** deletion ***/
832
833void json_delete(json_t *json)
834{
835 if(json_is_object(json))
836 json_delete_object(json_to_object(json));
837
838 else if(json_is_array(json))
839 json_delete_array(json_to_array(json));
840
841 else if(json_is_string(json))
842 json_delete_string(json_to_string(json));
843
844 else if(json_is_integer(json))
845 json_delete_integer(json_to_integer(json));
846
847 else if(json_is_real(json))
848 json_delete_real(json_to_real(json));
849
850 /* json_delete is not called for true, false or null */
851}
852
853
854/*** equality ***/
855
856int json_equal(json_t *json1, json_t *json2)
857{
858 if(!json1 || !json2)
859 return 0;
860
861 if(json_typeof(json1) != json_typeof(json2))
862 return 0;
863
864 /* this covers true, false and null as they are singletons */
865 if(json1 == json2)
866 return 1;
867
868 if(json_is_object(json1))
869 return json_object_equal(json1, json2);
870
871 if(json_is_array(json1))
872 return json_array_equal(json1, json2);
873
874 if(json_is_string(json1))
875 return json_string_equal(json1, json2);
876
877 if(json_is_integer(json1))
878 return json_integer_equal(json1, json2);
879
880 if(json_is_real(json1))
881 return json_real_equal(json1, json2);
882
883 return 0;
884}
885
886
887/*** copying ***/
888
889json_t *json_copy(json_t *json)
890{
891 if(!json)
892 return NULL;
893
894 if(json_is_object(json))
895 return json_object_copy(json);
896
897 if(json_is_array(json))
898 return json_array_copy(json);
899
900 if(json_is_string(json))
901 return json_string_copy(json);
902
903 if(json_is_integer(json))
904 return json_integer_copy(json);
905
906 if(json_is_real(json))
907 return json_real_copy(json);
908
909 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
910 return json;
911
912 return NULL;
913}
914
915json_t *json_deep_copy(json_t *json)
916{
917 if(!json)
918 return NULL;
919
920 if(json_is_object(json))
921 return json_object_deep_copy(json);
922
923 if(json_is_array(json))
924 return json_array_deep_copy(json);
925
926 /* for the rest of the types, deep copying doesn't differ from
927 shallow copying */
928
929 if(json_is_string(json))
930 return json_string_copy(json);
931
932 if(json_is_integer(json))
933 return json_integer_copy(json);
934
935 if(json_is_real(json))
936 return json_real_copy(json);
937
938 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
939 return json;
940
941 return NULL;
942}
943