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