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