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