2 * Copyright (c) 2009 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_at(json_t *json, const char *key)
195 json_object_t *object;
197 if(!key || !json_is_object(json))
200 object = json_to_object(json);
201 return hashtable_iter_at(&object->hashtable, key);
204 void *json_object_iter_next(json_t *json, void *iter)
206 json_object_t *object;
208 if(!json_is_object(json) || iter == NULL)
211 object = json_to_object(json);
212 return hashtable_iter_next(&object->hashtable, iter);
215 const char *json_object_iter_key(void *iter)
220 return (const char *)hashtable_iter_key(iter);
223 json_t *json_object_iter_value(void *iter)
228 return (json_t *)hashtable_iter_value(iter);
231 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
233 json_object_t *object;
235 if(!json_is_object(json) || !iter || !value)
238 object = json_to_object(json);
239 hashtable_iter_set(&object->hashtable, iter, value);
244 static int json_object_equal(json_t *object1, json_t *object2)
248 if(json_object_size(object1) != json_object_size(object2))
251 iter = json_object_iter(object1);
255 json_t *value1, *value2;
257 key = json_object_iter_key(iter);
258 value1 = json_object_iter_value(iter);
259 value2 = json_object_get(object2, key);
261 if(!json_equal(value1, value2))
264 iter = json_object_iter_next(object1, iter);
270 static json_t *json_object_copy(json_t *object)
275 result = json_object();
279 iter = json_object_iter(object);
285 key = json_object_iter_key(iter);
286 value = json_object_iter_value(iter);
287 json_object_set_nocheck(result, key, value);
289 iter = json_object_iter_next(object, iter);
295 static json_t *json_object_deep_copy(json_t *object)
300 result = json_object();
304 iter = json_object_iter(object);
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));
314 iter = json_object_iter_next(object, iter);
323 json_t *json_array(void)
325 json_array_t *array = malloc(sizeof(json_array_t));
328 json_init(&array->json, JSON_ARRAY);
333 array->table = malloc(array->size * sizeof(json_t *));
344 static void json_delete_array(json_array_t *array)
348 for(i = 0; i < array->entries; i++)
349 json_decref(array->table[i]);
355 unsigned int json_array_size(const json_t *json)
357 if(!json_is_array(json))
360 return json_to_array(json)->entries;
363 json_t *json_array_get(const json_t *json, unsigned int index)
366 if(!json_is_array(json))
368 array = json_to_array(json);
370 if(index >= array->entries)
373 return array->table[index];
376 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
383 if(!json_is_array(json) || json == value)
388 array = json_to_array(json);
390 if(index >= array->entries)
396 json_decref(array->table[index]);
397 array->table[index] = value;
402 static void array_move(json_array_t *array, unsigned int dest,
403 unsigned int src, unsigned int count)
405 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
408 static void array_copy(json_t **dest, unsigned int dpos,
409 json_t **src, unsigned int spos,
412 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
415 static json_t **json_array_grow(json_array_t *array,
419 unsigned int new_size;
420 json_t **old_table, **new_table;
422 if(array->entries + amount <= array->size)
425 old_table = array->table;
427 new_size = max(array->size + amount, array->size * 2);
428 new_table = malloc(new_size * sizeof(json_t *));
432 array->size = new_size;
433 array->table = new_table;
436 array_copy(array->table, 0, old_table, 0, array->entries);
444 int json_array_append_new(json_t *json, json_t *value)
451 if(!json_is_array(json) || json == value)
456 array = json_to_array(json);
458 if(!json_array_grow(array, 1, 1)) {
463 array->table[array->entries] = value;
469 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
477 if(!json_is_array(json) || json == value) {
481 array = json_to_array(json);
483 if(index > array->entries) {
488 old_table = json_array_grow(array, 1, 0);
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);
501 array_move(array, index + 1, index, array->entries - index);
503 array->table[index] = value;
509 int json_array_remove(json_t *json, unsigned int index)
513 if(!json_is_array(json))
515 array = json_to_array(json);
517 if(index >= array->entries)
520 json_decref(array->table[index]);
522 array_move(array, index, index + 1, array->entries - index);
528 int json_array_clear(json_t *json)
533 if(!json_is_array(json))
535 array = json_to_array(json);
537 for(i = 0; i < array->entries; i++)
538 json_decref(array->table[i]);
544 int json_array_extend(json_t *json, json_t *other_json)
546 json_array_t *array, *other;
549 if(!json_is_array(json) || !json_is_array(other_json))
551 array = json_to_array(json);
552 other = json_to_array(other_json);
554 if(!json_array_grow(array, other->entries, 1))
557 for(i = 0; i < other->entries; i++)
558 json_incref(other->table[i]);
560 array_copy(array->table, array->entries, other->table, 0, other->entries);
562 array->entries += other->entries;
566 static int json_array_equal(json_t *array1, json_t *array2)
568 unsigned int i, size;
570 size = json_array_size(array1);
571 if(size != json_array_size(array2))
574 for(i = 0; i < size; i++)
576 json_t *value1, *value2;
578 value1 = json_array_get(array1, i);
579 value2 = json_array_get(array2, i);
581 if(!json_equal(value1, value2))
588 static json_t *json_array_copy(json_t *array)
593 result = json_array();
597 for(i = 0; i < json_array_size(array); i++)
598 json_array_append(result, json_array_get(array, i));
603 static json_t *json_array_deep_copy(json_t *array)
608 result = json_array();
612 for(i = 0; i < json_array_size(array); i++)
613 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
620 json_t *json_string_nocheck(const char *value)
622 json_string_t *string;
627 string = malloc(sizeof(json_string_t));
630 json_init(&string->json, JSON_STRING);
632 string->value = strdup(value);
638 return &string->json;
641 json_t *json_string(const char *value)
643 if(!value || !utf8_check_string(value, -1))
646 return json_string_nocheck(value);
649 const char *json_string_value(const json_t *json)
651 if(!json_is_string(json))
654 return json_to_string(json)->value;
657 int json_string_set_nocheck(json_t *json, const char *value)
660 json_string_t *string;
666 string = json_to_string(json);
673 int json_string_set(json_t *json, const char *value)
675 if(!value || !utf8_check_string(value, -1))
678 return json_string_set_nocheck(json, value);
681 static void json_delete_string(json_string_t *string)
687 static int json_string_equal(json_t *string1, json_t *string2)
689 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
692 static json_t *json_string_copy(json_t *string)
694 return json_string_nocheck(json_string_value(string));
700 json_t *json_integer(int value)
702 json_integer_t *integer = malloc(sizeof(json_integer_t));
705 json_init(&integer->json, JSON_INTEGER);
707 integer->value = value;
708 return &integer->json;
711 int json_integer_value(const json_t *json)
713 if(!json_is_integer(json))
716 return json_to_integer(json)->value;
719 int json_integer_set(json_t *json, int value)
721 if(!json_is_integer(json))
724 json_to_integer(json)->value = value;
729 static void json_delete_integer(json_integer_t *integer)
734 static int json_integer_equal(json_t *integer1, json_t *integer2)
736 return json_integer_value(integer1) == json_integer_value(integer2);
739 static json_t *json_integer_copy(json_t *integer)
741 return json_integer(json_integer_value(integer));
747 json_t *json_real(double value)
749 json_real_t *real = malloc(sizeof(json_real_t));
752 json_init(&real->json, JSON_REAL);
758 double json_real_value(const json_t *json)
760 if(!json_is_real(json))
763 return json_to_real(json)->value;
766 int json_real_set(json_t *json, double value)
768 if(!json_is_real(json))
771 json_to_real(json)->value = value;
776 static void json_delete_real(json_real_t *real)
781 static int json_real_equal(json_t *real1, json_t *real2)
783 return json_real_value(real1) == json_real_value(real2);
786 static json_t *json_real_copy(json_t *real)
788 return json_real(json_real_value(real));
794 double json_number_value(const json_t *json)
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);
805 /*** simple values ***/
807 json_t *json_true(void)
809 static json_t the_true = {
811 .refcount = (unsigned int)1
817 json_t *json_false(void)
819 static json_t the_false = {
821 .refcount = (unsigned int)1
827 json_t *json_null(void)
829 static json_t the_null = {
831 .refcount = (unsigned int)1
839 void json_delete(json_t *json)
841 if(json_is_object(json))
842 json_delete_object(json_to_object(json));
844 else if(json_is_array(json))
845 json_delete_array(json_to_array(json));
847 else if(json_is_string(json))
848 json_delete_string(json_to_string(json));
850 else if(json_is_integer(json))
851 json_delete_integer(json_to_integer(json));
853 else if(json_is_real(json))
854 json_delete_real(json_to_real(json));
856 /* json_delete is not called for true, false or null */
862 int json_equal(json_t *json1, json_t *json2)
867 if(json_typeof(json1) != json_typeof(json2))
870 /* this covers true, false and null as they are singletons */
874 if(json_is_object(json1))
875 return json_object_equal(json1, json2);
877 if(json_is_array(json1))
878 return json_array_equal(json1, json2);
880 if(json_is_string(json1))
881 return json_string_equal(json1, json2);
883 if(json_is_integer(json1))
884 return json_integer_equal(json1, json2);
886 if(json_is_real(json1))
887 return json_real_equal(json1, json2);
895 json_t *json_copy(json_t *json)
900 if(json_is_object(json))
901 return json_object_copy(json);
903 if(json_is_array(json))
904 return json_array_copy(json);
906 if(json_is_string(json))
907 return json_string_copy(json);
909 if(json_is_integer(json))
910 return json_integer_copy(json);
912 if(json_is_real(json))
913 return json_real_copy(json);
915 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
921 json_t *json_deep_copy(json_t *json)
926 if(json_is_object(json))
927 return json_object_deep_copy(json);
929 if(json_is_array(json))
930 return json_array_deep_copy(json);
932 /* for the rest of the types, deep copying doesn't differ from
935 if(json_is_string(json))
936 return json_string_copy(json);
938 if(json_is_integer(json))
939 return json_integer_copy(json);
941 if(json_is_real(json))
942 return json_real_copy(json);
944 if(json_is_true(json) || json_is_false(json) || json_is_null(json))