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.
17 #include "hashtable.h"
18 #include "jansson_private.h"
23 static inline void json_init(json_t *json, json_type type)
32 /* This macro just returns a pointer that's a few bytes backwards from
33 string. This makes it possible to pass a pointer to object_key_t
34 when only the string inside it is used, without actually creating
35 an object_key_t instance. */
36 #define string_to_key(string) container_of(string, object_key_t, key)
38 static unsigned int hash_key(const void *ptr)
40 const char *str = ((const object_key_t *)ptr)->key;
42 unsigned int hash = 5381;
45 while((c = (unsigned int)*str))
47 hash = ((hash << 5) + hash) + c;
54 static int key_equal(const void *ptr1, const void *ptr2)
56 return strcmp(((const object_key_t *)ptr1)->key,
57 ((const object_key_t *)ptr2)->key) == 0;
60 static void value_decref(void *value)
62 json_decref((json_t *)value);
65 json_t *json_object(void)
67 json_object_t *object = malloc(sizeof(json_object_t));
70 json_init(&object->json, JSON_OBJECT);
72 if(hashtable_init(&object->hashtable, hash_key, key_equal,
85 static void json_delete_object(json_object_t *object)
87 hashtable_close(&object->hashtable);
91 unsigned int json_object_size(const json_t *json)
93 json_object_t *object;
95 if(!json_is_object(json))
98 object = json_to_object(json);
99 return object->hashtable.size;
102 json_t *json_object_get(const json_t *json, const char *key)
104 json_object_t *object;
106 if(!json_is_object(json))
109 object = json_to_object(json);
110 return hashtable_get(&object->hashtable, string_to_key(key));
113 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
115 json_object_t *object;
121 if(!json_is_object(json) || json == value)
126 object = json_to_object(json);
128 /* offsetof(...) returns the size of object_key_t without the
129 last, flexible member. This way, the correct amount is
131 k = malloc(offsetof(object_key_t, key) +
132 strlen(key) + 1); if(!k) return -1;
134 k->serial = object->serial++;
137 if(hashtable_set(&object->hashtable, k, value))
146 int json_object_set_new(json_t *json, const char *key, json_t *value)
148 if(!key || !utf8_check_string(key, -1))
154 return json_object_set_new_nocheck(json, key, value);
157 int json_object_del(json_t *json, const char *key)
159 json_object_t *object;
161 if(!json_is_object(json))
164 object = json_to_object(json);
165 return hashtable_del(&object->hashtable, string_to_key(key));
168 int json_object_clear(json_t *json)
170 json_object_t *object;
172 if(!json_is_object(json))
175 object = json_to_object(json);
176 hashtable_clear(&object->hashtable);
181 int json_object_update(json_t *object, json_t *other)
185 if(!json_is_object(object) || !json_is_object(other))
188 iter = json_object_iter(other);
193 key = json_object_iter_key(iter);
194 value = json_object_iter_value(iter);
196 if(json_object_set_nocheck(object, key, value))
199 iter = json_object_iter_next(other, iter);
205 void *json_object_iter(json_t *json)
207 json_object_t *object;
209 if(!json_is_object(json))
212 object = json_to_object(json);
213 return hashtable_iter(&object->hashtable);
216 void *json_object_iter_at(json_t *json, const char *key)
218 json_object_t *object;
220 if(!key || !json_is_object(json))
223 object = json_to_object(json);
224 return hashtable_iter_at(&object->hashtable, string_to_key(key));
227 void *json_object_iter_next(json_t *json, void *iter)
229 json_object_t *object;
231 if(!json_is_object(json) || iter == NULL)
234 object = json_to_object(json);
235 return hashtable_iter_next(&object->hashtable, iter);
238 const object_key_t *jsonp_object_iter_fullkey(void *iter)
243 return hashtable_iter_key(iter);
246 const char *json_object_iter_key(void *iter)
251 return jsonp_object_iter_fullkey(iter)->key;
254 json_t *json_object_iter_value(void *iter)
259 return (json_t *)hashtable_iter_value(iter);
262 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
264 json_object_t *object;
266 if(!json_is_object(json) || !iter || !value)
269 object = json_to_object(json);
270 hashtable_iter_set(&object->hashtable, iter, value);
275 static int json_object_equal(json_t *object1, json_t *object2)
279 if(json_object_size(object1) != json_object_size(object2))
282 iter = json_object_iter(object1);
286 json_t *value1, *value2;
288 key = json_object_iter_key(iter);
289 value1 = json_object_iter_value(iter);
290 value2 = json_object_get(object2, key);
292 if(!json_equal(value1, value2))
295 iter = json_object_iter_next(object1, iter);
301 static json_t *json_object_copy(json_t *object)
306 result = json_object();
310 iter = json_object_iter(object);
316 key = json_object_iter_key(iter);
317 value = json_object_iter_value(iter);
318 json_object_set_nocheck(result, key, value);
320 iter = json_object_iter_next(object, iter);
326 static json_t *json_object_deep_copy(json_t *object)
331 result = json_object();
335 iter = json_object_iter(object);
341 key = json_object_iter_key(iter);
342 value = json_object_iter_value(iter);
343 json_object_set_new_nocheck(result, key, json_deep_copy(value));
345 iter = json_object_iter_next(object, iter);
354 json_t *json_array(void)
356 json_array_t *array = malloc(sizeof(json_array_t));
359 json_init(&array->json, JSON_ARRAY);
364 array->table = malloc(array->size * sizeof(json_t *));
375 static void json_delete_array(json_array_t *array)
379 for(i = 0; i < array->entries; i++)
380 json_decref(array->table[i]);
386 unsigned int json_array_size(const json_t *json)
388 if(!json_is_array(json))
391 return json_to_array(json)->entries;
394 json_t *json_array_get(const json_t *json, unsigned int index)
397 if(!json_is_array(json))
399 array = json_to_array(json);
401 if(index >= array->entries)
404 return array->table[index];
407 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
414 if(!json_is_array(json) || json == value)
419 array = json_to_array(json);
421 if(index >= array->entries)
427 json_decref(array->table[index]);
428 array->table[index] = value;
433 static void array_move(json_array_t *array, unsigned int dest,
434 unsigned int src, unsigned int count)
436 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
439 static void array_copy(json_t **dest, unsigned int dpos,
440 json_t **src, unsigned int spos,
443 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
446 static json_t **json_array_grow(json_array_t *array,
450 unsigned int new_size;
451 json_t **old_table, **new_table;
453 if(array->entries + amount <= array->size)
456 old_table = array->table;
458 new_size = max(array->size + amount, array->size * 2);
459 new_table = malloc(new_size * sizeof(json_t *));
463 array->size = new_size;
464 array->table = new_table;
467 array_copy(array->table, 0, old_table, 0, array->entries);
475 int json_array_append_new(json_t *json, json_t *value)
482 if(!json_is_array(json) || json == value)
487 array = json_to_array(json);
489 if(!json_array_grow(array, 1, 1)) {
494 array->table[array->entries] = value;
500 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
508 if(!json_is_array(json) || json == value) {
512 array = json_to_array(json);
514 if(index > array->entries) {
519 old_table = json_array_grow(array, 1, 0);
525 if(old_table != array->table) {
526 array_copy(array->table, 0, old_table, 0, index);
527 array_copy(array->table, index + 1, old_table, index,
528 array->entries - index);
532 array_move(array, index + 1, index, array->entries - index);
534 array->table[index] = value;
540 int json_array_remove(json_t *json, unsigned int index)
544 if(!json_is_array(json))
546 array = json_to_array(json);
548 if(index >= array->entries)
551 json_decref(array->table[index]);
553 array_move(array, index, index + 1, array->entries - index);
559 int json_array_clear(json_t *json)
564 if(!json_is_array(json))
566 array = json_to_array(json);
568 for(i = 0; i < array->entries; i++)
569 json_decref(array->table[i]);
575 int json_array_extend(json_t *json, json_t *other_json)
577 json_array_t *array, *other;
580 if(!json_is_array(json) || !json_is_array(other_json))
582 array = json_to_array(json);
583 other = json_to_array(other_json);
585 if(!json_array_grow(array, other->entries, 1))
588 for(i = 0; i < other->entries; i++)
589 json_incref(other->table[i]);
591 array_copy(array->table, array->entries, other->table, 0, other->entries);
593 array->entries += other->entries;
597 static int json_array_equal(json_t *array1, json_t *array2)
599 unsigned int i, size;
601 size = json_array_size(array1);
602 if(size != json_array_size(array2))
605 for(i = 0; i < size; i++)
607 json_t *value1, *value2;
609 value1 = json_array_get(array1, i);
610 value2 = json_array_get(array2, i);
612 if(!json_equal(value1, value2))
619 static json_t *json_array_copy(json_t *array)
624 result = json_array();
628 for(i = 0; i < json_array_size(array); i++)
629 json_array_append(result, json_array_get(array, i));
634 static json_t *json_array_deep_copy(json_t *array)
639 result = json_array();
643 for(i = 0; i < json_array_size(array); i++)
644 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
651 json_t *json_string_nocheck(const char *value)
653 json_string_t *string;
658 string = malloc(sizeof(json_string_t));
661 json_init(&string->json, JSON_STRING);
663 string->value = strdup(value);
669 return &string->json;
672 json_t *json_string(const char *value)
674 if(!value || !utf8_check_string(value, -1))
677 return json_string_nocheck(value);
680 const char *json_string_value(const json_t *json)
682 if(!json_is_string(json))
685 return json_to_string(json)->value;
688 int json_string_set_nocheck(json_t *json, const char *value)
691 json_string_t *string;
697 string = json_to_string(json);
704 int json_string_set(json_t *json, const char *value)
706 if(!value || !utf8_check_string(value, -1))
709 return json_string_set_nocheck(json, value);
712 static void json_delete_string(json_string_t *string)
718 static int json_string_equal(json_t *string1, json_t *string2)
720 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
723 static json_t *json_string_copy(json_t *string)
725 return json_string_nocheck(json_string_value(string));
731 json_t *json_integer(int value)
733 json_integer_t *integer = malloc(sizeof(json_integer_t));
736 json_init(&integer->json, JSON_INTEGER);
738 integer->value = value;
739 return &integer->json;
742 int json_integer_value(const json_t *json)
744 if(!json_is_integer(json))
747 return json_to_integer(json)->value;
750 int json_integer_set(json_t *json, int value)
752 if(!json_is_integer(json))
755 json_to_integer(json)->value = value;
760 static void json_delete_integer(json_integer_t *integer)
765 static int json_integer_equal(json_t *integer1, json_t *integer2)
767 return json_integer_value(integer1) == json_integer_value(integer2);
770 static json_t *json_integer_copy(json_t *integer)
772 return json_integer(json_integer_value(integer));
778 json_t *json_real(double value)
780 json_real_t *real = malloc(sizeof(json_real_t));
783 json_init(&real->json, JSON_REAL);
789 double json_real_value(const json_t *json)
791 if(!json_is_real(json))
794 return json_to_real(json)->value;
797 int json_real_set(json_t *json, double value)
799 if(!json_is_real(json))
802 json_to_real(json)->value = value;
807 static void json_delete_real(json_real_t *real)
812 static int json_real_equal(json_t *real1, json_t *real2)
814 return json_real_value(real1) == json_real_value(real2);
817 static json_t *json_real_copy(json_t *real)
819 return json_real(json_real_value(real));
825 double json_number_value(const json_t *json)
827 if(json_is_integer(json))
828 return json_integer_value(json);
829 else if(json_is_real(json))
830 return json_real_value(json);
836 /*** simple values ***/
838 json_t *json_true(void)
840 static json_t the_true = {
842 .refcount = (unsigned int)-1
848 json_t *json_false(void)
850 static json_t the_false = {
852 .refcount = (unsigned int)-1
858 json_t *json_null(void)
860 static json_t the_null = {
862 .refcount = (unsigned int)-1
870 void json_delete(json_t *json)
872 if(json_is_object(json))
873 json_delete_object(json_to_object(json));
875 else if(json_is_array(json))
876 json_delete_array(json_to_array(json));
878 else if(json_is_string(json))
879 json_delete_string(json_to_string(json));
881 else if(json_is_integer(json))
882 json_delete_integer(json_to_integer(json));
884 else if(json_is_real(json))
885 json_delete_real(json_to_real(json));
887 /* json_delete is not called for true, false or null */
893 int json_equal(json_t *json1, json_t *json2)
898 if(json_typeof(json1) != json_typeof(json2))
901 /* this covers true, false and null as they are singletons */
905 if(json_is_object(json1))
906 return json_object_equal(json1, json2);
908 if(json_is_array(json1))
909 return json_array_equal(json1, json2);
911 if(json_is_string(json1))
912 return json_string_equal(json1, json2);
914 if(json_is_integer(json1))
915 return json_integer_equal(json1, json2);
917 if(json_is_real(json1))
918 return json_real_equal(json1, json2);
926 json_t *json_copy(json_t *json)
931 if(json_is_object(json))
932 return json_object_copy(json);
934 if(json_is_array(json))
935 return json_array_copy(json);
937 if(json_is_string(json))
938 return json_string_copy(json);
940 if(json_is_integer(json))
941 return json_integer_copy(json);
943 if(json_is_real(json))
944 return json_real_copy(json);
946 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
952 json_t *json_deep_copy(json_t *json)
957 if(json_is_object(json))
958 return json_object_deep_copy(json);
960 if(json_is_array(json))
961 return json_array_deep_copy(json);
963 /* for the rest of the types, deep copying doesn't differ from
966 if(json_is_string(json))
967 return json_string_copy(json);
969 if(json_is_integer(json))
970 return json_integer_copy(json);
972 if(json_is_real(json))
973 return json_real_copy(json);
975 if(json_is_true(json) || json_is_false(json) || json_is_null(json))