2 * Copyright (c) 2009-2011 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.
15 #include "hashtable.h"
16 #include "jansson_private.h"
20 static JSON_INLINE void json_init(json_t *json, json_type type)
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)
35 size_t jsonp_hash_key(const void *ptr)
37 const char *str = ((const object_key_t *)ptr)->key;
42 while((c = (size_t)*str))
44 hash = ((hash << 5) + hash) + c;
51 int jsonp_key_equal(const void *ptr1, const void *ptr2)
53 return strcmp(((const object_key_t *)ptr1)->key,
54 ((const object_key_t *)ptr2)->key) == 0;
57 static void value_decref(void *value)
59 json_decref((json_t *)value);
62 json_t *json_object(void)
64 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
67 json_init(&object->json, JSON_OBJECT);
69 if(hashtable_init(&object->hashtable,
70 jsonp_hash_key, jsonp_key_equal,
71 jsonp_free, value_decref))
83 static void json_delete_object(json_object_t *object)
85 hashtable_close(&object->hashtable);
89 size_t json_object_size(const json_t *json)
91 json_object_t *object;
93 if(!json_is_object(json))
96 object = json_to_object(json);
97 return object->hashtable.size;
100 json_t *json_object_get(const json_t *json, const char *key)
102 json_object_t *object;
104 if(!json_is_object(json))
107 object = json_to_object(json);
108 return hashtable_get(&object->hashtable, string_to_key(key));
111 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
113 json_object_t *object;
119 if(!json_is_object(json) || json == value)
124 object = json_to_object(json);
126 /* offsetof(...) returns the size of object_key_t without the
127 last, flexible member. This way, the correct amount is
129 k = jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1);
133 k->serial = object->serial++;
136 if(hashtable_set(&object->hashtable, k, value))
145 int json_object_set_new(json_t *json, const char *key, json_t *value)
147 if(!key || !utf8_check_string(key, -1))
153 return json_object_set_new_nocheck(json, key, value);
156 int json_object_del(json_t *json, const char *key)
158 json_object_t *object;
160 if(!json_is_object(json))
163 object = json_to_object(json);
164 return hashtable_del(&object->hashtable, string_to_key(key));
167 int json_object_clear(json_t *json)
169 json_object_t *object;
171 if(!json_is_object(json))
174 object = json_to_object(json);
175 hashtable_clear(&object->hashtable);
180 int json_object_update(json_t *object, json_t *other)
184 if(!json_is_object(object) || !json_is_object(other))
187 iter = json_object_iter(other);
192 key = json_object_iter_key(iter);
193 value = json_object_iter_value(iter);
195 if(json_object_set_nocheck(object, key, value))
198 iter = json_object_iter_next(other, iter);
204 void *json_object_iter(json_t *json)
206 json_object_t *object;
208 if(!json_is_object(json))
211 object = json_to_object(json);
212 return hashtable_iter(&object->hashtable);
215 void *json_object_iter_at(json_t *json, const char *key)
217 json_object_t *object;
219 if(!key || !json_is_object(json))
222 object = json_to_object(json);
223 return hashtable_iter_at(&object->hashtable, string_to_key(key));
226 void *json_object_iter_next(json_t *json, void *iter)
228 json_object_t *object;
230 if(!json_is_object(json) || iter == NULL)
233 object = json_to_object(json);
234 return hashtable_iter_next(&object->hashtable, iter);
237 const object_key_t *jsonp_object_iter_fullkey(void *iter)
242 return hashtable_iter_key(iter);
245 const char *json_object_iter_key(void *iter)
250 return jsonp_object_iter_fullkey(iter)->key;
253 json_t *json_object_iter_value(void *iter)
258 return (json_t *)hashtable_iter_value(iter);
261 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
263 json_object_t *object;
265 if(!json_is_object(json) || !iter || !value)
268 object = json_to_object(json);
269 hashtable_iter_set(&object->hashtable, iter, value);
274 static int json_object_equal(json_t *object1, json_t *object2)
278 if(json_object_size(object1) != json_object_size(object2))
281 iter = json_object_iter(object1);
285 json_t *value1, *value2;
287 key = json_object_iter_key(iter);
288 value1 = json_object_iter_value(iter);
289 value2 = json_object_get(object2, key);
291 if(!json_equal(value1, value2))
294 iter = json_object_iter_next(object1, iter);
300 static json_t *json_object_copy(json_t *object)
305 result = json_object();
309 iter = json_object_iter(object);
315 key = json_object_iter_key(iter);
316 value = json_object_iter_value(iter);
317 json_object_set_nocheck(result, key, value);
319 iter = json_object_iter_next(object, iter);
325 static json_t *json_object_deep_copy(json_t *object)
330 result = json_object();
334 iter = json_object_iter(object);
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));
344 iter = json_object_iter_next(object, iter);
353 json_t *json_array(void)
355 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
358 json_init(&array->json, JSON_ARRAY);
363 array->table = jsonp_malloc(array->size * sizeof(json_t *));
374 static void json_delete_array(json_array_t *array)
378 for(i = 0; i < array->entries; i++)
379 json_decref(array->table[i]);
381 jsonp_free(array->table);
385 size_t json_array_size(const json_t *json)
387 if(!json_is_array(json))
390 return json_to_array(json)->entries;
393 json_t *json_array_get(const json_t *json, size_t index)
396 if(!json_is_array(json))
398 array = json_to_array(json);
400 if(index >= array->entries)
403 return array->table[index];
406 int json_array_set_new(json_t *json, size_t index, json_t *value)
413 if(!json_is_array(json) || json == value)
418 array = json_to_array(json);
420 if(index >= array->entries)
426 json_decref(array->table[index]);
427 array->table[index] = value;
432 static void array_move(json_array_t *array, size_t dest,
433 size_t src, size_t count)
435 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
438 static void array_copy(json_t **dest, size_t dpos,
439 json_t **src, size_t spos,
442 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
445 static json_t **json_array_grow(json_array_t *array,
450 json_t **old_table, **new_table;
452 if(array->entries + amount <= array->size)
455 old_table = array->table;
457 new_size = max(array->size + amount, array->size * 2);
458 new_table = jsonp_malloc(new_size * sizeof(json_t *));
462 array->size = new_size;
463 array->table = new_table;
466 array_copy(array->table, 0, old_table, 0, array->entries);
467 jsonp_free(old_table);
474 int json_array_append_new(json_t *json, json_t *value)
481 if(!json_is_array(json) || json == value)
486 array = json_to_array(json);
488 if(!json_array_grow(array, 1, 1)) {
493 array->table[array->entries] = value;
499 int json_array_insert_new(json_t *json, size_t index, json_t *value)
507 if(!json_is_array(json) || json == value) {
511 array = json_to_array(json);
513 if(index > array->entries) {
518 old_table = json_array_grow(array, 1, 0);
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);
531 array_move(array, index + 1, index, array->entries - index);
533 array->table[index] = value;
539 int json_array_remove(json_t *json, size_t index)
543 if(!json_is_array(json))
545 array = json_to_array(json);
547 if(index >= array->entries)
550 json_decref(array->table[index]);
552 array_move(array, index, index + 1, array->entries - index);
558 int json_array_clear(json_t *json)
563 if(!json_is_array(json))
565 array = json_to_array(json);
567 for(i = 0; i < array->entries; i++)
568 json_decref(array->table[i]);
574 int json_array_extend(json_t *json, json_t *other_json)
576 json_array_t *array, *other;
579 if(!json_is_array(json) || !json_is_array(other_json))
581 array = json_to_array(json);
582 other = json_to_array(other_json);
584 if(!json_array_grow(array, other->entries, 1))
587 for(i = 0; i < other->entries; i++)
588 json_incref(other->table[i]);
590 array_copy(array->table, array->entries, other->table, 0, other->entries);
592 array->entries += other->entries;
596 static int json_array_equal(json_t *array1, json_t *array2)
600 size = json_array_size(array1);
601 if(size != json_array_size(array2))
604 for(i = 0; i < size; i++)
606 json_t *value1, *value2;
608 value1 = json_array_get(array1, i);
609 value2 = json_array_get(array2, i);
611 if(!json_equal(value1, value2))
618 static json_t *json_array_copy(json_t *array)
623 result = json_array();
627 for(i = 0; i < json_array_size(array); i++)
628 json_array_append(result, json_array_get(array, i));
633 static json_t *json_array_deep_copy(json_t *array)
638 result = json_array();
642 for(i = 0; i < json_array_size(array); i++)
643 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
650 json_t *json_string_nocheck(const char *value)
652 json_string_t *string;
657 string = jsonp_malloc(sizeof(json_string_t));
660 json_init(&string->json, JSON_STRING);
662 string->value = jsonp_strdup(value);
668 return &string->json;
671 json_t *json_string(const char *value)
673 if(!value || !utf8_check_string(value, -1))
676 return json_string_nocheck(value);
679 const char *json_string_value(const json_t *json)
681 if(!json_is_string(json))
684 return json_to_string(json)->value;
687 int json_string_set_nocheck(json_t *json, const char *value)
690 json_string_t *string;
692 dup = jsonp_strdup(value);
696 string = json_to_string(json);
697 jsonp_free(string->value);
703 int json_string_set(json_t *json, const char *value)
705 if(!value || !utf8_check_string(value, -1))
708 return json_string_set_nocheck(json, value);
711 static void json_delete_string(json_string_t *string)
713 jsonp_free(string->value);
717 static int json_string_equal(json_t *string1, json_t *string2)
719 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
722 static json_t *json_string_copy(json_t *string)
724 return json_string_nocheck(json_string_value(string));
730 json_t *json_integer(json_int_t value)
732 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
735 json_init(&integer->json, JSON_INTEGER);
737 integer->value = value;
738 return &integer->json;
741 json_int_t json_integer_value(const json_t *json)
743 if(!json_is_integer(json))
746 return json_to_integer(json)->value;
749 int json_integer_set(json_t *json, json_int_t value)
751 if(!json_is_integer(json))
754 json_to_integer(json)->value = value;
759 static void json_delete_integer(json_integer_t *integer)
764 static int json_integer_equal(json_t *integer1, json_t *integer2)
766 return json_integer_value(integer1) == json_integer_value(integer2);
769 static json_t *json_integer_copy(json_t *integer)
771 return json_integer(json_integer_value(integer));
777 json_t *json_real(double value)
779 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
782 json_init(&real->json, JSON_REAL);
788 double json_real_value(const json_t *json)
790 if(!json_is_real(json))
793 return json_to_real(json)->value;
796 int json_real_set(json_t *json, double value)
798 if(!json_is_real(json))
801 json_to_real(json)->value = value;
806 static void json_delete_real(json_real_t *real)
811 static int json_real_equal(json_t *real1, json_t *real2)
813 return json_real_value(real1) == json_real_value(real2);
816 static json_t *json_real_copy(json_t *real)
818 return json_real(json_real_value(real));
824 double json_number_value(const json_t *json)
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);
835 /*** simple values ***/
837 json_t *json_true(void)
839 static json_t the_true = {JSON_TRUE, (size_t)-1};
844 json_t *json_false(void)
846 static json_t the_false = {JSON_FALSE, (size_t)-1};
851 json_t *json_null(void)
853 static json_t the_null = {JSON_NULL, (size_t)-1};
860 void json_delete(json_t *json)
862 if(json_is_object(json))
863 json_delete_object(json_to_object(json));
865 else if(json_is_array(json))
866 json_delete_array(json_to_array(json));
868 else if(json_is_string(json))
869 json_delete_string(json_to_string(json));
871 else if(json_is_integer(json))
872 json_delete_integer(json_to_integer(json));
874 else if(json_is_real(json))
875 json_delete_real(json_to_real(json));
877 /* json_delete is not called for true, false or null */
883 int json_equal(json_t *json1, json_t *json2)
888 if(json_typeof(json1) != json_typeof(json2))
891 /* this covers true, false and null as they are singletons */
895 if(json_is_object(json1))
896 return json_object_equal(json1, json2);
898 if(json_is_array(json1))
899 return json_array_equal(json1, json2);
901 if(json_is_string(json1))
902 return json_string_equal(json1, json2);
904 if(json_is_integer(json1))
905 return json_integer_equal(json1, json2);
907 if(json_is_real(json1))
908 return json_real_equal(json1, json2);
916 json_t *json_copy(json_t *json)
921 if(json_is_object(json))
922 return json_object_copy(json);
924 if(json_is_array(json))
925 return json_array_copy(json);
927 if(json_is_string(json))
928 return json_string_copy(json);
930 if(json_is_integer(json))
931 return json_integer_copy(json);
933 if(json_is_real(json))
934 return json_real_copy(json);
936 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
942 json_t *json_deep_copy(json_t *json)
947 if(json_is_object(json))
948 return json_object_deep_copy(json);
950 if(json_is_array(json))
951 return json_array_deep_copy(json);
953 /* for the rest of the types, deep copying doesn't differ from
956 if(json_is_string(json))
957 return json_string_copy(json);
959 if(json_is_integer(json))
960 return json_integer_copy(json);
962 if(json_is_real(json))
963 return json_real_copy(json);
965 if(json_is_true(json) || json_is_false(json) || json_is_null(json))