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(!key || !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);
190 hashtable_clear(&object->hashtable);
196 int json_object_update(json_t *object, json_t *other)
200 if(!json_is_object(object) || !json_is_object(other))
203 iter = json_object_iter(other);
208 key = json_object_iter_key(iter);
209 value = json_object_iter_value(iter);
211 if(json_object_set_nocheck(object, key, value))
214 iter = json_object_iter_next(other, iter);
220 void *json_object_iter(json_t *json)
222 json_object_t *object;
224 if(!json_is_object(json))
227 object = json_to_object(json);
228 return hashtable_iter(&object->hashtable);
231 void *json_object_iter_at(json_t *json, const char *key)
233 json_object_t *object;
235 if(!key || !json_is_object(json))
238 object = json_to_object(json);
239 return hashtable_iter_at(&object->hashtable, string_to_key(key));
242 void *json_object_iter_next(json_t *json, void *iter)
244 json_object_t *object;
246 if(!json_is_object(json) || iter == NULL)
249 object = json_to_object(json);
250 return hashtable_iter_next(&object->hashtable, iter);
253 const object_key_t *jsonp_object_iter_fullkey(void *iter)
258 return hashtable_iter_key(iter);
261 const char *json_object_iter_key(void *iter)
266 return jsonp_object_iter_fullkey(iter)->key;
269 json_t *json_object_iter_value(void *iter)
274 return (json_t *)hashtable_iter_value(iter);
277 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
279 json_object_t *object;
281 if(!json_is_object(json) || !iter || !value)
284 object = json_to_object(json);
285 hashtable_iter_set(&object->hashtable, iter, value);
290 static int json_object_equal(json_t *object1, json_t *object2)
294 if(json_object_size(object1) != json_object_size(object2))
297 iter = json_object_iter(object1);
301 json_t *value1, *value2;
303 key = json_object_iter_key(iter);
304 value1 = json_object_iter_value(iter);
305 value2 = json_object_get(object2, key);
307 if(!json_equal(value1, value2))
310 iter = json_object_iter_next(object1, iter);
316 static json_t *json_object_copy(json_t *object)
321 result = json_object();
325 iter = json_object_iter(object);
331 key = json_object_iter_key(iter);
332 value = json_object_iter_value(iter);
333 json_object_set_nocheck(result, key, value);
335 iter = json_object_iter_next(object, iter);
341 static json_t *json_object_deep_copy(json_t *object)
346 result = json_object();
350 iter = json_object_iter(object);
356 key = json_object_iter_key(iter);
357 value = json_object_iter_value(iter);
358 json_object_set_new_nocheck(result, key, json_deep_copy(value));
360 iter = json_object_iter_next(object, iter);
369 json_t *json_array(void)
371 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
374 json_init(&array->json, JSON_ARRAY);
379 array->table = jsonp_malloc(array->size * sizeof(json_t *));
390 static void json_delete_array(json_array_t *array)
394 for(i = 0; i < array->entries; i++)
395 json_decref(array->table[i]);
397 jsonp_free(array->table);
401 size_t json_array_size(const json_t *json)
403 if(!json_is_array(json))
406 return json_to_array(json)->entries;
409 json_t *json_array_get(const json_t *json, size_t index)
412 if(!json_is_array(json))
414 array = json_to_array(json);
416 if(index >= array->entries)
419 return array->table[index];
422 int json_array_set_new(json_t *json, size_t index, json_t *value)
429 if(!json_is_array(json) || json == value)
434 array = json_to_array(json);
436 if(index >= array->entries)
442 json_decref(array->table[index]);
443 array->table[index] = value;
448 static void array_move(json_array_t *array, size_t dest,
449 size_t src, size_t count)
451 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
454 static void array_copy(json_t **dest, size_t dpos,
455 json_t **src, size_t spos,
458 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
461 static json_t **json_array_grow(json_array_t *array,
466 json_t **old_table, **new_table;
468 if(array->entries + amount <= array->size)
471 old_table = array->table;
473 new_size = max(array->size + amount, array->size * 2);
474 new_table = jsonp_malloc(new_size * sizeof(json_t *));
478 array->size = new_size;
479 array->table = new_table;
482 array_copy(array->table, 0, old_table, 0, array->entries);
483 jsonp_free(old_table);
490 int json_array_append_new(json_t *json, json_t *value)
497 if(!json_is_array(json) || json == value)
502 array = json_to_array(json);
504 if(!json_array_grow(array, 1, 1)) {
509 array->table[array->entries] = value;
515 int json_array_insert_new(json_t *json, size_t index, json_t *value)
523 if(!json_is_array(json) || json == value) {
527 array = json_to_array(json);
529 if(index > array->entries) {
534 old_table = json_array_grow(array, 1, 0);
540 if(old_table != array->table) {
541 array_copy(array->table, 0, old_table, 0, index);
542 array_copy(array->table, index + 1, old_table, index,
543 array->entries - index);
544 jsonp_free(old_table);
547 array_move(array, index + 1, index, array->entries - index);
549 array->table[index] = value;
555 int json_array_remove(json_t *json, size_t index)
559 if(!json_is_array(json))
561 array = json_to_array(json);
563 if(index >= array->entries)
566 json_decref(array->table[index]);
568 array_move(array, index, index + 1, array->entries - index);
574 int json_array_clear(json_t *json)
579 if(!json_is_array(json))
581 array = json_to_array(json);
583 for(i = 0; i < array->entries; i++)
584 json_decref(array->table[i]);
590 int json_array_extend(json_t *json, json_t *other_json)
592 json_array_t *array, *other;
595 if(!json_is_array(json) || !json_is_array(other_json))
597 array = json_to_array(json);
598 other = json_to_array(other_json);
600 if(!json_array_grow(array, other->entries, 1))
603 for(i = 0; i < other->entries; i++)
604 json_incref(other->table[i]);
606 array_copy(array->table, array->entries, other->table, 0, other->entries);
608 array->entries += other->entries;
612 static int json_array_equal(json_t *array1, json_t *array2)
616 size = json_array_size(array1);
617 if(size != json_array_size(array2))
620 for(i = 0; i < size; i++)
622 json_t *value1, *value2;
624 value1 = json_array_get(array1, i);
625 value2 = json_array_get(array2, i);
627 if(!json_equal(value1, value2))
634 static json_t *json_array_copy(json_t *array)
639 result = json_array();
643 for(i = 0; i < json_array_size(array); i++)
644 json_array_append(result, json_array_get(array, i));
649 static json_t *json_array_deep_copy(json_t *array)
654 result = json_array();
658 for(i = 0; i < json_array_size(array); i++)
659 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
666 json_t *json_string_nocheck(const char *value)
668 json_string_t *string;
673 string = jsonp_malloc(sizeof(json_string_t));
676 json_init(&string->json, JSON_STRING);
678 string->value = jsonp_strdup(value);
684 return &string->json;
687 json_t *json_string(const char *value)
689 if(!value || !utf8_check_string(value, -1))
692 return json_string_nocheck(value);
695 const char *json_string_value(const json_t *json)
697 if(!json_is_string(json))
700 return json_to_string(json)->value;
703 int json_string_set_nocheck(json_t *json, const char *value)
706 json_string_t *string;
708 if(!json_is_string(json) || !value)
711 dup = jsonp_strdup(value);
715 string = json_to_string(json);
716 jsonp_free(string->value);
722 int json_string_set(json_t *json, const char *value)
724 if(!value || !utf8_check_string(value, -1))
727 return json_string_set_nocheck(json, value);
730 static void json_delete_string(json_string_t *string)
732 jsonp_free(string->value);
736 static int json_string_equal(json_t *string1, json_t *string2)
738 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
741 static json_t *json_string_copy(json_t *string)
743 return json_string_nocheck(json_string_value(string));
749 json_t *json_integer(json_int_t value)
751 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
754 json_init(&integer->json, JSON_INTEGER);
756 integer->value = value;
757 return &integer->json;
760 json_int_t json_integer_value(const json_t *json)
762 if(!json_is_integer(json))
765 return json_to_integer(json)->value;
768 int json_integer_set(json_t *json, json_int_t value)
770 if(!json_is_integer(json))
773 json_to_integer(json)->value = value;
778 static void json_delete_integer(json_integer_t *integer)
783 static int json_integer_equal(json_t *integer1, json_t *integer2)
785 return json_integer_value(integer1) == json_integer_value(integer2);
788 static json_t *json_integer_copy(json_t *integer)
790 return json_integer(json_integer_value(integer));
796 json_t *json_real(double value)
798 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
801 json_init(&real->json, JSON_REAL);
807 double json_real_value(const json_t *json)
809 if(!json_is_real(json))
812 return json_to_real(json)->value;
815 int json_real_set(json_t *json, double value)
817 if(!json_is_real(json))
820 json_to_real(json)->value = value;
825 static void json_delete_real(json_real_t *real)
830 static int json_real_equal(json_t *real1, json_t *real2)
832 return json_real_value(real1) == json_real_value(real2);
835 static json_t *json_real_copy(json_t *real)
837 return json_real(json_real_value(real));
843 double json_number_value(const json_t *json)
845 if(json_is_integer(json))
846 return json_integer_value(json);
847 else if(json_is_real(json))
848 return json_real_value(json);
854 /*** simple values ***/
856 json_t *json_true(void)
858 static json_t the_true = {JSON_TRUE, (size_t)-1};
863 json_t *json_false(void)
865 static json_t the_false = {JSON_FALSE, (size_t)-1};
870 json_t *json_null(void)
872 static json_t the_null = {JSON_NULL, (size_t)-1};
879 void json_delete(json_t *json)
881 if(json_is_object(json))
882 json_delete_object(json_to_object(json));
884 else if(json_is_array(json))
885 json_delete_array(json_to_array(json));
887 else if(json_is_string(json))
888 json_delete_string(json_to_string(json));
890 else if(json_is_integer(json))
891 json_delete_integer(json_to_integer(json));
893 else if(json_is_real(json))
894 json_delete_real(json_to_real(json));
896 /* json_delete is not called for true, false or null */
902 int json_equal(json_t *json1, json_t *json2)
907 if(json_typeof(json1) != json_typeof(json2))
910 /* this covers true, false and null as they are singletons */
914 if(json_is_object(json1))
915 return json_object_equal(json1, json2);
917 if(json_is_array(json1))
918 return json_array_equal(json1, json2);
920 if(json_is_string(json1))
921 return json_string_equal(json1, json2);
923 if(json_is_integer(json1))
924 return json_integer_equal(json1, json2);
926 if(json_is_real(json1))
927 return json_real_equal(json1, json2);
935 json_t *json_copy(json_t *json)
940 if(json_is_object(json))
941 return json_object_copy(json);
943 if(json_is_array(json))
944 return json_array_copy(json);
946 if(json_is_string(json))
947 return json_string_copy(json);
949 if(json_is_integer(json))
950 return json_integer_copy(json);
952 if(json_is_real(json))
953 return json_real_copy(json);
955 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
961 json_t *json_deep_copy(json_t *json)
966 if(json_is_object(json))
967 return json_object_deep_copy(json);
969 if(json_is_array(json))
970 return json_array_deep_copy(json);
972 /* for the rest of the types, deep copying doesn't differ from
975 if(json_is_string(json))
976 return json_string_copy(json);
978 if(json_is_integer(json))
979 return json_integer_copy(json);
981 if(json_is_real(json))
982 return json_real_copy(json);
984 if(json_is_true(json) || json_is_false(json) || json_is_null(json))