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.
13 #include "hashtable.h"
14 #include "jansson_private.h"
19 static inline void json_init(json_t *json, json_type type)
28 /* This macro just returns a pointer that's a few bytes backwards from
29 string. This makes it possible to pass a pointer to object_key_t
30 when only the string inside it is used, without actually creating
31 an object_key_t instance. */
32 #define string_to_key(string) container_of(string, object_key_t, key)
34 static unsigned int hash_key(const void *ptr)
36 const char *str = ((const object_key_t *)ptr)->key;
38 unsigned int hash = 5381;
41 while((c = (unsigned int)*str))
43 hash = ((hash << 5) + hash) + c;
50 static int key_equal(const void *ptr1, const void *ptr2)
52 return strcmp(((const object_key_t *)ptr1)->key,
53 ((const object_key_t *)ptr2)->key) == 0;
56 static void value_decref(void *value)
58 json_decref((json_t *)value);
61 json_t *json_object(void)
63 json_object_t *object = malloc(sizeof(json_object_t));
66 json_init(&object->json, JSON_OBJECT);
68 if(hashtable_init(&object->hashtable, hash_key, key_equal,
81 static void json_delete_object(json_object_t *object)
83 hashtable_close(&object->hashtable);
87 unsigned int json_object_size(const json_t *json)
89 json_object_t *object;
91 if(!json_is_object(json))
94 object = json_to_object(json);
95 return object->hashtable.size;
98 json_t *json_object_get(const json_t *json, const char *key)
100 json_object_t *object;
102 if(!json_is_object(json))
105 object = json_to_object(json);
106 return hashtable_get(&object->hashtable, string_to_key(key));
109 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
111 json_object_t *object;
117 if(!json_is_object(json) || json == value)
122 object = json_to_object(json);
124 k = malloc(sizeof(object_key_t) + strlen(key) + 1);
128 k->serial = object->serial++;
131 if(hashtable_set(&object->hashtable, k, value))
140 int json_object_set_new(json_t *json, const char *key, json_t *value)
142 if(!key || !utf8_check_string(key, -1))
148 return json_object_set_new_nocheck(json, key, value);
151 int json_object_del(json_t *json, const char *key)
153 json_object_t *object;
155 if(!json_is_object(json))
158 object = json_to_object(json);
159 return hashtable_del(&object->hashtable, string_to_key(key));
162 int json_object_clear(json_t *json)
164 json_object_t *object;
166 if(!json_is_object(json))
169 object = json_to_object(json);
170 hashtable_clear(&object->hashtable);
175 int json_object_update(json_t *object, json_t *other)
179 if(!json_is_object(object) || !json_is_object(other))
182 iter = json_object_iter(other);
187 key = json_object_iter_key(iter);
188 value = json_object_iter_value(iter);
190 if(json_object_set_nocheck(object, key, value))
193 iter = json_object_iter_next(other, iter);
199 void *json_object_iter(json_t *json)
201 json_object_t *object;
203 if(!json_is_object(json))
206 object = json_to_object(json);
207 return hashtable_iter(&object->hashtable);
210 void *json_object_iter_at(json_t *json, const char *key)
212 json_object_t *object;
214 if(!key || !json_is_object(json))
217 object = json_to_object(json);
218 return hashtable_iter_at(&object->hashtable, string_to_key(key));
221 void *json_object_iter_next(json_t *json, void *iter)
223 json_object_t *object;
225 if(!json_is_object(json) || iter == NULL)
228 object = json_to_object(json);
229 return hashtable_iter_next(&object->hashtable, iter);
232 const object_key_t *jsonp_object_iter_fullkey(void *iter)
237 return hashtable_iter_key(iter);
240 const char *json_object_iter_key(void *iter)
245 return jsonp_object_iter_fullkey(iter)->key;
248 json_t *json_object_iter_value(void *iter)
253 return (json_t *)hashtable_iter_value(iter);
256 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
258 json_object_t *object;
260 if(!json_is_object(json) || !iter || !value)
263 object = json_to_object(json);
264 hashtable_iter_set(&object->hashtable, iter, value);
269 static int json_object_equal(json_t *object1, json_t *object2)
273 if(json_object_size(object1) != json_object_size(object2))
276 iter = json_object_iter(object1);
280 json_t *value1, *value2;
282 key = json_object_iter_key(iter);
283 value1 = json_object_iter_value(iter);
284 value2 = json_object_get(object2, key);
286 if(!json_equal(value1, value2))
289 iter = json_object_iter_next(object1, iter);
295 static json_t *json_object_copy(json_t *object)
300 result = json_object();
304 iter = json_object_iter(object);
310 key = json_object_iter_key(iter);
311 value = json_object_iter_value(iter);
312 json_object_set_nocheck(result, key, value);
314 iter = json_object_iter_next(object, iter);
320 static json_t *json_object_deep_copy(json_t *object)
325 result = json_object();
329 iter = json_object_iter(object);
335 key = json_object_iter_key(iter);
336 value = json_object_iter_value(iter);
337 json_object_set_new_nocheck(result, key, json_deep_copy(value));
339 iter = json_object_iter_next(object, iter);
348 json_t *json_array(void)
350 json_array_t *array = malloc(sizeof(json_array_t));
353 json_init(&array->json, JSON_ARRAY);
358 array->table = malloc(array->size * sizeof(json_t *));
369 static void json_delete_array(json_array_t *array)
373 for(i = 0; i < array->entries; i++)
374 json_decref(array->table[i]);
380 unsigned int json_array_size(const json_t *json)
382 if(!json_is_array(json))
385 return json_to_array(json)->entries;
388 json_t *json_array_get(const json_t *json, unsigned int index)
391 if(!json_is_array(json))
393 array = json_to_array(json);
395 if(index >= array->entries)
398 return array->table[index];
401 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
408 if(!json_is_array(json) || json == value)
413 array = json_to_array(json);
415 if(index >= array->entries)
421 json_decref(array->table[index]);
422 array->table[index] = value;
427 static void array_move(json_array_t *array, unsigned int dest,
428 unsigned int src, unsigned int count)
430 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
433 static void array_copy(json_t **dest, unsigned int dpos,
434 json_t **src, unsigned int spos,
437 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
440 static json_t **json_array_grow(json_array_t *array,
444 unsigned int new_size;
445 json_t **old_table, **new_table;
447 if(array->entries + amount <= array->size)
450 old_table = array->table;
452 new_size = max(array->size + amount, array->size * 2);
453 new_table = malloc(new_size * sizeof(json_t *));
457 array->size = new_size;
458 array->table = new_table;
461 array_copy(array->table, 0, old_table, 0, array->entries);
469 int json_array_append_new(json_t *json, json_t *value)
476 if(!json_is_array(json) || json == value)
481 array = json_to_array(json);
483 if(!json_array_grow(array, 1, 1)) {
488 array->table[array->entries] = value;
494 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
502 if(!json_is_array(json) || json == value) {
506 array = json_to_array(json);
508 if(index > array->entries) {
513 old_table = json_array_grow(array, 1, 0);
519 if(old_table != array->table) {
520 array_copy(array->table, 0, old_table, 0, index);
521 array_copy(array->table, index + 1, old_table, index,
522 array->entries - index);
526 array_move(array, index + 1, index, array->entries - index);
528 array->table[index] = value;
534 int json_array_remove(json_t *json, unsigned int index)
538 if(!json_is_array(json))
540 array = json_to_array(json);
542 if(index >= array->entries)
545 json_decref(array->table[index]);
547 array_move(array, index, index + 1, array->entries - index);
553 int json_array_clear(json_t *json)
558 if(!json_is_array(json))
560 array = json_to_array(json);
562 for(i = 0; i < array->entries; i++)
563 json_decref(array->table[i]);
569 int json_array_extend(json_t *json, json_t *other_json)
571 json_array_t *array, *other;
574 if(!json_is_array(json) || !json_is_array(other_json))
576 array = json_to_array(json);
577 other = json_to_array(other_json);
579 if(!json_array_grow(array, other->entries, 1))
582 for(i = 0; i < other->entries; i++)
583 json_incref(other->table[i]);
585 array_copy(array->table, array->entries, other->table, 0, other->entries);
587 array->entries += other->entries;
591 static int json_array_equal(json_t *array1, json_t *array2)
593 unsigned int i, size;
595 size = json_array_size(array1);
596 if(size != json_array_size(array2))
599 for(i = 0; i < size; i++)
601 json_t *value1, *value2;
603 value1 = json_array_get(array1, i);
604 value2 = json_array_get(array2, i);
606 if(!json_equal(value1, value2))
613 static json_t *json_array_copy(json_t *array)
618 result = json_array();
622 for(i = 0; i < json_array_size(array); i++)
623 json_array_append(result, json_array_get(array, i));
628 static json_t *json_array_deep_copy(json_t *array)
633 result = json_array();
637 for(i = 0; i < json_array_size(array); i++)
638 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
645 json_t *json_string_nocheck(const char *value)
647 json_string_t *string;
652 string = malloc(sizeof(json_string_t));
655 json_init(&string->json, JSON_STRING);
657 string->value = strdup(value);
663 return &string->json;
666 json_t *json_string(const char *value)
668 if(!value || !utf8_check_string(value, -1))
671 return json_string_nocheck(value);
674 const char *json_string_value(const json_t *json)
676 if(!json_is_string(json))
679 return json_to_string(json)->value;
682 int json_string_set_nocheck(json_t *json, const char *value)
685 json_string_t *string;
691 string = json_to_string(json);
698 int json_string_set(json_t *json, const char *value)
700 if(!value || !utf8_check_string(value, -1))
703 return json_string_set_nocheck(json, value);
706 static void json_delete_string(json_string_t *string)
712 static int json_string_equal(json_t *string1, json_t *string2)
714 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
717 static json_t *json_string_copy(json_t *string)
719 return json_string_nocheck(json_string_value(string));
725 json_t *json_integer(int value)
727 json_integer_t *integer = malloc(sizeof(json_integer_t));
730 json_init(&integer->json, JSON_INTEGER);
732 integer->value = value;
733 return &integer->json;
736 int json_integer_value(const json_t *json)
738 if(!json_is_integer(json))
741 return json_to_integer(json)->value;
744 int json_integer_set(json_t *json, int value)
746 if(!json_is_integer(json))
749 json_to_integer(json)->value = value;
754 static void json_delete_integer(json_integer_t *integer)
759 static int json_integer_equal(json_t *integer1, json_t *integer2)
761 return json_integer_value(integer1) == json_integer_value(integer2);
764 static json_t *json_integer_copy(json_t *integer)
766 return json_integer(json_integer_value(integer));
772 json_t *json_real(double value)
774 json_real_t *real = malloc(sizeof(json_real_t));
777 json_init(&real->json, JSON_REAL);
783 double json_real_value(const json_t *json)
785 if(!json_is_real(json))
788 return json_to_real(json)->value;
791 int json_real_set(json_t *json, double value)
793 if(!json_is_real(json))
796 json_to_real(json)->value = value;
801 static void json_delete_real(json_real_t *real)
806 static int json_real_equal(json_t *real1, json_t *real2)
808 return json_real_value(real1) == json_real_value(real2);
811 static json_t *json_real_copy(json_t *real)
813 return json_real(json_real_value(real));
819 double json_number_value(const json_t *json)
821 if(json_is_integer(json))
822 return json_integer_value(json);
823 else if(json_is_real(json))
824 return json_real_value(json);
830 /*** simple values ***/
832 json_t *json_true(void)
834 static json_t the_true = {
836 .refcount = (unsigned int)1
842 json_t *json_false(void)
844 static json_t the_false = {
846 .refcount = (unsigned int)1
852 json_t *json_null(void)
854 static json_t the_null = {
856 .refcount = (unsigned int)1
864 void json_delete(json_t *json)
866 if(json_is_object(json))
867 json_delete_object(json_to_object(json));
869 else if(json_is_array(json))
870 json_delete_array(json_to_array(json));
872 else if(json_is_string(json))
873 json_delete_string(json_to_string(json));
875 else if(json_is_integer(json))
876 json_delete_integer(json_to_integer(json));
878 else if(json_is_real(json))
879 json_delete_real(json_to_real(json));
881 /* json_delete is not called for true, false or null */
887 int json_equal(json_t *json1, json_t *json2)
892 if(json_typeof(json1) != json_typeof(json2))
895 /* this covers true, false and null as they are singletons */
899 if(json_is_object(json1))
900 return json_object_equal(json1, json2);
902 if(json_is_array(json1))
903 return json_array_equal(json1, json2);
905 if(json_is_string(json1))
906 return json_string_equal(json1, json2);
908 if(json_is_integer(json1))
909 return json_integer_equal(json1, json2);
911 if(json_is_real(json1))
912 return json_real_equal(json1, json2);
920 json_t *json_copy(json_t *json)
925 if(json_is_object(json))
926 return json_object_copy(json);
928 if(json_is_array(json))
929 return json_array_copy(json);
931 if(json_is_string(json))
932 return json_string_copy(json);
934 if(json_is_integer(json))
935 return json_integer_copy(json);
937 if(json_is_real(json))
938 return json_real_copy(json);
940 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
946 json_t *json_deep_copy(json_t *json)
951 if(json_is_object(json))
952 return json_object_deep_copy(json);
954 if(json_is_array(json))
955 return json_array_deep_copy(json);
957 /* for the rest of the types, deep copying doesn't differ from
960 if(json_is_string(json))
961 return json_string_copy(json);
963 if(json_is_integer(json))
964 return json_integer_copy(json);
966 if(json_is_real(json))
967 return json_real_copy(json);
969 if(json_is_true(json) || json_is_false(json) || json_is_null(json))