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 /* From http://www.cse.yorku.ca/~oz/hash.html */
30 size_t jsonp_hash_str(const void *ptr)
32 const char *str = (const char *)ptr;
37 while((c = (size_t)*str))
39 hash = ((hash << 5) + hash) + c;
46 int jsonp_str_equal(const void *ptr1, const void *ptr2)
48 return strcmp((const char *)ptr1, (const char *)ptr2) == 0;
51 /* This macro just returns a pointer that's a few bytes backwards from
52 string. This makes it possible to pass a pointer to object_key_t
53 when only the string inside it is used, without actually creating
54 an object_key_t instance. */
55 #define string_to_key(string) container_of(string, object_key_t, key)
57 static size_t hash_key(const void *ptr)
59 return jsonp_hash_str(((const object_key_t *)ptr)->key);
62 static int key_equal(const void *ptr1, const void *ptr2)
64 return jsonp_str_equal(((const object_key_t *)ptr1)->key,
65 ((const object_key_t *)ptr2)->key);
68 static void value_decref(void *value)
70 json_decref((json_t *)value);
73 json_t *json_object(void)
75 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
78 json_init(&object->json, JSON_OBJECT);
80 if(hashtable_init(&object->hashtable,
82 jsonp_free, value_decref))
94 static void json_delete_object(json_object_t *object)
96 hashtable_close(&object->hashtable);
100 size_t json_object_size(const json_t *json)
102 json_object_t *object;
104 if(!json_is_object(json))
107 object = json_to_object(json);
108 return object->hashtable.size;
111 json_t *json_object_get(const json_t *json, const char *key)
113 json_object_t *object;
115 if(!json_is_object(json))
118 object = json_to_object(json);
119 return hashtable_get(&object->hashtable, string_to_key(key));
122 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
124 json_object_t *object;
130 if(!json_is_object(json) || json == value)
135 object = json_to_object(json);
137 /* offsetof(...) returns the size of object_key_t without the
138 last, flexible member. This way, the correct amount is
140 k = jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1);
144 k->serial = object->serial++;
147 if(hashtable_set(&object->hashtable, k, value))
156 int json_object_set_new(json_t *json, const char *key, json_t *value)
158 if(!key || !utf8_check_string(key, -1))
164 return json_object_set_new_nocheck(json, key, value);
167 int json_object_del(json_t *json, const char *key)
169 json_object_t *object;
171 if(!json_is_object(json))
174 object = json_to_object(json);
175 return hashtable_del(&object->hashtable, string_to_key(key));
178 int json_object_clear(json_t *json)
180 json_object_t *object;
182 if(!json_is_object(json))
185 object = json_to_object(json);
186 hashtable_clear(&object->hashtable);
191 int json_object_update(json_t *object, json_t *other)
195 if(!json_is_object(object) || !json_is_object(other))
198 iter = json_object_iter(other);
203 key = json_object_iter_key(iter);
204 value = json_object_iter_value(iter);
206 if(json_object_set_nocheck(object, key, value))
209 iter = json_object_iter_next(other, iter);
215 void *json_object_iter(json_t *json)
217 json_object_t *object;
219 if(!json_is_object(json))
222 object = json_to_object(json);
223 return hashtable_iter(&object->hashtable);
226 void *json_object_iter_at(json_t *json, const char *key)
228 json_object_t *object;
230 if(!key || !json_is_object(json))
233 object = json_to_object(json);
234 return hashtable_iter_at(&object->hashtable, string_to_key(key));
237 void *json_object_iter_next(json_t *json, void *iter)
239 json_object_t *object;
241 if(!json_is_object(json) || iter == NULL)
244 object = json_to_object(json);
245 return hashtable_iter_next(&object->hashtable, iter);
248 const object_key_t *jsonp_object_iter_fullkey(void *iter)
253 return hashtable_iter_key(iter);
256 const char *json_object_iter_key(void *iter)
261 return jsonp_object_iter_fullkey(iter)->key;
264 json_t *json_object_iter_value(void *iter)
269 return (json_t *)hashtable_iter_value(iter);
272 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
274 json_object_t *object;
276 if(!json_is_object(json) || !iter || !value)
279 object = json_to_object(json);
280 hashtable_iter_set(&object->hashtable, iter, value);
285 static int json_object_equal(json_t *object1, json_t *object2)
289 if(json_object_size(object1) != json_object_size(object2))
292 iter = json_object_iter(object1);
296 json_t *value1, *value2;
298 key = json_object_iter_key(iter);
299 value1 = json_object_iter_value(iter);
300 value2 = json_object_get(object2, key);
302 if(!json_equal(value1, value2))
305 iter = json_object_iter_next(object1, iter);
311 static json_t *json_object_copy(json_t *object)
316 result = json_object();
320 iter = json_object_iter(object);
326 key = json_object_iter_key(iter);
327 value = json_object_iter_value(iter);
328 json_object_set_nocheck(result, key, value);
330 iter = json_object_iter_next(object, iter);
336 static json_t *json_object_deep_copy(json_t *object)
341 result = json_object();
345 iter = json_object_iter(object);
351 key = json_object_iter_key(iter);
352 value = json_object_iter_value(iter);
353 json_object_set_new_nocheck(result, key, json_deep_copy(value));
355 iter = json_object_iter_next(object, iter);
364 json_t *json_array(void)
366 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
369 json_init(&array->json, JSON_ARRAY);
374 array->table = jsonp_malloc(array->size * sizeof(json_t *));
385 static void json_delete_array(json_array_t *array)
389 for(i = 0; i < array->entries; i++)
390 json_decref(array->table[i]);
392 jsonp_free(array->table);
396 size_t json_array_size(const json_t *json)
398 if(!json_is_array(json))
401 return json_to_array(json)->entries;
404 json_t *json_array_get(const json_t *json, size_t index)
407 if(!json_is_array(json))
409 array = json_to_array(json);
411 if(index >= array->entries)
414 return array->table[index];
417 int json_array_set_new(json_t *json, size_t index, json_t *value)
424 if(!json_is_array(json) || json == value)
429 array = json_to_array(json);
431 if(index >= array->entries)
437 json_decref(array->table[index]);
438 array->table[index] = value;
443 static void array_move(json_array_t *array, size_t dest,
444 size_t src, size_t count)
446 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
449 static void array_copy(json_t **dest, size_t dpos,
450 json_t **src, size_t spos,
453 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
456 static json_t **json_array_grow(json_array_t *array,
461 json_t **old_table, **new_table;
463 if(array->entries + amount <= array->size)
466 old_table = array->table;
468 new_size = max(array->size + amount, array->size * 2);
469 new_table = jsonp_malloc(new_size * sizeof(json_t *));
473 array->size = new_size;
474 array->table = new_table;
477 array_copy(array->table, 0, old_table, 0, array->entries);
478 jsonp_free(old_table);
485 int json_array_append_new(json_t *json, json_t *value)
492 if(!json_is_array(json) || json == value)
497 array = json_to_array(json);
499 if(!json_array_grow(array, 1, 1)) {
504 array->table[array->entries] = value;
510 int json_array_insert_new(json_t *json, size_t index, json_t *value)
518 if(!json_is_array(json) || json == value) {
522 array = json_to_array(json);
524 if(index > array->entries) {
529 old_table = json_array_grow(array, 1, 0);
535 if(old_table != array->table) {
536 array_copy(array->table, 0, old_table, 0, index);
537 array_copy(array->table, index + 1, old_table, index,
538 array->entries - index);
539 jsonp_free(old_table);
542 array_move(array, index + 1, index, array->entries - index);
544 array->table[index] = value;
550 int json_array_remove(json_t *json, size_t index)
554 if(!json_is_array(json))
556 array = json_to_array(json);
558 if(index >= array->entries)
561 json_decref(array->table[index]);
563 array_move(array, index, index + 1, array->entries - index);
569 int json_array_clear(json_t *json)
574 if(!json_is_array(json))
576 array = json_to_array(json);
578 for(i = 0; i < array->entries; i++)
579 json_decref(array->table[i]);
585 int json_array_extend(json_t *json, json_t *other_json)
587 json_array_t *array, *other;
590 if(!json_is_array(json) || !json_is_array(other_json))
592 array = json_to_array(json);
593 other = json_to_array(other_json);
595 if(!json_array_grow(array, other->entries, 1))
598 for(i = 0; i < other->entries; i++)
599 json_incref(other->table[i]);
601 array_copy(array->table, array->entries, other->table, 0, other->entries);
603 array->entries += other->entries;
607 static int json_array_equal(json_t *array1, json_t *array2)
611 size = json_array_size(array1);
612 if(size != json_array_size(array2))
615 for(i = 0; i < size; i++)
617 json_t *value1, *value2;
619 value1 = json_array_get(array1, i);
620 value2 = json_array_get(array2, i);
622 if(!json_equal(value1, value2))
629 static json_t *json_array_copy(json_t *array)
634 result = json_array();
638 for(i = 0; i < json_array_size(array); i++)
639 json_array_append(result, json_array_get(array, i));
644 static json_t *json_array_deep_copy(json_t *array)
649 result = json_array();
653 for(i = 0; i < json_array_size(array); i++)
654 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
661 json_t *json_string_nocheck(const char *value)
663 json_string_t *string;
668 string = jsonp_malloc(sizeof(json_string_t));
671 json_init(&string->json, JSON_STRING);
673 string->value = jsonp_strdup(value);
679 return &string->json;
682 json_t *json_string(const char *value)
684 if(!value || !utf8_check_string(value, -1))
687 return json_string_nocheck(value);
690 const char *json_string_value(const json_t *json)
692 if(!json_is_string(json))
695 return json_to_string(json)->value;
698 int json_string_set_nocheck(json_t *json, const char *value)
701 json_string_t *string;
703 dup = jsonp_strdup(value);
707 string = json_to_string(json);
708 jsonp_free(string->value);
714 int json_string_set(json_t *json, const char *value)
716 if(!value || !utf8_check_string(value, -1))
719 return json_string_set_nocheck(json, value);
722 static void json_delete_string(json_string_t *string)
724 jsonp_free(string->value);
728 static int json_string_equal(json_t *string1, json_t *string2)
730 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
733 static json_t *json_string_copy(json_t *string)
735 return json_string_nocheck(json_string_value(string));
741 json_t *json_integer(json_int_t value)
743 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
746 json_init(&integer->json, JSON_INTEGER);
748 integer->value = value;
749 return &integer->json;
752 json_int_t json_integer_value(const json_t *json)
754 if(!json_is_integer(json))
757 return json_to_integer(json)->value;
760 int json_integer_set(json_t *json, json_int_t value)
762 if(!json_is_integer(json))
765 json_to_integer(json)->value = value;
770 static void json_delete_integer(json_integer_t *integer)
775 static int json_integer_equal(json_t *integer1, json_t *integer2)
777 return json_integer_value(integer1) == json_integer_value(integer2);
780 static json_t *json_integer_copy(json_t *integer)
782 return json_integer(json_integer_value(integer));
788 json_t *json_real(double value)
790 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
793 json_init(&real->json, JSON_REAL);
799 double json_real_value(const json_t *json)
801 if(!json_is_real(json))
804 return json_to_real(json)->value;
807 int json_real_set(json_t *json, double value)
809 if(!json_is_real(json))
812 json_to_real(json)->value = value;
817 static void json_delete_real(json_real_t *real)
822 static int json_real_equal(json_t *real1, json_t *real2)
824 return json_real_value(real1) == json_real_value(real2);
827 static json_t *json_real_copy(json_t *real)
829 return json_real(json_real_value(real));
835 double json_number_value(const json_t *json)
837 if(json_is_integer(json))
838 return json_integer_value(json);
839 else if(json_is_real(json))
840 return json_real_value(json);
846 /*** simple values ***/
848 json_t *json_true(void)
850 static json_t the_true = {JSON_TRUE, (size_t)-1};
855 json_t *json_false(void)
857 static json_t the_false = {JSON_FALSE, (size_t)-1};
862 json_t *json_null(void)
864 static json_t the_null = {JSON_NULL, (size_t)-1};
871 void json_delete(json_t *json)
873 if(json_is_object(json))
874 json_delete_object(json_to_object(json));
876 else if(json_is_array(json))
877 json_delete_array(json_to_array(json));
879 else if(json_is_string(json))
880 json_delete_string(json_to_string(json));
882 else if(json_is_integer(json))
883 json_delete_integer(json_to_integer(json));
885 else if(json_is_real(json))
886 json_delete_real(json_to_real(json));
888 /* json_delete is not called for true, false or null */
894 int json_equal(json_t *json1, json_t *json2)
899 if(json_typeof(json1) != json_typeof(json2))
902 /* this covers true, false and null as they are singletons */
906 if(json_is_object(json1))
907 return json_object_equal(json1, json2);
909 if(json_is_array(json1))
910 return json_array_equal(json1, json2);
912 if(json_is_string(json1))
913 return json_string_equal(json1, json2);
915 if(json_is_integer(json1))
916 return json_integer_equal(json1, json2);
918 if(json_is_real(json1))
919 return json_real_equal(json1, json2);
927 json_t *json_copy(json_t *json)
932 if(json_is_object(json))
933 return json_object_copy(json);
935 if(json_is_array(json))
936 return json_array_copy(json);
938 if(json_is_string(json))
939 return json_string_copy(json);
941 if(json_is_integer(json))
942 return json_integer_copy(json);
944 if(json_is_real(json))
945 return json_real_copy(json);
947 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
953 json_t *json_deep_copy(json_t *json)
958 if(json_is_object(json))
959 return json_object_deep_copy(json);
961 if(json_is_array(json))
962 return json_array_deep_copy(json);
964 /* for the rest of the types, deep copying doesn't differ from
967 if(json_is_string(json))
968 return json_string_copy(json);
970 if(json_is_integer(json))
971 return json_integer_copy(json);
973 if(json_is_real(json))
974 return json_real_copy(json);
976 if(json_is_true(json) || json_is_false(json) || json_is_null(json))