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