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