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