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