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.
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 static size_t 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 static int 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 = malloc(sizeof(json_object_t));
67 json_init(&object->json, JSON_OBJECT);
69 if(hashtable_init(&object->hashtable, hash_key, key_equal,
82 static void json_delete_object(json_object_t *object)
84 hashtable_close(&object->hashtable);
88 size_t json_object_size(const json_t *json)
90 json_object_t *object;
92 if(!json_is_object(json))
95 object = json_to_object(json);
96 return object->hashtable.size;
99 json_t *json_object_get(const json_t *json, const char *key)
101 json_object_t *object;
103 if(!json_is_object(json))
106 object = json_to_object(json);
107 return hashtable_get(&object->hashtable, string_to_key(key));
110 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
112 json_object_t *object;
118 if(!json_is_object(json) || json == value)
123 object = json_to_object(json);
125 /* offsetof(...) returns the size of object_key_t without the
126 last, flexible member. This way, the correct amount is
128 k = malloc(offsetof(object_key_t, key) +
129 strlen(key) + 1); if(!k) return -1;
131 k->serial = object->serial++;
134 if(hashtable_set(&object->hashtable, k, value))
143 int json_object_set_new(json_t *json, const char *key, json_t *value)
145 if(!key || !utf8_check_string(key, -1))
151 return json_object_set_new_nocheck(json, key, value);
154 int json_object_del(json_t *json, const char *key)
156 json_object_t *object;
158 if(!json_is_object(json))
161 object = json_to_object(json);
162 return hashtable_del(&object->hashtable, string_to_key(key));
165 int json_object_clear(json_t *json)
167 json_object_t *object;
169 if(!json_is_object(json))
172 object = json_to_object(json);
173 hashtable_clear(&object->hashtable);
178 int json_object_update(json_t *object, json_t *other)
182 if(!json_is_object(object) || !json_is_object(other))
185 iter = json_object_iter(other);
190 key = json_object_iter_key(iter);
191 value = json_object_iter_value(iter);
193 if(json_object_set_nocheck(object, key, value))
196 iter = json_object_iter_next(other, iter);
202 void *json_object_iter(json_t *json)
204 json_object_t *object;
206 if(!json_is_object(json))
209 object = json_to_object(json);
210 return hashtable_iter(&object->hashtable);
213 void *json_object_iter_at(json_t *json, const char *key)
215 json_object_t *object;
217 if(!key || !json_is_object(json))
220 object = json_to_object(json);
221 return hashtable_iter_at(&object->hashtable, string_to_key(key));
224 void *json_object_iter_next(json_t *json, void *iter)
226 json_object_t *object;
228 if(!json_is_object(json) || iter == NULL)
231 object = json_to_object(json);
232 return hashtable_iter_next(&object->hashtable, iter);
235 const object_key_t *jsonp_object_iter_fullkey(void *iter)
240 return hashtable_iter_key(iter);
243 const char *json_object_iter_key(void *iter)
248 return jsonp_object_iter_fullkey(iter)->key;
251 json_t *json_object_iter_value(void *iter)
256 return (json_t *)hashtable_iter_value(iter);
259 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
261 json_object_t *object;
263 if(!json_is_object(json) || !iter || !value)
266 object = json_to_object(json);
267 hashtable_iter_set(&object->hashtable, iter, value);
272 static int json_object_equal(json_t *object1, json_t *object2)
276 if(json_object_size(object1) != json_object_size(object2))
279 iter = json_object_iter(object1);
283 json_t *value1, *value2;
285 key = json_object_iter_key(iter);
286 value1 = json_object_iter_value(iter);
287 value2 = json_object_get(object2, key);
289 if(!json_equal(value1, value2))
292 iter = json_object_iter_next(object1, iter);
298 static json_t *json_object_copy(json_t *object)
303 result = json_object();
307 iter = json_object_iter(object);
313 key = json_object_iter_key(iter);
314 value = json_object_iter_value(iter);
315 json_object_set_nocheck(result, key, value);
317 iter = json_object_iter_next(object, iter);
323 static json_t *json_object_deep_copy(json_t *object)
328 result = json_object();
332 iter = json_object_iter(object);
338 key = json_object_iter_key(iter);
339 value = json_object_iter_value(iter);
340 json_object_set_new_nocheck(result, key, json_deep_copy(value));
342 iter = json_object_iter_next(object, iter);
351 json_t *json_array(void)
353 json_array_t *array = malloc(sizeof(json_array_t));
356 json_init(&array->json, JSON_ARRAY);
361 array->table = malloc(array->size * sizeof(json_t *));
372 static void json_delete_array(json_array_t *array)
376 for(i = 0; i < array->entries; i++)
377 json_decref(array->table[i]);
383 size_t json_array_size(const json_t *json)
385 if(!json_is_array(json))
388 return json_to_array(json)->entries;
391 json_t *json_array_get(const json_t *json, size_t index)
394 if(!json_is_array(json))
396 array = json_to_array(json);
398 if(index >= array->entries)
401 return array->table[index];
404 int json_array_set_new(json_t *json, size_t index, json_t *value)
411 if(!json_is_array(json) || json == value)
416 array = json_to_array(json);
418 if(index >= array->entries)
424 json_decref(array->table[index]);
425 array->table[index] = value;
430 static void array_move(json_array_t *array, size_t dest,
431 size_t src, size_t count)
433 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
436 static void array_copy(json_t **dest, size_t dpos,
437 json_t **src, size_t spos,
440 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
443 static json_t **json_array_grow(json_array_t *array,
448 json_t **old_table, **new_table;
450 if(array->entries + amount <= array->size)
453 old_table = array->table;
455 new_size = max(array->size + amount, array->size * 2);
456 new_table = malloc(new_size * sizeof(json_t *));
460 array->size = new_size;
461 array->table = new_table;
464 array_copy(array->table, 0, old_table, 0, array->entries);
472 int json_array_append_new(json_t *json, json_t *value)
479 if(!json_is_array(json) || json == value)
484 array = json_to_array(json);
486 if(!json_array_grow(array, 1, 1)) {
491 array->table[array->entries] = value;
497 int json_array_insert_new(json_t *json, size_t index, json_t *value)
505 if(!json_is_array(json) || json == value) {
509 array = json_to_array(json);
511 if(index > array->entries) {
516 old_table = json_array_grow(array, 1, 0);
522 if(old_table != array->table) {
523 array_copy(array->table, 0, old_table, 0, index);
524 array_copy(array->table, index + 1, old_table, index,
525 array->entries - index);
529 array_move(array, index + 1, index, array->entries - index);
531 array->table[index] = value;
537 int json_array_remove(json_t *json, size_t index)
541 if(!json_is_array(json))
543 array = json_to_array(json);
545 if(index >= array->entries)
548 json_decref(array->table[index]);
550 array_move(array, index, index + 1, array->entries - index);
556 int json_array_clear(json_t *json)
561 if(!json_is_array(json))
563 array = json_to_array(json);
565 for(i = 0; i < array->entries; i++)
566 json_decref(array->table[i]);
572 int json_array_extend(json_t *json, json_t *other_json)
574 json_array_t *array, *other;
577 if(!json_is_array(json) || !json_is_array(other_json))
579 array = json_to_array(json);
580 other = json_to_array(other_json);
582 if(!json_array_grow(array, other->entries, 1))
585 for(i = 0; i < other->entries; i++)
586 json_incref(other->table[i]);
588 array_copy(array->table, array->entries, other->table, 0, other->entries);
590 array->entries += other->entries;
594 static int json_array_equal(json_t *array1, json_t *array2)
598 size = json_array_size(array1);
599 if(size != json_array_size(array2))
602 for(i = 0; i < size; i++)
604 json_t *value1, *value2;
606 value1 = json_array_get(array1, i);
607 value2 = json_array_get(array2, i);
609 if(!json_equal(value1, value2))
616 static json_t *json_array_copy(json_t *array)
621 result = json_array();
625 for(i = 0; i < json_array_size(array); i++)
626 json_array_append(result, json_array_get(array, i));
631 static json_t *json_array_deep_copy(json_t *array)
636 result = json_array();
640 for(i = 0; i < json_array_size(array); i++)
641 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
648 json_t *json_string_nocheck(const char *value)
650 json_string_t *string;
655 string = malloc(sizeof(json_string_t));
658 json_init(&string->json, JSON_STRING);
660 string->value = strdup(value);
666 return &string->json;
669 json_t *json_string(const char *value)
671 if(!value || !utf8_check_string(value, -1))
674 return json_string_nocheck(value);
677 const char *json_string_value(const json_t *json)
679 if(!json_is_string(json))
682 return json_to_string(json)->value;
685 int json_string_set_nocheck(json_t *json, const char *value)
688 json_string_t *string;
694 string = json_to_string(json);
701 int json_string_set(json_t *json, const char *value)
703 if(!value || !utf8_check_string(value, -1))
706 return json_string_set_nocheck(json, value);
709 static void json_delete_string(json_string_t *string)
715 static int json_string_equal(json_t *string1, json_t *string2)
717 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
720 static json_t *json_string_copy(json_t *string)
722 return json_string_nocheck(json_string_value(string));
728 json_t *json_integer(json_int_t value)
730 json_integer_t *integer = malloc(sizeof(json_integer_t));
733 json_init(&integer->json, JSON_INTEGER);
735 integer->value = value;
736 return &integer->json;
739 json_int_t json_integer_value(const json_t *json)
741 if(!json_is_integer(json))
744 return json_to_integer(json)->value;
747 int json_integer_set(json_t *json, json_int_t value)
749 if(!json_is_integer(json))
752 json_to_integer(json)->value = value;
757 static void json_delete_integer(json_integer_t *integer)
762 static int json_integer_equal(json_t *integer1, json_t *integer2)
764 return json_integer_value(integer1) == json_integer_value(integer2);
767 static json_t *json_integer_copy(json_t *integer)
769 return json_integer(json_integer_value(integer));
775 json_t *json_real(double value)
777 json_real_t *real = malloc(sizeof(json_real_t));
780 json_init(&real->json, JSON_REAL);
786 double json_real_value(const json_t *json)
788 if(!json_is_real(json))
791 return json_to_real(json)->value;
794 int json_real_set(json_t *json, double value)
796 if(!json_is_real(json))
799 json_to_real(json)->value = value;
804 static void json_delete_real(json_real_t *real)
809 static int json_real_equal(json_t *real1, json_t *real2)
811 return json_real_value(real1) == json_real_value(real2);
814 static json_t *json_real_copy(json_t *real)
816 return json_real(json_real_value(real));
822 double json_number_value(const json_t *json)
824 if(json_is_integer(json))
825 return json_integer_value(json);
826 else if(json_is_real(json))
827 return json_real_value(json);
833 /*** simple values ***/
835 json_t *json_true(void)
837 static json_t the_true = {JSON_TRUE, (size_t)-1};
842 json_t *json_false(void)
844 static json_t the_false = {JSON_FALSE, (size_t)-1};
849 json_t *json_null(void)
851 static json_t the_null = {JSON_NULL, (size_t)-1};
858 void json_delete(json_t *json)
860 if(json_is_object(json))
861 json_delete_object(json_to_object(json));
863 else if(json_is_array(json))
864 json_delete_array(json_to_array(json));
866 else if(json_is_string(json))
867 json_delete_string(json_to_string(json));
869 else if(json_is_integer(json))
870 json_delete_integer(json_to_integer(json));
872 else if(json_is_real(json))
873 json_delete_real(json_to_real(json));
875 /* json_delete is not called for true, false or null */
881 int json_equal(json_t *json1, json_t *json2)
886 if(json_typeof(json1) != json_typeof(json2))
889 /* this covers true, false and null as they are singletons */
893 if(json_is_object(json1))
894 return json_object_equal(json1, json2);
896 if(json_is_array(json1))
897 return json_array_equal(json1, json2);
899 if(json_is_string(json1))
900 return json_string_equal(json1, json2);
902 if(json_is_integer(json1))
903 return json_integer_equal(json1, json2);
905 if(json_is_real(json1))
906 return json_real_equal(json1, json2);
914 json_t *json_copy(json_t *json)
919 if(json_is_object(json))
920 return json_object_copy(json);
922 if(json_is_array(json))
923 return json_array_copy(json);
925 if(json_is_string(json))
926 return json_string_copy(json);
928 if(json_is_integer(json))
929 return json_integer_copy(json);
931 if(json_is_real(json))
932 return json_real_copy(json);
934 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
940 json_t *json_deep_copy(json_t *json)
945 if(json_is_object(json))
946 return json_object_deep_copy(json);
948 if(json_is_array(json))
949 return json_array_deep_copy(json);
951 /* for the rest of the types, deep copying doesn't differ from
954 if(json_is_string(json))
955 return json_string_copy(json);
957 if(json_is_integer(json))
958 return json_integer_copy(json);
960 if(json_is_real(json))
961 return json_real_copy(json);
963 if(json_is_true(json) || json_is_false(json) || json_is_null(json))