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 /* 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 size_t jsonp_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 int jsonp_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,
70 jsonp_hash_key, jsonp_key_equal,
83 static void json_delete_object(json_object_t *object)
85 hashtable_close(&object->hashtable);
89 size_t json_object_size(const json_t *json)
91 json_object_t *object;
93 if(!json_is_object(json))
96 object = json_to_object(json);
97 return object->hashtable.size;
100 json_t *json_object_get(const json_t *json, const char *key)
102 json_object_t *object;
104 if(!json_is_object(json))
107 object = json_to_object(json);
108 return hashtable_get(&object->hashtable, string_to_key(key));
111 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
113 json_object_t *object;
119 if(!json_is_object(json) || json == value)
124 object = json_to_object(json);
126 /* offsetof(...) returns the size of object_key_t without the
127 last, flexible member. This way, the correct amount is
129 k = malloc(offsetof(object_key_t, key) +
130 strlen(key) + 1); if(!k) return -1;
132 k->serial = object->serial++;
135 if(hashtable_set(&object->hashtable, k, value))
144 int json_object_set_new(json_t *json, const char *key, json_t *value)
146 if(!key || !utf8_check_string(key, -1))
152 return json_object_set_new_nocheck(json, key, value);
155 int json_object_del(json_t *json, const char *key)
157 json_object_t *object;
159 if(!json_is_object(json))
162 object = json_to_object(json);
163 return hashtable_del(&object->hashtable, string_to_key(key));
166 int json_object_clear(json_t *json)
168 json_object_t *object;
170 if(!json_is_object(json))
173 object = json_to_object(json);
174 hashtable_clear(&object->hashtable);
179 int json_object_update(json_t *object, json_t *other)
183 if(!json_is_object(object) || !json_is_object(other))
186 iter = json_object_iter(other);
191 key = json_object_iter_key(iter);
192 value = json_object_iter_value(iter);
194 if(json_object_set_nocheck(object, key, value))
197 iter = json_object_iter_next(other, iter);
203 void *json_object_iter(json_t *json)
205 json_object_t *object;
207 if(!json_is_object(json))
210 object = json_to_object(json);
211 return hashtable_iter(&object->hashtable);
214 void *json_object_iter_at(json_t *json, const char *key)
216 json_object_t *object;
218 if(!key || !json_is_object(json))
221 object = json_to_object(json);
222 return hashtable_iter_at(&object->hashtable, string_to_key(key));
225 void *json_object_iter_next(json_t *json, void *iter)
227 json_object_t *object;
229 if(!json_is_object(json) || iter == NULL)
232 object = json_to_object(json);
233 return hashtable_iter_next(&object->hashtable, iter);
236 const object_key_t *jsonp_object_iter_fullkey(void *iter)
241 return hashtable_iter_key(iter);
244 const char *json_object_iter_key(void *iter)
249 return jsonp_object_iter_fullkey(iter)->key;
252 json_t *json_object_iter_value(void *iter)
257 return (json_t *)hashtable_iter_value(iter);
260 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
262 json_object_t *object;
264 if(!json_is_object(json) || !iter || !value)
267 object = json_to_object(json);
268 hashtable_iter_set(&object->hashtable, iter, value);
273 static int json_object_equal(json_t *object1, json_t *object2)
277 if(json_object_size(object1) != json_object_size(object2))
280 iter = json_object_iter(object1);
284 json_t *value1, *value2;
286 key = json_object_iter_key(iter);
287 value1 = json_object_iter_value(iter);
288 value2 = json_object_get(object2, key);
290 if(!json_equal(value1, value2))
293 iter = json_object_iter_next(object1, iter);
299 static json_t *json_object_copy(json_t *object)
304 result = json_object();
308 iter = json_object_iter(object);
314 key = json_object_iter_key(iter);
315 value = json_object_iter_value(iter);
316 json_object_set_nocheck(result, key, value);
318 iter = json_object_iter_next(object, iter);
324 static json_t *json_object_deep_copy(json_t *object)
329 result = json_object();
333 iter = json_object_iter(object);
339 key = json_object_iter_key(iter);
340 value = json_object_iter_value(iter);
341 json_object_set_new_nocheck(result, key, json_deep_copy(value));
343 iter = json_object_iter_next(object, iter);
352 json_t *json_array(void)
354 json_array_t *array = malloc(sizeof(json_array_t));
357 json_init(&array->json, JSON_ARRAY);
362 array->table = malloc(array->size * sizeof(json_t *));
373 static void json_delete_array(json_array_t *array)
377 for(i = 0; i < array->entries; i++)
378 json_decref(array->table[i]);
384 size_t json_array_size(const json_t *json)
386 if(!json_is_array(json))
389 return json_to_array(json)->entries;
392 json_t *json_array_get(const json_t *json, size_t index)
395 if(!json_is_array(json))
397 array = json_to_array(json);
399 if(index >= array->entries)
402 return array->table[index];
405 int json_array_set_new(json_t *json, size_t index, json_t *value)
412 if(!json_is_array(json) || json == value)
417 array = json_to_array(json);
419 if(index >= array->entries)
425 json_decref(array->table[index]);
426 array->table[index] = value;
431 static void array_move(json_array_t *array, size_t dest,
432 size_t src, size_t count)
434 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
437 static void array_copy(json_t **dest, size_t dpos,
438 json_t **src, size_t spos,
441 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
444 static json_t **json_array_grow(json_array_t *array,
449 json_t **old_table, **new_table;
451 if(array->entries + amount <= array->size)
454 old_table = array->table;
456 new_size = max(array->size + amount, array->size * 2);
457 new_table = malloc(new_size * sizeof(json_t *));
461 array->size = new_size;
462 array->table = new_table;
465 array_copy(array->table, 0, old_table, 0, array->entries);
473 int json_array_append_new(json_t *json, json_t *value)
480 if(!json_is_array(json) || json == value)
485 array = json_to_array(json);
487 if(!json_array_grow(array, 1, 1)) {
492 array->table[array->entries] = value;
498 int json_array_insert_new(json_t *json, size_t index, json_t *value)
506 if(!json_is_array(json) || json == value) {
510 array = json_to_array(json);
512 if(index > array->entries) {
517 old_table = json_array_grow(array, 1, 0);
523 if(old_table != array->table) {
524 array_copy(array->table, 0, old_table, 0, index);
525 array_copy(array->table, index + 1, old_table, index,
526 array->entries - index);
530 array_move(array, index + 1, index, array->entries - index);
532 array->table[index] = value;
538 int json_array_remove(json_t *json, size_t index)
542 if(!json_is_array(json))
544 array = json_to_array(json);
546 if(index >= array->entries)
549 json_decref(array->table[index]);
551 array_move(array, index, index + 1, array->entries - index);
557 int json_array_clear(json_t *json)
562 if(!json_is_array(json))
564 array = json_to_array(json);
566 for(i = 0; i < array->entries; i++)
567 json_decref(array->table[i]);
573 int json_array_extend(json_t *json, json_t *other_json)
575 json_array_t *array, *other;
578 if(!json_is_array(json) || !json_is_array(other_json))
580 array = json_to_array(json);
581 other = json_to_array(other_json);
583 if(!json_array_grow(array, other->entries, 1))
586 for(i = 0; i < other->entries; i++)
587 json_incref(other->table[i]);
589 array_copy(array->table, array->entries, other->table, 0, other->entries);
591 array->entries += other->entries;
595 static int json_array_equal(json_t *array1, json_t *array2)
599 size = json_array_size(array1);
600 if(size != json_array_size(array2))
603 for(i = 0; i < size; i++)
605 json_t *value1, *value2;
607 value1 = json_array_get(array1, i);
608 value2 = json_array_get(array2, i);
610 if(!json_equal(value1, value2))
617 static json_t *json_array_copy(json_t *array)
622 result = json_array();
626 for(i = 0; i < json_array_size(array); i++)
627 json_array_append(result, json_array_get(array, i));
632 static json_t *json_array_deep_copy(json_t *array)
637 result = json_array();
641 for(i = 0; i < json_array_size(array); i++)
642 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
649 json_t *json_string_nocheck(const char *value)
651 json_string_t *string;
656 string = malloc(sizeof(json_string_t));
659 json_init(&string->json, JSON_STRING);
661 string->value = strdup(value);
667 return &string->json;
670 json_t *json_string(const char *value)
672 if(!value || !utf8_check_string(value, -1))
675 return json_string_nocheck(value);
678 const char *json_string_value(const json_t *json)
680 if(!json_is_string(json))
683 return json_to_string(json)->value;
686 int json_string_set_nocheck(json_t *json, const char *value)
689 json_string_t *string;
695 string = json_to_string(json);
702 int json_string_set(json_t *json, const char *value)
704 if(!value || !utf8_check_string(value, -1))
707 return json_string_set_nocheck(json, value);
710 static void json_delete_string(json_string_t *string)
716 static int json_string_equal(json_t *string1, json_t *string2)
718 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
721 static json_t *json_string_copy(json_t *string)
723 return json_string_nocheck(json_string_value(string));
729 json_t *json_integer(json_int_t value)
731 json_integer_t *integer = malloc(sizeof(json_integer_t));
734 json_init(&integer->json, JSON_INTEGER);
736 integer->value = value;
737 return &integer->json;
740 json_int_t json_integer_value(const json_t *json)
742 if(!json_is_integer(json))
745 return json_to_integer(json)->value;
748 int json_integer_set(json_t *json, json_int_t value)
750 if(!json_is_integer(json))
753 json_to_integer(json)->value = value;
758 static void json_delete_integer(json_integer_t *integer)
763 static int json_integer_equal(json_t *integer1, json_t *integer2)
765 return json_integer_value(integer1) == json_integer_value(integer2);
768 static json_t *json_integer_copy(json_t *integer)
770 return json_integer(json_integer_value(integer));
776 json_t *json_real(double value)
778 json_real_t *real = malloc(sizeof(json_real_t));
781 json_init(&real->json, JSON_REAL);
787 double json_real_value(const json_t *json)
789 if(!json_is_real(json))
792 return json_to_real(json)->value;
795 int json_real_set(json_t *json, double value)
797 if(!json_is_real(json))
800 json_to_real(json)->value = value;
805 static void json_delete_real(json_real_t *real)
810 static int json_real_equal(json_t *real1, json_t *real2)
812 return json_real_value(real1) == json_real_value(real2);
815 static json_t *json_real_copy(json_t *real)
817 return json_real(json_real_value(real));
823 double json_number_value(const json_t *json)
825 if(json_is_integer(json))
826 return json_integer_value(json);
827 else if(json_is_real(json))
828 return json_real_value(json);
834 /*** simple values ***/
836 json_t *json_true(void)
838 static json_t the_true = {JSON_TRUE, (size_t)-1};
843 json_t *json_false(void)
845 static json_t the_false = {JSON_FALSE, (size_t)-1};
850 json_t *json_null(void)
852 static json_t the_null = {JSON_NULL, (size_t)-1};
859 void json_delete(json_t *json)
861 if(json_is_object(json))
862 json_delete_object(json_to_object(json));
864 else if(json_is_array(json))
865 json_delete_array(json_to_array(json));
867 else if(json_is_string(json))
868 json_delete_string(json_to_string(json));
870 else if(json_is_integer(json))
871 json_delete_integer(json_to_integer(json));
873 else if(json_is_real(json))
874 json_delete_real(json_to_real(json));
876 /* json_delete is not called for true, false or null */
882 int json_equal(json_t *json1, json_t *json2)
887 if(json_typeof(json1) != json_typeof(json2))
890 /* this covers true, false and null as they are singletons */
894 if(json_is_object(json1))
895 return json_object_equal(json1, json2);
897 if(json_is_array(json1))
898 return json_array_equal(json1, json2);
900 if(json_is_string(json1))
901 return json_string_equal(json1, json2);
903 if(json_is_integer(json1))
904 return json_integer_equal(json1, json2);
906 if(json_is_real(json1))
907 return json_real_equal(json1, json2);
915 json_t *json_copy(json_t *json)
920 if(json_is_object(json))
921 return json_object_copy(json);
923 if(json_is_array(json))
924 return json_array_copy(json);
926 if(json_is_string(json))
927 return json_string_copy(json);
929 if(json_is_integer(json))
930 return json_integer_copy(json);
932 if(json_is_real(json))
933 return json_real_copy(json);
935 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
941 json_t *json_deep_copy(json_t *json)
946 if(json_is_object(json))
947 return json_object_deep_copy(json);
949 if(json_is_array(json))
950 return json_array_deep_copy(json);
952 /* for the rest of the types, deep copying doesn't differ from
955 if(json_is_string(json))
956 return json_string_copy(json);
958 if(json_is_integer(json))
959 return json_integer_copy(json);
961 if(json_is_real(json))
962 return json_real_copy(json);
964 if(json_is_true(json) || json_is_false(json) || json_is_null(json))