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);
147 k->serial = object->serial++;
150 if(hashtable_set(&object->hashtable, k, value))
159 int json_object_set_new(json_t *json, const char *key, json_t *value)
161 if(!key || !utf8_check_string(key, -1))
167 return json_object_set_new_nocheck(json, key, value);
170 int json_object_del(json_t *json, const char *key)
172 json_object_t *object;
174 if(!json_is_object(json))
177 object = json_to_object(json);
178 return hashtable_del(&object->hashtable, string_to_key(key));
181 int json_object_clear(json_t *json)
183 json_object_t *object;
185 if(!json_is_object(json))
188 object = json_to_object(json);
189 hashtable_clear(&object->hashtable);
194 int json_object_update(json_t *object, json_t *other)
198 if(!json_is_object(object) || !json_is_object(other))
201 iter = json_object_iter(other);
206 key = json_object_iter_key(iter);
207 value = json_object_iter_value(iter);
209 if(json_object_set_nocheck(object, key, value))
212 iter = json_object_iter_next(other, iter);
218 void *json_object_iter(json_t *json)
220 json_object_t *object;
222 if(!json_is_object(json))
225 object = json_to_object(json);
226 return hashtable_iter(&object->hashtable);
229 void *json_object_iter_at(json_t *json, const char *key)
231 json_object_t *object;
233 if(!key || !json_is_object(json))
236 object = json_to_object(json);
237 return hashtable_iter_at(&object->hashtable, string_to_key(key));
240 void *json_object_iter_next(json_t *json, void *iter)
242 json_object_t *object;
244 if(!json_is_object(json) || iter == NULL)
247 object = json_to_object(json);
248 return hashtable_iter_next(&object->hashtable, iter);
251 const object_key_t *jsonp_object_iter_fullkey(void *iter)
256 return hashtable_iter_key(iter);
259 const char *json_object_iter_key(void *iter)
264 return jsonp_object_iter_fullkey(iter)->key;
267 json_t *json_object_iter_value(void *iter)
272 return (json_t *)hashtable_iter_value(iter);
275 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
277 json_object_t *object;
279 if(!json_is_object(json) || !iter || !value)
282 object = json_to_object(json);
283 hashtable_iter_set(&object->hashtable, iter, value);
288 static int json_object_equal(json_t *object1, json_t *object2)
292 if(json_object_size(object1) != json_object_size(object2))
295 iter = json_object_iter(object1);
299 json_t *value1, *value2;
301 key = json_object_iter_key(iter);
302 value1 = json_object_iter_value(iter);
303 value2 = json_object_get(object2, key);
305 if(!json_equal(value1, value2))
308 iter = json_object_iter_next(object1, iter);
314 static json_t *json_object_copy(json_t *object)
319 result = json_object();
323 iter = json_object_iter(object);
329 key = json_object_iter_key(iter);
330 value = json_object_iter_value(iter);
331 json_object_set_nocheck(result, key, value);
333 iter = json_object_iter_next(object, iter);
339 static json_t *json_object_deep_copy(json_t *object)
344 result = json_object();
348 iter = json_object_iter(object);
354 key = json_object_iter_key(iter);
355 value = json_object_iter_value(iter);
356 json_object_set_new_nocheck(result, key, json_deep_copy(value));
358 iter = json_object_iter_next(object, iter);
367 json_t *json_array(void)
369 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
372 json_init(&array->json, JSON_ARRAY);
377 array->table = jsonp_malloc(array->size * sizeof(json_t *));
388 static void json_delete_array(json_array_t *array)
392 for(i = 0; i < array->entries; i++)
393 json_decref(array->table[i]);
395 jsonp_free(array->table);
399 size_t json_array_size(const json_t *json)
401 if(!json_is_array(json))
404 return json_to_array(json)->entries;
407 json_t *json_array_get(const json_t *json, size_t index)
410 if(!json_is_array(json))
412 array = json_to_array(json);
414 if(index >= array->entries)
417 return array->table[index];
420 int json_array_set_new(json_t *json, size_t index, json_t *value)
427 if(!json_is_array(json) || json == value)
432 array = json_to_array(json);
434 if(index >= array->entries)
440 json_decref(array->table[index]);
441 array->table[index] = value;
446 static void array_move(json_array_t *array, size_t dest,
447 size_t src, size_t count)
449 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
452 static void array_copy(json_t **dest, size_t dpos,
453 json_t **src, size_t spos,
456 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
459 static json_t **json_array_grow(json_array_t *array,
464 json_t **old_table, **new_table;
466 if(array->entries + amount <= array->size)
469 old_table = array->table;
471 new_size = max(array->size + amount, array->size * 2);
472 new_table = jsonp_malloc(new_size * sizeof(json_t *));
476 array->size = new_size;
477 array->table = new_table;
480 array_copy(array->table, 0, old_table, 0, array->entries);
481 jsonp_free(old_table);
488 int json_array_append_new(json_t *json, json_t *value)
495 if(!json_is_array(json) || json == value)
500 array = json_to_array(json);
502 if(!json_array_grow(array, 1, 1)) {
507 array->table[array->entries] = value;
513 int json_array_insert_new(json_t *json, size_t index, json_t *value)
521 if(!json_is_array(json) || json == value) {
525 array = json_to_array(json);
527 if(index > array->entries) {
532 old_table = json_array_grow(array, 1, 0);
538 if(old_table != array->table) {
539 array_copy(array->table, 0, old_table, 0, index);
540 array_copy(array->table, index + 1, old_table, index,
541 array->entries - index);
542 jsonp_free(old_table);
545 array_move(array, index + 1, index, array->entries - index);
547 array->table[index] = value;
553 int json_array_remove(json_t *json, size_t index)
557 if(!json_is_array(json))
559 array = json_to_array(json);
561 if(index >= array->entries)
564 json_decref(array->table[index]);
566 array_move(array, index, index + 1, array->entries - index);
572 int json_array_clear(json_t *json)
577 if(!json_is_array(json))
579 array = json_to_array(json);
581 for(i = 0; i < array->entries; i++)
582 json_decref(array->table[i]);
588 int json_array_extend(json_t *json, json_t *other_json)
590 json_array_t *array, *other;
593 if(!json_is_array(json) || !json_is_array(other_json))
595 array = json_to_array(json);
596 other = json_to_array(other_json);
598 if(!json_array_grow(array, other->entries, 1))
601 for(i = 0; i < other->entries; i++)
602 json_incref(other->table[i]);
604 array_copy(array->table, array->entries, other->table, 0, other->entries);
606 array->entries += other->entries;
610 static int json_array_equal(json_t *array1, json_t *array2)
614 size = json_array_size(array1);
615 if(size != json_array_size(array2))
618 for(i = 0; i < size; i++)
620 json_t *value1, *value2;
622 value1 = json_array_get(array1, i);
623 value2 = json_array_get(array2, i);
625 if(!json_equal(value1, value2))
632 static json_t *json_array_copy(json_t *array)
637 result = json_array();
641 for(i = 0; i < json_array_size(array); i++)
642 json_array_append(result, json_array_get(array, i));
647 static json_t *json_array_deep_copy(json_t *array)
652 result = json_array();
656 for(i = 0; i < json_array_size(array); i++)
657 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
664 json_t *json_string_nocheck(const char *value)
666 json_string_t *string;
671 string = jsonp_malloc(sizeof(json_string_t));
674 json_init(&string->json, JSON_STRING);
676 string->value = jsonp_strdup(value);
682 return &string->json;
685 json_t *json_string(const char *value)
687 if(!value || !utf8_check_string(value, -1))
690 return json_string_nocheck(value);
693 const char *json_string_value(const json_t *json)
695 if(!json_is_string(json))
698 return json_to_string(json)->value;
701 int json_string_set_nocheck(json_t *json, const char *value)
704 json_string_t *string;
706 if(!json_is_string(json) || !value)
709 dup = jsonp_strdup(value);
713 string = json_to_string(json);
714 jsonp_free(string->value);
720 int json_string_set(json_t *json, const char *value)
722 if(!value || !utf8_check_string(value, -1))
725 return json_string_set_nocheck(json, value);
728 static void json_delete_string(json_string_t *string)
730 jsonp_free(string->value);
734 static int json_string_equal(json_t *string1, json_t *string2)
736 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
739 static json_t *json_string_copy(json_t *string)
741 return json_string_nocheck(json_string_value(string));
747 json_t *json_integer(json_int_t value)
749 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
752 json_init(&integer->json, JSON_INTEGER);
754 integer->value = value;
755 return &integer->json;
758 json_int_t json_integer_value(const json_t *json)
760 if(!json_is_integer(json))
763 return json_to_integer(json)->value;
766 int json_integer_set(json_t *json, json_int_t value)
768 if(!json_is_integer(json))
771 json_to_integer(json)->value = value;
776 static void json_delete_integer(json_integer_t *integer)
781 static int json_integer_equal(json_t *integer1, json_t *integer2)
783 return json_integer_value(integer1) == json_integer_value(integer2);
786 static json_t *json_integer_copy(json_t *integer)
788 return json_integer(json_integer_value(integer));
794 json_t *json_real(double value)
796 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
799 json_init(&real->json, JSON_REAL);
805 double json_real_value(const json_t *json)
807 if(!json_is_real(json))
810 return json_to_real(json)->value;
813 int json_real_set(json_t *json, double value)
815 if(!json_is_real(json))
818 json_to_real(json)->value = value;
823 static void json_delete_real(json_real_t *real)
828 static int json_real_equal(json_t *real1, json_t *real2)
830 return json_real_value(real1) == json_real_value(real2);
833 static json_t *json_real_copy(json_t *real)
835 return json_real(json_real_value(real));
841 double json_number_value(const json_t *json)
843 if(json_is_integer(json))
844 return json_integer_value(json);
845 else if(json_is_real(json))
846 return json_real_value(json);
852 /*** simple values ***/
854 json_t *json_true(void)
856 static json_t the_true = {JSON_TRUE, (size_t)-1};
861 json_t *json_false(void)
863 static json_t the_false = {JSON_FALSE, (size_t)-1};
868 json_t *json_null(void)
870 static json_t the_null = {JSON_NULL, (size_t)-1};
877 void json_delete(json_t *json)
879 if(json_is_object(json))
880 json_delete_object(json_to_object(json));
882 else if(json_is_array(json))
883 json_delete_array(json_to_array(json));
885 else if(json_is_string(json))
886 json_delete_string(json_to_string(json));
888 else if(json_is_integer(json))
889 json_delete_integer(json_to_integer(json));
891 else if(json_is_real(json))
892 json_delete_real(json_to_real(json));
894 /* json_delete is not called for true, false or null */
900 int json_equal(json_t *json1, json_t *json2)
905 if(json_typeof(json1) != json_typeof(json2))
908 /* this covers true, false and null as they are singletons */
912 if(json_is_object(json1))
913 return json_object_equal(json1, json2);
915 if(json_is_array(json1))
916 return json_array_equal(json1, json2);
918 if(json_is_string(json1))
919 return json_string_equal(json1, json2);
921 if(json_is_integer(json1))
922 return json_integer_equal(json1, json2);
924 if(json_is_real(json1))
925 return json_real_equal(json1, json2);
933 json_t *json_copy(json_t *json)
938 if(json_is_object(json))
939 return json_object_copy(json);
941 if(json_is_array(json))
942 return json_array_copy(json);
944 if(json_is_string(json))
945 return json_string_copy(json);
947 if(json_is_integer(json))
948 return json_integer_copy(json);
950 if(json_is_real(json))
951 return json_real_copy(json);
953 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
959 json_t *json_deep_copy(json_t *json)
964 if(json_is_object(json))
965 return json_object_deep_copy(json);
967 if(json_is_array(json))
968 return json_array_deep_copy(json);
970 /* for the rest of the types, deep copying doesn't differ from
973 if(json_is_string(json))
974 return json_string_copy(json);
976 if(json_is_integer(json))
977 return json_integer_copy(json);
979 if(json_is_real(json))
980 return json_real_copy(json);
982 if(json_is_true(json) || json_is_false(json) || json_is_null(json))