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