2 * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
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.
13 #include "hashtable.h"
14 #include "jansson_private.h"
19 static inline void json_init(json_t *json, json_type type)
28 static unsigned int hash_string(const void *key)
30 const char *str = (const char *)key;
31 unsigned int hash = 5381;
34 while((c = (unsigned int)*str))
36 hash = ((hash << 5) + hash) + c;
43 static int string_equal(const void *key1, const void *key2)
45 return strcmp((const char *)key1, (const char *)key2) == 0;
48 static void value_decref(void *value)
50 json_decref((json_t *)value);
53 json_t *json_object(void)
55 json_object_t *object = malloc(sizeof(json_object_t));
58 json_init(&object->json, JSON_OBJECT);
60 if(hashtable_init(&object->hashtable, hash_string, string_equal,
72 static void json_delete_object(json_object_t *object)
74 hashtable_close(&object->hashtable);
78 unsigned int json_object_size(const json_t *json)
80 json_object_t *object;
82 if(!json_is_object(json))
85 object = json_to_object(json);
86 return object->hashtable.size;
89 json_t *json_object_get(const json_t *json, const char *key)
91 json_object_t *object;
93 if(!json_is_object(json))
96 object = json_to_object(json);
97 return hashtable_get(&object->hashtable, key);
100 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
102 json_object_t *object;
107 if(!json_is_object(json) || json == value)
112 object = json_to_object(json);
114 if(hashtable_set(&object->hashtable, strdup(key), value))
123 int json_object_set_new(json_t *json, const char *key, json_t *value)
125 if(!key || !utf8_check_string(key, -1))
131 return json_object_set_new_nocheck(json, key, value);
134 int json_object_del(json_t *json, const char *key)
136 json_object_t *object;
138 if(!json_is_object(json))
141 object = json_to_object(json);
142 return hashtable_del(&object->hashtable, key);
145 int json_object_clear(json_t *json)
147 json_object_t *object;
149 if(!json_is_object(json))
152 object = json_to_object(json);
153 hashtable_clear(&object->hashtable);
158 int json_object_update(json_t *object, json_t *other)
162 if(!json_is_object(object) || !json_is_object(other))
165 iter = json_object_iter(other);
170 key = json_object_iter_key(iter);
171 value = json_object_iter_value(iter);
173 if(json_object_set_nocheck(object, key, value))
176 iter = json_object_iter_next(other, iter);
182 void *json_object_iter(json_t *json)
184 json_object_t *object;
186 if(!json_is_object(json))
189 object = json_to_object(json);
190 return hashtable_iter(&object->hashtable);
193 void *json_object_iter_next(json_t *json, void *iter)
195 json_object_t *object;
197 if(!json_is_object(json) || iter == NULL)
200 object = json_to_object(json);
201 return hashtable_iter_next(&object->hashtable, iter);
204 const char *json_object_iter_key(void *iter)
209 return (const char *)hashtable_iter_key(iter);
212 json_t *json_object_iter_value(void *iter)
217 return (json_t *)hashtable_iter_value(iter);
220 static int json_object_equal(json_t *object1, json_t *object2)
224 if(json_object_size(object1) != json_object_size(object2))
227 iter = json_object_iter(object1);
231 json_t *value1, *value2;
233 key = json_object_iter_key(iter);
234 value1 = json_object_iter_value(iter);
235 value2 = json_object_get(object2, key);
237 if(!json_equal(value1, value2))
240 iter = json_object_iter_next(object1, iter);
246 static json_t *json_object_copy(json_t *object)
251 result = json_object();
255 iter = json_object_iter(object);
261 key = json_object_iter_key(iter);
262 value = json_object_iter_value(iter);
263 json_object_set_nocheck(result, key, value);
265 iter = json_object_iter_next(object, iter);
271 static json_t *json_object_deep_copy(json_t *object)
276 result = json_object();
280 iter = json_object_iter(object);
286 key = json_object_iter_key(iter);
287 value = json_object_iter_value(iter);
288 json_object_set_new_nocheck(result, key, json_deep_copy(value));
290 iter = json_object_iter_next(object, iter);
299 json_t *json_array(void)
301 json_array_t *array = malloc(sizeof(json_array_t));
304 json_init(&array->json, JSON_ARRAY);
309 array->table = malloc(array->size * sizeof(json_t *));
320 static void json_delete_array(json_array_t *array)
324 for(i = 0; i < array->entries; i++)
325 json_decref(array->table[i]);
331 unsigned int json_array_size(const json_t *json)
333 if(!json_is_array(json))
336 return json_to_array(json)->entries;
339 json_t *json_array_get(const json_t *json, unsigned int index)
342 if(!json_is_array(json))
344 array = json_to_array(json);
346 if(index >= array->entries)
349 return array->table[index];
352 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
359 if(!json_is_array(json) || json == value)
364 array = json_to_array(json);
366 if(index >= array->entries)
372 json_decref(array->table[index]);
373 array->table[index] = value;
378 static void array_move(json_array_t *array, unsigned int dest,
379 unsigned int src, unsigned int count)
381 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
384 static void array_copy(json_t **dest, unsigned int dpos,
385 json_t **src, unsigned int spos,
388 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
391 static json_t **json_array_grow(json_array_t *array,
395 unsigned int new_size;
396 json_t **old_table, **new_table;
398 if(array->entries + amount <= array->size)
401 old_table = array->table;
403 new_size = max(array->size + amount, array->size * 2);
404 new_table = malloc(new_size * sizeof(json_t *));
408 array->size = new_size;
409 array->table = new_table;
412 array_copy(array->table, 0, old_table, 0, array->entries);
420 int json_array_append_new(json_t *json, json_t *value)
427 if(!json_is_array(json) || json == value)
432 array = json_to_array(json);
434 if(!json_array_grow(array, 1, 1)) {
439 array->table[array->entries] = value;
445 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
453 if(!json_is_array(json) || json == value) {
457 array = json_to_array(json);
459 if(index > array->entries) {
464 old_table = json_array_grow(array, 1, 0);
470 if(old_table != array->table) {
471 array_copy(array->table, 0, old_table, 0, index);
472 array_copy(array->table, index + 1, old_table, index,
473 array->entries - index);
477 array_move(array, index + 1, index, array->entries - index);
479 array->table[index] = value;
485 int json_array_remove(json_t *json, unsigned int index)
489 if(!json_is_array(json))
491 array = json_to_array(json);
493 if(index >= array->entries)
496 json_decref(array->table[index]);
498 array_move(array, index, index + 1, array->entries - index);
504 int json_array_clear(json_t *json)
509 if(!json_is_array(json))
511 array = json_to_array(json);
513 for(i = 0; i < array->entries; i++)
514 json_decref(array->table[i]);
520 int json_array_extend(json_t *json, json_t *other_json)
522 json_array_t *array, *other;
525 if(!json_is_array(json) || !json_is_array(other_json))
527 array = json_to_array(json);
528 other = json_to_array(other_json);
530 if(!json_array_grow(array, other->entries, 1))
533 for(i = 0; i < other->entries; i++)
534 json_incref(other->table[i]);
536 array_copy(array->table, array->entries, other->table, 0, other->entries);
538 array->entries += other->entries;
542 static int json_array_equal(json_t *array1, json_t *array2)
544 unsigned int i, size;
546 size = json_array_size(array1);
547 if(size != json_array_size(array2))
550 for(i = 0; i < size; i++)
552 json_t *value1, *value2;
554 value1 = json_array_get(array1, i);
555 value2 = json_array_get(array2, i);
557 if(!json_equal(value1, value2))
564 static json_t *json_array_copy(json_t *array)
569 result = json_array();
573 for(i = 0; i < json_array_size(array); i++)
574 json_array_append(result, json_array_get(array, i));
579 static json_t *json_array_deep_copy(json_t *array)
584 result = json_array();
588 for(i = 0; i < json_array_size(array); i++)
589 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
596 json_t *json_string_nocheck(const char *value)
598 json_string_t *string;
603 string = malloc(sizeof(json_string_t));
606 json_init(&string->json, JSON_STRING);
608 string->value = strdup(value);
614 return &string->json;
617 json_t *json_string(const char *value)
619 if(!value || !utf8_check_string(value, -1))
622 return json_string_nocheck(value);
625 const char *json_string_value(const json_t *json)
627 if(!json_is_string(json))
630 return json_to_string(json)->value;
633 int json_string_set_nocheck(json_t *json, const char *value)
636 json_string_t *string;
642 string = json_to_string(json);
649 int json_string_set(json_t *json, const char *value)
651 if(!value || !utf8_check_string(value, -1))
654 return json_string_set_nocheck(json, value);
657 static void json_delete_string(json_string_t *string)
663 static int json_string_equal(json_t *string1, json_t *string2)
665 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
668 static json_t *json_string_copy(json_t *string)
670 return json_string_nocheck(json_string_value(string));
676 json_t *json_integer(int value)
678 json_integer_t *integer = malloc(sizeof(json_integer_t));
681 json_init(&integer->json, JSON_INTEGER);
683 integer->value = value;
684 return &integer->json;
687 int json_integer_value(const json_t *json)
689 if(!json_is_integer(json))
692 return json_to_integer(json)->value;
695 int json_integer_set(json_t *json, int value)
697 if(!json_is_integer(json))
700 json_to_integer(json)->value = value;
705 static void json_delete_integer(json_integer_t *integer)
710 static int json_integer_equal(json_t *integer1, json_t *integer2)
712 return json_integer_value(integer1) == json_integer_value(integer2);
715 static json_t *json_integer_copy(json_t *integer)
717 return json_integer(json_integer_value(integer));
723 json_t *json_real(double value)
725 json_real_t *real = malloc(sizeof(json_real_t));
728 json_init(&real->json, JSON_REAL);
734 double json_real_value(const json_t *json)
736 if(!json_is_real(json))
739 return json_to_real(json)->value;
742 int json_real_set(json_t *json, double value)
744 if(!json_is_real(json))
747 json_to_real(json)->value = value;
752 static void json_delete_real(json_real_t *real)
757 static int json_real_equal(json_t *real1, json_t *real2)
759 return json_real_value(real1) == json_real_value(real2);
762 static json_t *json_real_copy(json_t *real)
764 return json_real(json_real_value(real));
770 double json_number_value(const json_t *json)
772 if(json_is_integer(json))
773 return json_integer_value(json);
774 else if(json_is_real(json))
775 return json_real_value(json);
781 /*** simple values ***/
783 json_t *json_true(void)
785 static json_t the_true = {
787 .refcount = (unsigned int)-1
793 json_t *json_false(void)
795 static json_t the_false = {
797 .refcount = (unsigned int)-1
803 json_t *json_null(void)
805 static json_t the_null = {
807 .refcount = (unsigned int)-1
815 void json_delete(json_t *json)
817 if(json_is_object(json))
818 json_delete_object(json_to_object(json));
820 else if(json_is_array(json))
821 json_delete_array(json_to_array(json));
823 else if(json_is_string(json))
824 json_delete_string(json_to_string(json));
826 else if(json_is_integer(json))
827 json_delete_integer(json_to_integer(json));
829 else if(json_is_real(json))
830 json_delete_real(json_to_real(json));
832 /* json_delete is not called for true, false or null */
838 int json_equal(json_t *json1, json_t *json2)
843 if(json_typeof(json1) != json_typeof(json2))
846 /* this covers true, false and null as they are singletons */
850 if(json_is_object(json1))
851 return json_object_equal(json1, json2);
853 if(json_is_array(json1))
854 return json_array_equal(json1, json2);
856 if(json_is_string(json1))
857 return json_string_equal(json1, json2);
859 if(json_is_integer(json1))
860 return json_integer_equal(json1, json2);
862 if(json_is_real(json1))
863 return json_real_equal(json1, json2);
871 json_t *json_copy(json_t *json)
876 if(json_is_object(json))
877 return json_object_copy(json);
879 if(json_is_array(json))
880 return json_array_copy(json);
882 if(json_is_string(json))
883 return json_string_copy(json);
885 if(json_is_integer(json))
886 return json_integer_copy(json);
888 if(json_is_real(json))
889 return json_real_copy(json);
891 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
897 json_t *json_deep_copy(json_t *json)
902 if(json_is_object(json))
903 return json_object_deep_copy(json);
905 if(json_is_array(json))
906 return json_array_deep_copy(json);
908 /* for the rest of the types, deep copying doesn't differ from
911 if(json_is_string(json))
912 return json_string_copy(json);
914 if(json_is_integer(json))
915 return json_integer_copy(json);
917 if(json_is_real(json))
918 return json_real_copy(json);
920 if(json_is_true(json) || json_is_false(json) || json_is_null(json))