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