2 * Copyright (c) 2009-2012 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 json_t *json_object(void)
31 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
34 json_init(&object->json, JSON_OBJECT);
36 if(hashtable_init(&object->hashtable))
48 static void json_delete_object(json_object_t *object)
50 hashtable_close(&object->hashtable);
54 size_t json_object_size(const json_t *json)
56 json_object_t *object;
58 if(!json_is_object(json))
61 object = json_to_object(json);
62 return object->hashtable.size;
65 json_t *json_object_get(const json_t *json, const char *key)
67 json_object_t *object;
69 if(!json_is_object(json))
72 object = json_to_object(json);
73 return hashtable_get(&object->hashtable, key);
76 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
78 json_object_t *object;
83 if(!key || !json_is_object(json) || json == value)
88 object = json_to_object(json);
90 if(hashtable_set(&object->hashtable, key, object->serial++, value))
99 int json_object_set_new(json_t *json, const char *key, json_t *value)
101 if(!key || !utf8_check_string(key, -1))
107 return json_object_set_new_nocheck(json, key, value);
110 int json_object_del(json_t *json, const char *key)
112 json_object_t *object;
114 if(!json_is_object(json))
117 object = json_to_object(json);
118 return hashtable_del(&object->hashtable, key);
121 int json_object_clear(json_t *json)
123 json_object_t *object;
125 if(!json_is_object(json))
128 object = json_to_object(json);
130 hashtable_clear(&object->hashtable);
136 int json_object_update(json_t *object, json_t *other)
141 if(!json_is_object(object) || !json_is_object(other))
144 json_object_foreach(other, key, value) {
145 if(json_object_set_nocheck(object, key, value))
152 int json_object_update_existing(json_t *object, json_t *other)
157 if(!json_is_object(object) || !json_is_object(other))
160 json_object_foreach(other, key, value) {
161 if(json_object_get(object, key))
162 json_object_set_nocheck(object, key, value);
168 int json_object_update_missing(json_t *object, json_t *other)
173 if(!json_is_object(object) || !json_is_object(other))
176 json_object_foreach(other, key, value) {
177 if(!json_object_get(object, key))
178 json_object_set_nocheck(object, key, value);
184 void *json_object_iter(json_t *json)
186 json_object_t *object;
188 if(!json_is_object(json))
191 object = json_to_object(json);
192 return hashtable_iter(&object->hashtable);
195 void *json_object_iter_at(json_t *json, const char *key)
197 json_object_t *object;
199 if(!key || !json_is_object(json))
202 object = json_to_object(json);
203 return hashtable_iter_at(&object->hashtable, key);
206 void *json_object_iter_next(json_t *json, void *iter)
208 json_object_t *object;
210 if(!json_is_object(json) || iter == NULL)
213 object = json_to_object(json);
214 return hashtable_iter_next(&object->hashtable, iter);
217 const char *json_object_iter_key(void *iter)
222 return hashtable_iter_key(iter);
225 json_t *json_object_iter_value(void *iter)
230 return (json_t *)hashtable_iter_value(iter);
233 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
235 if(!json_is_object(json) || !iter || !value)
238 hashtable_iter_set(iter, value);
242 void *json_object_key_to_iter(const char *key)
247 return hashtable_key_to_iter(key);
250 static int json_object_equal(json_t *object1, json_t *object2)
253 json_t *value1, *value2;
255 if(json_object_size(object1) != json_object_size(object2))
258 json_object_foreach(object1, key, value1) {
259 value2 = json_object_get(object2, key);
261 if(!json_equal(value1, value2))
268 static json_t *json_object_copy(json_t *object)
275 result = json_object();
279 json_object_foreach(object, key, value)
280 json_object_set_nocheck(result, key, value);
285 static json_t *json_object_deep_copy(json_t *object)
292 result = json_object();
296 json_object_foreach(object, key, value)
297 json_object_set_new_nocheck(result, key, json_deep_copy(value));
305 json_t *json_array(void)
307 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
310 json_init(&array->json, JSON_ARRAY);
315 array->table = jsonp_malloc(array->size * sizeof(json_t *));
326 static void json_delete_array(json_array_t *array)
330 for(i = 0; i < array->entries; i++)
331 json_decref(array->table[i]);
333 jsonp_free(array->table);
337 size_t json_array_size(const json_t *json)
339 if(!json_is_array(json))
342 return json_to_array(json)->entries;
345 json_t *json_array_get(const json_t *json, size_t index)
348 if(!json_is_array(json))
350 array = json_to_array(json);
352 if(index >= array->entries)
355 return array->table[index];
358 int json_array_set_new(json_t *json, size_t index, json_t *value)
365 if(!json_is_array(json) || json == value)
370 array = json_to_array(json);
372 if(index >= array->entries)
378 json_decref(array->table[index]);
379 array->table[index] = value;
384 static void array_move(json_array_t *array, size_t dest,
385 size_t src, size_t count)
387 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
390 static void array_copy(json_t **dest, size_t dpos,
391 json_t **src, size_t spos,
394 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
397 static json_t **json_array_grow(json_array_t *array,
402 json_t **old_table, **new_table;
404 if(array->entries + amount <= array->size)
407 old_table = array->table;
409 new_size = max(array->size + amount, array->size * 2);
410 new_table = jsonp_malloc(new_size * sizeof(json_t *));
414 array->size = new_size;
415 array->table = new_table;
418 array_copy(array->table, 0, old_table, 0, array->entries);
419 jsonp_free(old_table);
426 int json_array_append_new(json_t *json, json_t *value)
433 if(!json_is_array(json) || json == value)
438 array = json_to_array(json);
440 if(!json_array_grow(array, 1, 1)) {
445 array->table[array->entries] = value;
451 int json_array_insert_new(json_t *json, size_t index, json_t *value)
459 if(!json_is_array(json) || json == value) {
463 array = json_to_array(json);
465 if(index > array->entries) {
470 old_table = json_array_grow(array, 1, 0);
476 if(old_table != array->table) {
477 array_copy(array->table, 0, old_table, 0, index);
478 array_copy(array->table, index + 1, old_table, index,
479 array->entries - index);
480 jsonp_free(old_table);
483 array_move(array, index + 1, index, array->entries - index);
485 array->table[index] = value;
491 int json_array_remove(json_t *json, size_t index)
495 if(!json_is_array(json))
497 array = json_to_array(json);
499 if(index >= array->entries)
502 json_decref(array->table[index]);
504 array_move(array, index, index + 1, array->entries - index);
510 int json_array_clear(json_t *json)
515 if(!json_is_array(json))
517 array = json_to_array(json);
519 for(i = 0; i < array->entries; i++)
520 json_decref(array->table[i]);
526 int json_array_extend(json_t *json, json_t *other_json)
528 json_array_t *array, *other;
531 if(!json_is_array(json) || !json_is_array(other_json))
533 array = json_to_array(json);
534 other = json_to_array(other_json);
536 if(!json_array_grow(array, other->entries, 1))
539 for(i = 0; i < other->entries; i++)
540 json_incref(other->table[i]);
542 array_copy(array->table, array->entries, other->table, 0, other->entries);
544 array->entries += other->entries;
548 static int json_array_equal(json_t *array1, json_t *array2)
552 size = json_array_size(array1);
553 if(size != json_array_size(array2))
556 for(i = 0; i < size; i++)
558 json_t *value1, *value2;
560 value1 = json_array_get(array1, i);
561 value2 = json_array_get(array2, i);
563 if(!json_equal(value1, value2))
570 static json_t *json_array_copy(json_t *array)
575 result = json_array();
579 for(i = 0; i < json_array_size(array); i++)
580 json_array_append(result, json_array_get(array, i));
585 static json_t *json_array_deep_copy(json_t *array)
590 result = json_array();
594 for(i = 0; i < json_array_size(array); i++)
595 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
602 json_t *json_string_nocheck(const char *value)
604 json_string_t *string;
609 string = jsonp_malloc(sizeof(json_string_t));
612 json_init(&string->json, JSON_STRING);
614 string->value = jsonp_strdup(value);
620 return &string->json;
623 json_t *json_string(const char *value)
625 if(!value || !utf8_check_string(value, -1))
628 return json_string_nocheck(value);
631 const char *json_string_value(const json_t *json)
633 if(!json_is_string(json))
636 return json_to_string(json)->value;
639 int json_string_set_nocheck(json_t *json, const char *value)
642 json_string_t *string;
644 if(!json_is_string(json) || !value)
647 dup = jsonp_strdup(value);
651 string = json_to_string(json);
652 jsonp_free(string->value);
658 int json_string_set(json_t *json, const char *value)
660 if(!value || !utf8_check_string(value, -1))
663 return json_string_set_nocheck(json, value);
666 static void json_delete_string(json_string_t *string)
668 jsonp_free(string->value);
672 static int json_string_equal(json_t *string1, json_t *string2)
674 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
677 static json_t *json_string_copy(json_t *string)
679 return json_string_nocheck(json_string_value(string));
685 json_t *json_integer(json_int_t value)
687 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
690 json_init(&integer->json, JSON_INTEGER);
692 integer->value = value;
693 return &integer->json;
696 json_int_t json_integer_value(const json_t *json)
698 if(!json_is_integer(json))
701 return json_to_integer(json)->value;
704 int json_integer_set(json_t *json, json_int_t value)
706 if(!json_is_integer(json))
709 json_to_integer(json)->value = value;
714 static void json_delete_integer(json_integer_t *integer)
719 static int json_integer_equal(json_t *integer1, json_t *integer2)
721 return json_integer_value(integer1) == json_integer_value(integer2);
724 static json_t *json_integer_copy(json_t *integer)
726 return json_integer(json_integer_value(integer));
732 json_t *json_real(double value)
734 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
737 json_init(&real->json, JSON_REAL);
743 double json_real_value(const json_t *json)
745 if(!json_is_real(json))
748 return json_to_real(json)->value;
751 int json_real_set(json_t *json, double value)
753 if(!json_is_real(json))
756 json_to_real(json)->value = value;
761 static void json_delete_real(json_real_t *real)
766 static int json_real_equal(json_t *real1, json_t *real2)
768 return json_real_value(real1) == json_real_value(real2);
771 static json_t *json_real_copy(json_t *real)
773 return json_real(json_real_value(real));
779 double json_number_value(const json_t *json)
781 if(json_is_integer(json))
782 return json_integer_value(json);
783 else if(json_is_real(json))
784 return json_real_value(json);
790 /*** simple values ***/
792 json_t *json_true(void)
794 static json_t the_true = {JSON_TRUE, (size_t)-1};
799 json_t *json_false(void)
801 static json_t the_false = {JSON_FALSE, (size_t)-1};
806 json_t *json_null(void)
808 static json_t the_null = {JSON_NULL, (size_t)-1};
815 void json_delete(json_t *json)
817 if(json_is_object(json))
818 json_delete_object(json_to_object(json));
820 else if(json_is_array(json))
821 json_delete_array(json_to_array(json));
823 else if(json_is_string(json))
824 json_delete_string(json_to_string(json));
826 else if(json_is_integer(json))
827 json_delete_integer(json_to_integer(json));
829 else if(json_is_real(json))
830 json_delete_real(json_to_real(json));
832 /* json_delete is not called for true, false or null */
838 int json_equal(json_t *json1, json_t *json2)
843 if(json_typeof(json1) != json_typeof(json2))
846 /* this covers true, false and null as they are singletons */
850 if(json_is_object(json1))
851 return json_object_equal(json1, json2);
853 if(json_is_array(json1))
854 return json_array_equal(json1, json2);
856 if(json_is_string(json1))
857 return json_string_equal(json1, json2);
859 if(json_is_integer(json1))
860 return json_integer_equal(json1, json2);
862 if(json_is_real(json1))
863 return json_real_equal(json1, json2);
871 json_t *json_copy(json_t *json)
876 if(json_is_object(json))
877 return json_object_copy(json);
879 if(json_is_array(json))
880 return json_array_copy(json);
882 if(json_is_string(json))
883 return json_string_copy(json);
885 if(json_is_integer(json))
886 return json_integer_copy(json);
888 if(json_is_real(json))
889 return json_real_copy(json);
891 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
897 json_t *json_deep_copy(json_t *json)
902 if(json_is_object(json))
903 return json_object_deep_copy(json);
905 if(json_is_array(json))
906 return json_array_deep_copy(json);
908 /* for the rest of the types, deep copying doesn't differ from
911 if(json_is_string(json))
912 return json_string_copy(json);
914 if(json_is_integer(json))
915 return json_integer_copy(json);
917 if(json_is_real(json))
918 return json_real_copy(json);
920 if(json_is_true(json) || json_is_false(json) || json_is_null(json))