Add equality test for JSON values
[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(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_next(json_t *json, void *iter)
194 {
195     json_object_t *object;
196
197     if(!json_is_object(json) || iter == NULL)
198         return NULL;
199
200     object = json_to_object(json);
201     return hashtable_iter_next(&object->hashtable, iter);
202 }
203
204 const char *json_object_iter_key(void *iter)
205 {
206     if(!iter)
207         return NULL;
208
209     return (const char *)hashtable_iter_key(iter);
210 }
211
212 json_t *json_object_iter_value(void *iter)
213 {
214     if(!iter)
215         return NULL;
216
217     return (json_t *)hashtable_iter_value(iter);
218 }
219
220 static int json_object_equal(json_t *object1, json_t *object2)
221 {
222     void *iter;
223
224     if(json_object_size(object1) != json_object_size(object2))
225         return 0;
226
227     iter = json_object_iter(object1);
228     while(iter)
229     {
230         const char *key;
231         json_t *value1, *value2;
232
233         key = json_object_iter_key(iter);
234         value1 = json_object_iter_value(iter);
235         value2 = json_object_get(object2, key);
236
237         if(!json_equal(value1, value2))
238             return 0;
239
240         iter = json_object_iter_next(object1, iter);
241     }
242
243     return 1;
244 }
245
246
247 /*** array ***/
248
249 json_t *json_array(void)
250 {
251     json_array_t *array = malloc(sizeof(json_array_t));
252     if(!array)
253         return NULL;
254     json_init(&array->json, JSON_ARRAY);
255
256     array->entries = 0;
257     array->size = 8;
258
259     array->table = malloc(array->size * sizeof(json_t *));
260     if(!array->table) {
261         free(array);
262         return NULL;
263     }
264
265     array->visited = 0;
266
267     return &array->json;
268 }
269
270 static void json_delete_array(json_array_t *array)
271 {
272     unsigned int i;
273
274     for(i = 0; i < array->entries; i++)
275         json_decref(array->table[i]);
276
277     free(array->table);
278     free(array);
279 }
280
281 unsigned int json_array_size(const json_t *json)
282 {
283     if(!json_is_array(json))
284         return 0;
285
286     return json_to_array(json)->entries;
287 }
288
289 json_t *json_array_get(const json_t *json, unsigned int index)
290 {
291     json_array_t *array;
292     if(!json_is_array(json))
293         return NULL;
294     array = json_to_array(json);
295
296     if(index >= array->entries)
297         return NULL;
298
299     return array->table[index];
300 }
301
302 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
303 {
304     json_array_t *array;
305
306     if(!value)
307         return -1;
308
309     if(!json_is_array(json) || json == value)
310     {
311         json_decref(value);
312         return -1;
313     }
314     array = json_to_array(json);
315
316     if(index >= array->entries)
317     {
318         json_decref(value);
319         return -1;
320     }
321
322     json_decref(array->table[index]);
323     array->table[index] = value;
324
325     return 0;
326 }
327
328 static void array_move(json_array_t *array, unsigned int dest,
329                        unsigned int src, unsigned int count)
330 {
331     memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
332 }
333
334 static void array_copy(json_t **dest, unsigned int dpos,
335                        json_t **src, unsigned int spos,
336                        unsigned int count)
337 {
338     memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
339 }
340
341 static json_t **json_array_grow(json_array_t *array,
342                                 unsigned int amount,
343                                 int copy)
344 {
345     unsigned int new_size;
346     json_t **old_table, **new_table;
347
348     if(array->entries + amount <= array->size)
349         return array->table;
350
351     old_table = array->table;
352
353     new_size = max(array->size + amount, array->size * 2);
354     new_table = malloc(new_size * sizeof(json_t *));
355     if(!new_table)
356         return NULL;
357
358     array->size = new_size;
359     array->table = new_table;
360
361     if(copy) {
362         array_copy(array->table, 0, old_table, 0, array->entries);
363         free(old_table);
364         return array->table;
365     }
366
367     return old_table;
368 }
369
370 int json_array_append_new(json_t *json, json_t *value)
371 {
372     json_array_t *array;
373
374     if(!value)
375         return -1;
376
377     if(!json_is_array(json) || json == value)
378     {
379         json_decref(value);
380         return -1;
381     }
382     array = json_to_array(json);
383
384     if(!json_array_grow(array, 1, 1)) {
385         json_decref(value);
386         return -1;
387     }
388
389     array->table[array->entries] = value;
390     array->entries++;
391
392     return 0;
393 }
394
395 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
396 {
397     json_array_t *array;
398     json_t **old_table;
399
400     if(!value)
401         return -1;
402
403     if(!json_is_array(json) || json == value) {
404         json_decref(value);
405         return -1;
406     }
407     array = json_to_array(json);
408
409     if(index > array->entries) {
410         json_decref(value);
411         return -1;
412     }
413
414     old_table = json_array_grow(array, 1, 0);
415     if(!old_table) {
416         json_decref(value);
417         return -1;
418     }
419
420     if(old_table != array->table) {
421         array_copy(array->table, 0, old_table, 0, index);
422         array_copy(array->table, index + 1, old_table, index,
423                    array->entries - index);
424         free(old_table);
425     }
426     else
427         array_move(array, index + 1, index, array->entries - index);
428
429     array->table[index] = value;
430     array->entries++;
431
432     return 0;
433 }
434
435 int json_array_remove(json_t *json, unsigned int index)
436 {
437     json_array_t *array;
438
439     if(!json_is_array(json))
440         return -1;
441     array = json_to_array(json);
442
443     if(index >= array->entries)
444         return -1;
445
446     json_decref(array->table[index]);
447
448     array_move(array, index, index + 1, array->entries - index);
449     array->entries--;
450
451     return 0;
452 }
453
454 int json_array_clear(json_t *json)
455 {
456     json_array_t *array;
457     unsigned int i;
458
459     if(!json_is_array(json))
460         return -1;
461     array = json_to_array(json);
462
463     for(i = 0; i < array->entries; i++)
464         json_decref(array->table[i]);
465
466     array->entries = 0;
467     return 0;
468 }
469
470 int json_array_extend(json_t *json, json_t *other_json)
471 {
472     json_array_t *array, *other;
473     unsigned int i;
474
475     if(!json_is_array(json) || !json_is_array(other_json))
476         return -1;
477     array = json_to_array(json);
478     other = json_to_array(other_json);
479
480     if(!json_array_grow(array, other->entries, 1))
481         return -1;
482
483     for(i = 0; i < other->entries; i++)
484         json_incref(other->table[i]);
485
486     array_copy(array->table, array->entries, other->table, 0, other->entries);
487
488     array->entries += other->entries;
489     return 0;
490 }
491
492 static int json_array_equal(json_t *array1, json_t *array2)
493 {
494     unsigned int i, size;
495
496     size = json_array_size(array1);
497     if(size != json_array_size(array2))
498         return 0;
499
500     for(i = 0; i < size; i++)
501     {
502         json_t *value1, *value2;
503
504         value1 = json_array_get(array1, i);
505         value2 = json_array_get(array2, i);
506
507         if(!json_equal(value1, value2))
508             return 0;
509     }
510
511     return 1;
512 }
513
514
515 /*** string ***/
516
517 json_t *json_string_nocheck(const char *value)
518 {
519     json_string_t *string;
520
521     if(!value)
522         return NULL;
523
524     string = malloc(sizeof(json_string_t));
525     if(!string)
526         return NULL;
527     json_init(&string->json, JSON_STRING);
528
529     string->value = strdup(value);
530     if(!string->value) {
531         free(string);
532         return NULL;
533     }
534
535     return &string->json;
536 }
537
538 json_t *json_string(const char *value)
539 {
540     if(!value || !utf8_check_string(value, -1))
541         return NULL;
542
543     return json_string_nocheck(value);
544 }
545
546 const char *json_string_value(const json_t *json)
547 {
548     if(!json_is_string(json))
549         return NULL;
550
551     return json_to_string(json)->value;
552 }
553
554 int json_string_set_nocheck(json_t *json, const char *value)
555 {
556     char *dup;
557     json_string_t *string;
558
559     dup = strdup(value);
560     if(!dup)
561         return -1;
562
563     string = json_to_string(json);
564     free(string->value);
565     string->value = dup;
566
567     return 0;
568 }
569
570 int json_string_set(json_t *json, const char *value)
571 {
572     if(!value || !utf8_check_string(value, -1))
573         return -1;
574
575     return json_string_set_nocheck(json, value);
576 }
577
578 static void json_delete_string(json_string_t *string)
579 {
580     free(string->value);
581     free(string);
582 }
583
584 static int json_string_equal(json_t *string1, json_t *string2)
585 {
586     return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
587 }
588
589 /*** integer ***/
590
591 json_t *json_integer(int value)
592 {
593     json_integer_t *integer = malloc(sizeof(json_integer_t));
594     if(!integer)
595         return NULL;
596     json_init(&integer->json, JSON_INTEGER);
597
598     integer->value = value;
599     return &integer->json;
600 }
601
602 int json_integer_value(const json_t *json)
603 {
604     if(!json_is_integer(json))
605         return 0;
606
607     return json_to_integer(json)->value;
608 }
609
610 int json_integer_set(json_t *json, int value)
611 {
612     if(!json_is_integer(json))
613         return -1;
614
615     json_to_integer(json)->value = value;
616
617     return 0;
618 }
619
620 static void json_delete_integer(json_integer_t *integer)
621 {
622     free(integer);
623 }
624
625 static int json_integer_equal(json_t *integer1, json_t *integer2)
626 {
627     return json_integer_value(integer1) == json_integer_value(integer2);
628 }
629
630 /*** real ***/
631
632 json_t *json_real(double value)
633 {
634     json_real_t *real = malloc(sizeof(json_real_t));
635     if(!real)
636         return NULL;
637     json_init(&real->json, JSON_REAL);
638
639     real->value = value;
640     return &real->json;
641 }
642
643 double json_real_value(const json_t *json)
644 {
645     if(!json_is_real(json))
646         return 0;
647
648     return json_to_real(json)->value;
649 }
650
651 int json_real_set(json_t *json, double value)
652 {
653     if(!json_is_real(json))
654         return 0;
655
656     json_to_real(json)->value = value;
657
658     return 0;
659 }
660
661 static void json_delete_real(json_real_t *real)
662 {
663     free(real);
664 }
665
666 static int json_real_equal(json_t *real1, json_t *real2)
667 {
668     return json_real_value(real1) == json_real_value(real2);
669 }
670
671 /*** number ***/
672
673 double json_number_value(const json_t *json)
674 {
675     if(json_is_integer(json))
676         return json_integer_value(json);
677     else if(json_is_real(json))
678         return json_real_value(json);
679     else
680         return 0.0;
681 }
682
683
684 /*** simple values ***/
685
686 json_t *json_true(void)
687 {
688     static json_t the_true = {
689         .type = JSON_TRUE,
690         .refcount = (unsigned int)1
691     };
692     return &the_true;
693 }
694
695
696 json_t *json_false(void)
697 {
698     static json_t the_false = {
699         .type = JSON_FALSE,
700         .refcount = (unsigned int)1
701     };
702     return &the_false;
703 }
704
705
706 json_t *json_null(void)
707 {
708     static json_t the_null = {
709         .type = JSON_NULL,
710         .refcount = (unsigned int)1
711     };
712     return &the_null;
713 }
714
715
716 /*** deletion ***/
717
718 void json_delete(json_t *json)
719 {
720     if(json_is_object(json))
721         json_delete_object(json_to_object(json));
722
723     else if(json_is_array(json))
724         json_delete_array(json_to_array(json));
725
726     else if(json_is_string(json))
727         json_delete_string(json_to_string(json));
728
729     else if(json_is_integer(json))
730         json_delete_integer(json_to_integer(json));
731
732     else if(json_is_real(json))
733         json_delete_real(json_to_real(json));
734
735     /* json_delete is not called for true, false or null */
736 }
737
738
739 /*** equality ***/
740
741 int json_equal(json_t *json1, json_t *json2)
742 {
743     if(!json1 || !json2)
744         return 0;
745
746     if(json_typeof(json1) != json_typeof(json2))
747         return 0;
748
749     /* this covers true, false and null as they are singletons */
750     if(json1 == json2)
751         return 1;
752
753     if(json_is_object(json1))
754         return json_object_equal(json1, json2);
755
756     if(json_is_array(json1))
757         return json_array_equal(json1, json2);
758
759     if(json_is_string(json1))
760         return json_string_equal(json1, json2);
761
762     if(json_is_integer(json1))
763         return json_integer_equal(json1, json2);
764
765     if(json_is_real(json1))
766         return json_real_equal(json1, json2);
767
768     return 0;
769 }