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.
16 #include "hashtable.h"
17 #include "jansson_private.h"
20 /* Work around nonstandard isnan() and isinf() implementations */
22 static JSON_INLINE int isnan(double x) { return x != x; }
25 static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
28 static JSON_INLINE void json_init(json_t *json, json_type type)
37 json_t *json_object(void)
39 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
42 json_init(&object->json, JSON_OBJECT);
44 if(hashtable_init(&object->hashtable))
56 static void json_delete_object(json_object_t *object)
58 hashtable_close(&object->hashtable);
62 size_t json_object_size(const json_t *json)
64 json_object_t *object;
66 if(!json_is_object(json))
69 object = json_to_object(json);
70 return object->hashtable.size;
73 json_t *json_object_get(const json_t *json, const char *key)
75 json_object_t *object;
77 if(!json_is_object(json))
80 object = json_to_object(json);
81 return hashtable_get(&object->hashtable, key);
84 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
86 json_object_t *object;
91 if(!key || !json_is_object(json) || json == value)
96 object = json_to_object(json);
98 if(hashtable_set(&object->hashtable, key, object->serial++, value))
107 int json_object_set_new(json_t *json, const char *key, json_t *value)
109 if(!key || !utf8_check_string(key, -1))
115 return json_object_set_new_nocheck(json, key, value);
118 int json_object_del(json_t *json, const char *key)
120 json_object_t *object;
122 if(!json_is_object(json))
125 object = json_to_object(json);
126 return hashtable_del(&object->hashtable, key);
129 int json_object_clear(json_t *json)
131 json_object_t *object;
133 if(!json_is_object(json))
136 object = json_to_object(json);
138 hashtable_clear(&object->hashtable);
144 int json_object_update(json_t *object, json_t *other)
149 if(!json_is_object(object) || !json_is_object(other))
152 json_object_foreach(other, key, value) {
153 if(json_object_set_nocheck(object, key, value))
160 int json_object_update_existing(json_t *object, json_t *other)
165 if(!json_is_object(object) || !json_is_object(other))
168 json_object_foreach(other, key, value) {
169 if(json_object_get(object, key))
170 json_object_set_nocheck(object, key, value);
176 int json_object_update_missing(json_t *object, json_t *other)
181 if(!json_is_object(object) || !json_is_object(other))
184 json_object_foreach(other, key, value) {
185 if(!json_object_get(object, key))
186 json_object_set_nocheck(object, key, value);
192 void *json_object_iter(json_t *json)
194 json_object_t *object;
196 if(!json_is_object(json))
199 object = json_to_object(json);
200 return hashtable_iter(&object->hashtable);
203 void *json_object_iter_at(json_t *json, const char *key)
205 json_object_t *object;
207 if(!key || !json_is_object(json))
210 object = json_to_object(json);
211 return hashtable_iter_at(&object->hashtable, key);
214 void *json_object_iter_next(json_t *json, void *iter)
216 json_object_t *object;
218 if(!json_is_object(json) || iter == NULL)
221 object = json_to_object(json);
222 return hashtable_iter_next(&object->hashtable, iter);
225 const char *json_object_iter_key(void *iter)
230 return hashtable_iter_key(iter);
233 json_t *json_object_iter_value(void *iter)
238 return (json_t *)hashtable_iter_value(iter);
241 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
243 if(!json_is_object(json) || !iter || !value)
246 hashtable_iter_set(iter, value);
250 void *json_object_key_to_iter(const char *key)
255 return hashtable_key_to_iter(key);
258 static int json_object_equal(json_t *object1, json_t *object2)
261 json_t *value1, *value2;
263 if(json_object_size(object1) != json_object_size(object2))
266 json_object_foreach(object1, key, value1) {
267 value2 = json_object_get(object2, key);
269 if(!json_equal(value1, value2))
276 static json_t *json_object_copy(json_t *object)
283 result = json_object();
287 json_object_foreach(object, key, value)
288 json_object_set_nocheck(result, key, value);
293 static json_t *json_object_deep_copy(json_t *object)
300 result = json_object();
304 json_object_foreach(object, key, value)
305 json_object_set_new_nocheck(result, key, json_deep_copy(value));
313 json_t *json_array(void)
315 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
318 json_init(&array->json, JSON_ARRAY);
323 array->table = jsonp_malloc(array->size * sizeof(json_t *));
334 static void json_delete_array(json_array_t *array)
338 for(i = 0; i < array->entries; i++)
339 json_decref(array->table[i]);
341 jsonp_free(array->table);
345 size_t json_array_size(const json_t *json)
347 if(!json_is_array(json))
350 return json_to_array(json)->entries;
353 json_t *json_array_get(const json_t *json, size_t index)
356 if(!json_is_array(json))
358 array = json_to_array(json);
360 if(index >= array->entries)
363 return array->table[index];
366 int json_array_set_new(json_t *json, size_t index, json_t *value)
373 if(!json_is_array(json) || json == value)
378 array = json_to_array(json);
380 if(index >= array->entries)
386 json_decref(array->table[index]);
387 array->table[index] = value;
392 static void array_move(json_array_t *array, size_t dest,
393 size_t src, size_t count)
395 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
398 static void array_copy(json_t **dest, size_t dpos,
399 json_t **src, size_t spos,
402 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
405 static json_t **json_array_grow(json_array_t *array,
410 json_t **old_table, **new_table;
412 if(array->entries + amount <= array->size)
415 old_table = array->table;
417 new_size = max(array->size + amount, array->size * 2);
418 new_table = jsonp_malloc(new_size * sizeof(json_t *));
422 array->size = new_size;
423 array->table = new_table;
426 array_copy(array->table, 0, old_table, 0, array->entries);
427 jsonp_free(old_table);
434 int json_array_append_new(json_t *json, json_t *value)
441 if(!json_is_array(json) || json == value)
446 array = json_to_array(json);
448 if(!json_array_grow(array, 1, 1)) {
453 array->table[array->entries] = value;
459 int json_array_insert_new(json_t *json, size_t index, json_t *value)
467 if(!json_is_array(json) || json == value) {
471 array = json_to_array(json);
473 if(index > array->entries) {
478 old_table = json_array_grow(array, 1, 0);
484 if(old_table != array->table) {
485 array_copy(array->table, 0, old_table, 0, index);
486 array_copy(array->table, index + 1, old_table, index,
487 array->entries - index);
488 jsonp_free(old_table);
491 array_move(array, index + 1, index, array->entries - index);
493 array->table[index] = value;
499 int json_array_remove(json_t *json, size_t index)
503 if(!json_is_array(json))
505 array = json_to_array(json);
507 if(index >= array->entries)
510 json_decref(array->table[index]);
512 array_move(array, index, index + 1, array->entries - index);
518 int json_array_clear(json_t *json)
523 if(!json_is_array(json))
525 array = json_to_array(json);
527 for(i = 0; i < array->entries; i++)
528 json_decref(array->table[i]);
534 int json_array_extend(json_t *json, json_t *other_json)
536 json_array_t *array, *other;
539 if(!json_is_array(json) || !json_is_array(other_json))
541 array = json_to_array(json);
542 other = json_to_array(other_json);
544 if(!json_array_grow(array, other->entries, 1))
547 for(i = 0; i < other->entries; i++)
548 json_incref(other->table[i]);
550 array_copy(array->table, array->entries, other->table, 0, other->entries);
552 array->entries += other->entries;
556 static int json_array_equal(json_t *array1, json_t *array2)
560 size = json_array_size(array1);
561 if(size != json_array_size(array2))
564 for(i = 0; i < size; i++)
566 json_t *value1, *value2;
568 value1 = json_array_get(array1, i);
569 value2 = json_array_get(array2, i);
571 if(!json_equal(value1, value2))
578 static json_t *json_array_copy(json_t *array)
583 result = json_array();
587 for(i = 0; i < json_array_size(array); i++)
588 json_array_append(result, json_array_get(array, i));
593 static json_t *json_array_deep_copy(json_t *array)
598 result = json_array();
602 for(i = 0; i < json_array_size(array); i++)
603 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
610 json_t *json_string_nocheck(const char *value)
612 json_string_t *string;
617 string = jsonp_malloc(sizeof(json_string_t));
620 json_init(&string->json, JSON_STRING);
622 string->value = jsonp_strdup(value);
628 return &string->json;
631 json_t *json_string(const char *value)
633 if(!value || !utf8_check_string(value, -1))
636 return json_string_nocheck(value);
639 const char *json_string_value(const json_t *json)
641 if(!json_is_string(json))
644 return json_to_string(json)->value;
647 int json_string_set_nocheck(json_t *json, const char *value)
650 json_string_t *string;
652 if(!json_is_string(json) || !value)
655 dup = jsonp_strdup(value);
659 string = json_to_string(json);
660 jsonp_free(string->value);
666 int json_string_set(json_t *json, const char *value)
668 if(!value || !utf8_check_string(value, -1))
671 return json_string_set_nocheck(json, value);
674 static void json_delete_string(json_string_t *string)
676 jsonp_free(string->value);
680 static int json_string_equal(json_t *string1, json_t *string2)
682 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
685 static json_t *json_string_copy(json_t *string)
687 return json_string_nocheck(json_string_value(string));
693 json_t *json_integer(json_int_t value)
695 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
698 json_init(&integer->json, JSON_INTEGER);
700 integer->value = value;
701 return &integer->json;
704 json_int_t json_integer_value(const json_t *json)
706 if(!json_is_integer(json))
709 return json_to_integer(json)->value;
712 int json_integer_set(json_t *json, json_int_t value)
714 if(!json_is_integer(json))
717 json_to_integer(json)->value = value;
722 static void json_delete_integer(json_integer_t *integer)
727 static int json_integer_equal(json_t *integer1, json_t *integer2)
729 return json_integer_value(integer1) == json_integer_value(integer2);
732 static json_t *json_integer_copy(json_t *integer)
734 return json_integer(json_integer_value(integer));
740 json_t *json_real(double value)
744 if(isnan(value) || isinf(value))
747 real = jsonp_malloc(sizeof(json_real_t));
750 json_init(&real->json, JSON_REAL);
756 double json_real_value(const json_t *json)
758 if(!json_is_real(json))
761 return json_to_real(json)->value;
764 int json_real_set(json_t *json, double value)
766 if(!json_is_real(json) || isnan(value) || isinf(value))
769 json_to_real(json)->value = value;
774 static void json_delete_real(json_real_t *real)
779 static int json_real_equal(json_t *real1, json_t *real2)
781 return json_real_value(real1) == json_real_value(real2);
784 static json_t *json_real_copy(json_t *real)
786 return json_real(json_real_value(real));
792 double json_number_value(const json_t *json)
794 if(json_is_integer(json))
795 return (double)json_integer_value(json);
796 else if(json_is_real(json))
797 return json_real_value(json);
803 /*** simple values ***/
805 json_t *json_true(void)
807 static json_t the_true = {JSON_TRUE, (size_t)-1};
812 json_t *json_false(void)
814 static json_t the_false = {JSON_FALSE, (size_t)-1};
819 json_t *json_null(void)
821 static json_t the_null = {JSON_NULL, (size_t)-1};
828 void json_delete(json_t *json)
830 if(json_is_object(json))
831 json_delete_object(json_to_object(json));
833 else if(json_is_array(json))
834 json_delete_array(json_to_array(json));
836 else if(json_is_string(json))
837 json_delete_string(json_to_string(json));
839 else if(json_is_integer(json))
840 json_delete_integer(json_to_integer(json));
842 else if(json_is_real(json))
843 json_delete_real(json_to_real(json));
845 /* json_delete is not called for true, false or null */
851 int json_equal(json_t *json1, json_t *json2)
856 if(json_typeof(json1) != json_typeof(json2))
859 /* this covers true, false and null as they are singletons */
863 if(json_is_object(json1))
864 return json_object_equal(json1, json2);
866 if(json_is_array(json1))
867 return json_array_equal(json1, json2);
869 if(json_is_string(json1))
870 return json_string_equal(json1, json2);
872 if(json_is_integer(json1))
873 return json_integer_equal(json1, json2);
875 if(json_is_real(json1))
876 return json_real_equal(json1, json2);
884 json_t *json_copy(json_t *json)
889 if(json_is_object(json))
890 return json_object_copy(json);
892 if(json_is_array(json))
893 return json_array_copy(json);
895 if(json_is_string(json))
896 return json_string_copy(json);
898 if(json_is_integer(json))
899 return json_integer_copy(json);
901 if(json_is_real(json))
902 return json_real_copy(json);
904 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
910 json_t *json_deep_copy(json_t *json)
915 if(json_is_object(json))
916 return json_object_deep_copy(json);
918 if(json_is_array(json))
919 return json_array_deep_copy(json);
921 /* for the rest of the types, deep copying doesn't differ from
924 if(json_is_string(json))
925 return json_string_copy(json);
927 if(json_is_integer(json))
928 return json_integer_copy(json);
930 if(json_is_real(json))
931 return json_real_copy(json);
933 if(json_is_true(json) || json_is_false(json) || json_is_null(json))