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