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.
16 #include "hashtable.h"
17 #include "jansson_private.h"
22 static inline void json_init(json_t *json, json_type type)
31 static unsigned int hash_string(const void *key)
33 const char *str = (const char *)key;
34 unsigned int hash = 5381;
37 while((c = (unsigned int)*str))
39 hash = ((hash << 5) + hash) + c;
46 static int string_equal(const void *key1, const void *key2)
48 return strcmp((const char *)key1, (const char *)key2) == 0;
51 static void value_decref(void *value)
53 json_decref((json_t *)value);
56 json_t *json_object(void)
58 json_object_t *object = malloc(sizeof(json_object_t));
61 json_init(&object->json, JSON_OBJECT);
63 if(hashtable_init(&object->hashtable, hash_string, string_equal,
75 static void json_delete_object(json_object_t *object)
77 hashtable_close(&object->hashtable);
81 unsigned int json_object_size(const json_t *json)
83 json_object_t *object;
85 if(!json_is_object(json))
88 object = json_to_object(json);
89 return object->hashtable.size;
92 json_t *json_object_get(const json_t *json, const char *key)
94 json_object_t *object;
96 if(!json_is_object(json))
99 object = json_to_object(json);
100 return hashtable_get(&object->hashtable, key);
103 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
105 json_object_t *object;
110 if(!json_is_object(json) || json == value)
115 object = json_to_object(json);
117 if(hashtable_set(&object->hashtable, strdup(key), value))
126 int json_object_set_new(json_t *json, const char *key, json_t *value)
128 if(!key || !utf8_check_string(key, -1))
134 return json_object_set_new_nocheck(json, key, value);
137 int json_object_del(json_t *json, const char *key)
139 json_object_t *object;
141 if(!json_is_object(json))
144 object = json_to_object(json);
145 return hashtable_del(&object->hashtable, key);
148 int json_object_clear(json_t *json)
150 json_object_t *object;
152 if(!json_is_object(json))
155 object = json_to_object(json);
156 hashtable_clear(&object->hashtable);
161 int json_object_update(json_t *object, json_t *other)
165 if(!json_is_object(object) || !json_is_object(other))
168 iter = json_object_iter(other);
173 key = json_object_iter_key(iter);
174 value = json_object_iter_value(iter);
176 if(json_object_set_nocheck(object, key, value))
179 iter = json_object_iter_next(other, iter);
185 void *json_object_iter(json_t *json)
187 json_object_t *object;
189 if(!json_is_object(json))
192 object = json_to_object(json);
193 return hashtable_iter(&object->hashtable);
196 void *json_object_iter_next(json_t *json, void *iter)
198 json_object_t *object;
200 if(!json_is_object(json) || iter == NULL)
203 object = json_to_object(json);
204 return hashtable_iter_next(&object->hashtable, iter);
207 const char *json_object_iter_key(void *iter)
212 return (const char *)hashtable_iter_key(iter);
215 json_t *json_object_iter_value(void *iter)
220 return (json_t *)hashtable_iter_value(iter);
223 static int json_object_equal(json_t *object1, json_t *object2)
227 if(json_object_size(object1) != json_object_size(object2))
230 iter = json_object_iter(object1);
234 json_t *value1, *value2;
236 key = json_object_iter_key(iter);
237 value1 = json_object_iter_value(iter);
238 value2 = json_object_get(object2, key);
240 if(!json_equal(value1, value2))
243 iter = json_object_iter_next(object1, iter);
249 static json_t *json_object_copy(json_t *object)
254 result = json_object();
258 iter = json_object_iter(object);
264 key = json_object_iter_key(iter);
265 value = json_object_iter_value(iter);
266 json_object_set_nocheck(result, key, value);
268 iter = json_object_iter_next(object, iter);
274 static json_t *json_object_deep_copy(json_t *object)
279 result = json_object();
283 iter = json_object_iter(object);
289 key = json_object_iter_key(iter);
290 value = json_object_iter_value(iter);
291 json_object_set_new_nocheck(result, key, json_deep_copy(value));
293 iter = json_object_iter_next(object, iter);
302 json_t *json_array(void)
304 json_array_t *array = malloc(sizeof(json_array_t));
307 json_init(&array->json, JSON_ARRAY);
312 array->table = malloc(array->size * sizeof(json_t *));
323 static void json_delete_array(json_array_t *array)
327 for(i = 0; i < array->entries; i++)
328 json_decref(array->table[i]);
334 unsigned int json_array_size(const json_t *json)
336 if(!json_is_array(json))
339 return json_to_array(json)->entries;
342 json_t *json_array_get(const json_t *json, unsigned int index)
345 if(!json_is_array(json))
347 array = json_to_array(json);
349 if(index >= array->entries)
352 return array->table[index];
355 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
362 if(!json_is_array(json) || json == value)
367 array = json_to_array(json);
369 if(index >= array->entries)
375 json_decref(array->table[index]);
376 array->table[index] = value;
381 static void array_move(json_array_t *array, unsigned int dest,
382 unsigned int src, unsigned int count)
384 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
387 static void array_copy(json_t **dest, unsigned int dpos,
388 json_t **src, unsigned int spos,
391 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
394 static json_t **json_array_grow(json_array_t *array,
398 unsigned int new_size;
399 json_t **old_table, **new_table;
401 if(array->entries + amount <= array->size)
404 old_table = array->table;
406 new_size = max(array->size + amount, array->size * 2);
407 new_table = malloc(new_size * sizeof(json_t *));
411 array->size = new_size;
412 array->table = new_table;
415 array_copy(array->table, 0, old_table, 0, array->entries);
423 int json_array_append_new(json_t *json, json_t *value)
430 if(!json_is_array(json) || json == value)
435 array = json_to_array(json);
437 if(!json_array_grow(array, 1, 1)) {
442 array->table[array->entries] = value;
448 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
456 if(!json_is_array(json) || json == value) {
460 array = json_to_array(json);
462 if(index > array->entries) {
467 old_table = json_array_grow(array, 1, 0);
473 if(old_table != array->table) {
474 array_copy(array->table, 0, old_table, 0, index);
475 array_copy(array->table, index + 1, old_table, index,
476 array->entries - index);
480 array_move(array, index + 1, index, array->entries - index);
482 array->table[index] = value;
488 int json_array_remove(json_t *json, unsigned int index)
492 if(!json_is_array(json))
494 array = json_to_array(json);
496 if(index >= array->entries)
499 json_decref(array->table[index]);
501 array_move(array, index, index + 1, array->entries - index);
507 int json_array_clear(json_t *json)
512 if(!json_is_array(json))
514 array = json_to_array(json);
516 for(i = 0; i < array->entries; i++)
517 json_decref(array->table[i]);
523 int json_array_extend(json_t *json, json_t *other_json)
525 json_array_t *array, *other;
528 if(!json_is_array(json) || !json_is_array(other_json))
530 array = json_to_array(json);
531 other = json_to_array(other_json);
533 if(!json_array_grow(array, other->entries, 1))
536 for(i = 0; i < other->entries; i++)
537 json_incref(other->table[i]);
539 array_copy(array->table, array->entries, other->table, 0, other->entries);
541 array->entries += other->entries;
545 static int json_array_equal(json_t *array1, json_t *array2)
547 unsigned int i, size;
549 size = json_array_size(array1);
550 if(size != json_array_size(array2))
553 for(i = 0; i < size; i++)
555 json_t *value1, *value2;
557 value1 = json_array_get(array1, i);
558 value2 = json_array_get(array2, i);
560 if(!json_equal(value1, value2))
567 static json_t *json_array_copy(json_t *array)
572 result = json_array();
576 for(i = 0; i < json_array_size(array); i++)
577 json_array_append(result, json_array_get(array, i));
582 static json_t *json_array_deep_copy(json_t *array)
587 result = json_array();
591 for(i = 0; i < json_array_size(array); i++)
592 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
599 json_t *json_string_nocheck(const char *value)
601 json_string_t *string;
606 string = malloc(sizeof(json_string_t));
609 json_init(&string->json, JSON_STRING);
611 string->value = strdup(value);
617 return &string->json;
620 json_t *json_string(const char *value)
622 if(!value || !utf8_check_string(value, -1))
625 return json_string_nocheck(value);
628 const char *json_string_value(const json_t *json)
630 if(!json_is_string(json))
633 return json_to_string(json)->value;
636 int json_string_set_nocheck(json_t *json, const char *value)
639 json_string_t *string;
645 string = json_to_string(json);
652 int json_string_set(json_t *json, const char *value)
654 if(!value || !utf8_check_string(value, -1))
657 return json_string_set_nocheck(json, value);
660 static void json_delete_string(json_string_t *string)
666 static int json_string_equal(json_t *string1, json_t *string2)
668 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
671 static json_t *json_string_copy(json_t *string)
673 return json_string_nocheck(json_string_value(string));
679 json_t *json_integer(int value)
681 json_integer_t *integer = malloc(sizeof(json_integer_t));
684 json_init(&integer->json, JSON_INTEGER);
686 integer->value = value;
687 return &integer->json;
690 int json_integer_value(const json_t *json)
692 if(!json_is_integer(json))
695 return json_to_integer(json)->value;
698 int json_integer_set(json_t *json, int value)
700 if(!json_is_integer(json))
703 json_to_integer(json)->value = value;
708 static void json_delete_integer(json_integer_t *integer)
713 static int json_integer_equal(json_t *integer1, json_t *integer2)
715 return json_integer_value(integer1) == json_integer_value(integer2);
718 static json_t *json_integer_copy(json_t *integer)
720 return json_integer(json_integer_value(integer));
726 json_t *json_real(double value)
728 json_real_t *real = malloc(sizeof(json_real_t));
731 json_init(&real->json, JSON_REAL);
737 double json_real_value(const json_t *json)
739 if(!json_is_real(json))
742 return json_to_real(json)->value;
745 int json_real_set(json_t *json, double value)
747 if(!json_is_real(json))
750 json_to_real(json)->value = value;
755 static void json_delete_real(json_real_t *real)
760 static int json_real_equal(json_t *real1, json_t *real2)
762 return json_real_value(real1) == json_real_value(real2);
765 static json_t *json_real_copy(json_t *real)
767 return json_real(json_real_value(real));
773 double json_number_value(const json_t *json)
775 if(json_is_integer(json))
776 return json_integer_value(json);
777 else if(json_is_real(json))
778 return json_real_value(json);
784 /*** simple values ***/
786 json_t *json_true(void)
788 static json_t the_true = {
790 .refcount = (unsigned int)-1
796 json_t *json_false(void)
798 static json_t the_false = {
800 .refcount = (unsigned int)-1
806 json_t *json_null(void)
808 static json_t the_null = {
810 .refcount = (unsigned int)-1
818 void json_delete(json_t *json)
820 if(json_is_object(json))
821 json_delete_object(json_to_object(json));
823 else if(json_is_array(json))
824 json_delete_array(json_to_array(json));
826 else if(json_is_string(json))
827 json_delete_string(json_to_string(json));
829 else if(json_is_integer(json))
830 json_delete_integer(json_to_integer(json));
832 else if(json_is_real(json))
833 json_delete_real(json_to_real(json));
835 /* json_delete is not called for true, false or null */
841 int json_equal(json_t *json1, json_t *json2)
846 if(json_typeof(json1) != json_typeof(json2))
849 /* this covers true, false and null as they are singletons */
853 if(json_is_object(json1))
854 return json_object_equal(json1, json2);
856 if(json_is_array(json1))
857 return json_array_equal(json1, json2);
859 if(json_is_string(json1))
860 return json_string_equal(json1, json2);
862 if(json_is_integer(json1))
863 return json_integer_equal(json1, json2);
865 if(json_is_real(json1))
866 return json_real_equal(json1, json2);
874 json_t *json_copy(json_t *json)
879 if(json_is_object(json))
880 return json_object_copy(json);
882 if(json_is_array(json))
883 return json_array_copy(json);
885 if(json_is_string(json))
886 return json_string_copy(json);
888 if(json_is_integer(json))
889 return json_integer_copy(json);
891 if(json_is_real(json))
892 return json_real_copy(json);
894 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
900 json_t *json_deep_copy(json_t *json)
905 if(json_is_object(json))
906 return json_object_deep_copy(json);
908 if(json_is_array(json))
909 return json_array_deep_copy(json);
911 /* for the rest of the types, deep copying doesn't differ from
914 if(json_is_string(json))
915 return json_string_copy(json);
917 if(json_is_integer(json))
918 return json_integer_copy(json);
920 if(json_is_real(json))
921 return json_real_copy(json);
923 if(json_is_true(json) || json_is_false(json) || json_is_null(json))