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