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.
16 #include "hashtable.h"
17 #include "jansson_private.h"
22 static inline void json_init(json_t *json, json_type type)
31 /* This macro just returns a pointer that's a few bytes backwards from
32 string. This makes it possible to pass a pointer to object_key_t
33 when only the string inside it is used, without actually creating
34 an object_key_t instance. */
35 #define string_to_key(string) container_of(string, object_key_t, key)
37 static size_t hash_key(const void *ptr)
39 const char *str = ((const object_key_t *)ptr)->key;
44 while((c = (size_t)*str))
46 hash = ((hash << 5) + hash) + c;
53 static int key_equal(const void *ptr1, const void *ptr2)
55 return strcmp(((const object_key_t *)ptr1)->key,
56 ((const object_key_t *)ptr2)->key) == 0;
59 static void value_decref(void *value)
61 json_decref((json_t *)value);
64 json_t *json_object(void)
66 json_object_t *object = malloc(sizeof(json_object_t));
69 json_init(&object->json, JSON_OBJECT);
71 if(hashtable_init(&object->hashtable, hash_key, key_equal,
84 static void json_delete_object(json_object_t *object)
86 hashtable_close(&object->hashtable);
90 size_t json_object_size(const json_t *json)
92 json_object_t *object;
94 if(!json_is_object(json))
97 object = json_to_object(json);
98 return object->hashtable.size;
101 json_t *json_object_get(const json_t *json, const char *key)
103 json_object_t *object;
105 if(!json_is_object(json))
108 object = json_to_object(json);
109 return hashtable_get(&object->hashtable, string_to_key(key));
112 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
114 json_object_t *object;
120 if(!json_is_object(json) || json == value)
125 object = json_to_object(json);
127 k = malloc(sizeof(object_key_t) + strlen(key) + 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(long 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 long 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, long 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 = {
839 .refcount = (size_t)-1
845 json_t *json_false(void)
847 static json_t the_false = {
849 .refcount = (size_t)-1
855 json_t *json_null(void)
857 static json_t the_null = {
859 .refcount = (size_t)-1
867 void json_delete(json_t *json)
869 if(json_is_object(json))
870 json_delete_object(json_to_object(json));
872 else if(json_is_array(json))
873 json_delete_array(json_to_array(json));
875 else if(json_is_string(json))
876 json_delete_string(json_to_string(json));
878 else if(json_is_integer(json))
879 json_delete_integer(json_to_integer(json));
881 else if(json_is_real(json))
882 json_delete_real(json_to_real(json));
884 /* json_delete is not called for true, false or null */
890 int json_equal(json_t *json1, json_t *json2)
895 if(json_typeof(json1) != json_typeof(json2))
898 /* this covers true, false and null as they are singletons */
902 if(json_is_object(json1))
903 return json_object_equal(json1, json2);
905 if(json_is_array(json1))
906 return json_array_equal(json1, json2);
908 if(json_is_string(json1))
909 return json_string_equal(json1, json2);
911 if(json_is_integer(json1))
912 return json_integer_equal(json1, json2);
914 if(json_is_real(json1))
915 return json_real_equal(json1, json2);
923 json_t *json_copy(json_t *json)
928 if(json_is_object(json))
929 return json_object_copy(json);
931 if(json_is_array(json))
932 return json_array_copy(json);
934 if(json_is_string(json))
935 return json_string_copy(json);
937 if(json_is_integer(json))
938 return json_integer_copy(json);
940 if(json_is_real(json))
941 return json_real_copy(json);
943 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
949 json_t *json_deep_copy(json_t *json)
954 if(json_is_object(json))
955 return json_object_deep_copy(json);
957 if(json_is_array(json))
958 return json_array_deep_copy(json);
960 /* for the rest of the types, deep copying doesn't differ from
963 if(json_is_string(json))
964 return json_string_copy(json);
966 if(json_is_integer(json))
967 return json_integer_copy(json);
969 if(json_is_real(json))
970 return json_real_copy(json);
972 if(json_is_true(json) || json_is_false(json) || json_is_null(json))