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