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