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