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 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)
140 if(!json_is_object(object) || !json_is_object(other))
143 iter = json_object_iter(other);
148 key = json_object_iter_key(iter);
149 value = json_object_iter_value(iter);
151 if(json_object_set_nocheck(object, key, value))
154 iter = json_object_iter_next(other, iter);
160 void *json_object_iter(json_t *json)
162 json_object_t *object;
164 if(!json_is_object(json))
167 object = json_to_object(json);
168 return hashtable_iter(&object->hashtable);
171 void *json_object_iter_at(json_t *json, const char *key)
173 json_object_t *object;
175 if(!key || !json_is_object(json))
178 object = json_to_object(json);
179 return hashtable_iter_at(&object->hashtable, key);
182 void *json_object_iter_next(json_t *json, void *iter)
184 json_object_t *object;
186 if(!json_is_object(json) || iter == NULL)
189 object = json_to_object(json);
190 return hashtable_iter_next(&object->hashtable, iter);
193 const char *json_object_iter_key(void *iter)
198 return hashtable_iter_key(iter);
201 json_t *json_object_iter_value(void *iter)
206 return (json_t *)hashtable_iter_value(iter);
209 int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
211 if(!json_is_object(json) || !iter || !value)
214 hashtable_iter_set(iter, value);
218 static int json_object_equal(json_t *object1, json_t *object2)
222 if(json_object_size(object1) != json_object_size(object2))
225 iter = json_object_iter(object1);
229 json_t *value1, *value2;
231 key = json_object_iter_key(iter);
232 value1 = json_object_iter_value(iter);
233 value2 = json_object_get(object2, key);
235 if(!json_equal(value1, value2))
238 iter = json_object_iter_next(object1, iter);
244 static json_t *json_object_copy(json_t *object)
249 result = json_object();
253 iter = json_object_iter(object);
259 key = json_object_iter_key(iter);
260 value = json_object_iter_value(iter);
261 json_object_set_nocheck(result, key, value);
263 iter = json_object_iter_next(object, iter);
269 static json_t *json_object_deep_copy(json_t *object)
274 result = json_object();
278 iter = json_object_iter(object);
284 key = json_object_iter_key(iter);
285 value = json_object_iter_value(iter);
286 json_object_set_new_nocheck(result, key, json_deep_copy(value));
288 iter = json_object_iter_next(object, iter);
297 json_t *json_array(void)
299 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
302 json_init(&array->json, JSON_ARRAY);
307 array->table = jsonp_malloc(array->size * sizeof(json_t *));
318 static void json_delete_array(json_array_t *array)
322 for(i = 0; i < array->entries; i++)
323 json_decref(array->table[i]);
325 jsonp_free(array->table);
329 size_t json_array_size(const json_t *json)
331 if(!json_is_array(json))
334 return json_to_array(json)->entries;
337 json_t *json_array_get(const json_t *json, size_t index)
340 if(!json_is_array(json))
342 array = json_to_array(json);
344 if(index >= array->entries)
347 return array->table[index];
350 int json_array_set_new(json_t *json, size_t index, json_t *value)
357 if(!json_is_array(json) || json == value)
362 array = json_to_array(json);
364 if(index >= array->entries)
370 json_decref(array->table[index]);
371 array->table[index] = value;
376 static void array_move(json_array_t *array, size_t dest,
377 size_t src, size_t count)
379 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
382 static void array_copy(json_t **dest, size_t dpos,
383 json_t **src, size_t spos,
386 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
389 static json_t **json_array_grow(json_array_t *array,
394 json_t **old_table, **new_table;
396 if(array->entries + amount <= array->size)
399 old_table = array->table;
401 new_size = max(array->size + amount, array->size * 2);
402 new_table = jsonp_malloc(new_size * sizeof(json_t *));
406 array->size = new_size;
407 array->table = new_table;
410 array_copy(array->table, 0, old_table, 0, array->entries);
411 jsonp_free(old_table);
418 int json_array_append_new(json_t *json, json_t *value)
425 if(!json_is_array(json) || json == value)
430 array = json_to_array(json);
432 if(!json_array_grow(array, 1, 1)) {
437 array->table[array->entries] = value;
443 int json_array_insert_new(json_t *json, size_t index, json_t *value)
451 if(!json_is_array(json) || json == value) {
455 array = json_to_array(json);
457 if(index > array->entries) {
462 old_table = json_array_grow(array, 1, 0);
468 if(old_table != array->table) {
469 array_copy(array->table, 0, old_table, 0, index);
470 array_copy(array->table, index + 1, old_table, index,
471 array->entries - index);
472 jsonp_free(old_table);
475 array_move(array, index + 1, index, array->entries - index);
477 array->table[index] = value;
483 int json_array_remove(json_t *json, size_t index)
487 if(!json_is_array(json))
489 array = json_to_array(json);
491 if(index >= array->entries)
494 json_decref(array->table[index]);
496 array_move(array, index, index + 1, array->entries - index);
502 int json_array_clear(json_t *json)
507 if(!json_is_array(json))
509 array = json_to_array(json);
511 for(i = 0; i < array->entries; i++)
512 json_decref(array->table[i]);
518 int json_array_extend(json_t *json, json_t *other_json)
520 json_array_t *array, *other;
523 if(!json_is_array(json) || !json_is_array(other_json))
525 array = json_to_array(json);
526 other = json_to_array(other_json);
528 if(!json_array_grow(array, other->entries, 1))
531 for(i = 0; i < other->entries; i++)
532 json_incref(other->table[i]);
534 array_copy(array->table, array->entries, other->table, 0, other->entries);
536 array->entries += other->entries;
540 static int json_array_equal(json_t *array1, json_t *array2)
544 size = json_array_size(array1);
545 if(size != json_array_size(array2))
548 for(i = 0; i < size; i++)
550 json_t *value1, *value2;
552 value1 = json_array_get(array1, i);
553 value2 = json_array_get(array2, i);
555 if(!json_equal(value1, value2))
562 static json_t *json_array_copy(json_t *array)
567 result = json_array();
571 for(i = 0; i < json_array_size(array); i++)
572 json_array_append(result, json_array_get(array, i));
577 static json_t *json_array_deep_copy(json_t *array)
582 result = json_array();
586 for(i = 0; i < json_array_size(array); i++)
587 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
594 json_t *json_string_nocheck(const char *value)
596 json_string_t *string;
601 string = jsonp_malloc(sizeof(json_string_t));
604 json_init(&string->json, JSON_STRING);
606 string->value = jsonp_strdup(value);
612 return &string->json;
615 json_t *json_string(const char *value)
617 if(!value || !utf8_check_string(value, -1))
620 return json_string_nocheck(value);
623 const char *json_string_value(const json_t *json)
625 if(!json_is_string(json))
628 return json_to_string(json)->value;
631 int json_string_set_nocheck(json_t *json, const char *value)
634 json_string_t *string;
636 if(!json_is_string(json) || !value)
639 dup = jsonp_strdup(value);
643 string = json_to_string(json);
644 jsonp_free(string->value);
650 int json_string_set(json_t *json, const char *value)
652 if(!value || !utf8_check_string(value, -1))
655 return json_string_set_nocheck(json, value);
658 static void json_delete_string(json_string_t *string)
660 jsonp_free(string->value);
664 static int json_string_equal(json_t *string1, json_t *string2)
666 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
669 static json_t *json_string_copy(json_t *string)
671 return json_string_nocheck(json_string_value(string));
677 json_t *json_integer(json_int_t value)
679 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
682 json_init(&integer->json, JSON_INTEGER);
684 integer->value = value;
685 return &integer->json;
688 json_int_t json_integer_value(const json_t *json)
690 if(!json_is_integer(json))
693 return json_to_integer(json)->value;
696 int json_integer_set(json_t *json, json_int_t value)
698 if(!json_is_integer(json))
701 json_to_integer(json)->value = value;
706 static void json_delete_integer(json_integer_t *integer)
711 static int json_integer_equal(json_t *integer1, json_t *integer2)
713 return json_integer_value(integer1) == json_integer_value(integer2);
716 static json_t *json_integer_copy(json_t *integer)
718 return json_integer(json_integer_value(integer));
724 json_t *json_real(double value)
726 json_real_t *real = jsonp_malloc(sizeof(json_real_t));
729 json_init(&real->json, JSON_REAL);
735 double json_real_value(const json_t *json)
737 if(!json_is_real(json))
740 return json_to_real(json)->value;
743 int json_real_set(json_t *json, double value)
745 if(!json_is_real(json))
748 json_to_real(json)->value = value;
753 static void json_delete_real(json_real_t *real)
758 static int json_real_equal(json_t *real1, json_t *real2)
760 return json_real_value(real1) == json_real_value(real2);
763 static json_t *json_real_copy(json_t *real)
765 return json_real(json_real_value(real));
771 double json_number_value(const json_t *json)
773 if(json_is_integer(json))
774 return json_integer_value(json);
775 else if(json_is_real(json))
776 return json_real_value(json);
782 /*** simple values ***/
784 json_t *json_true(void)
786 static json_t the_true = {JSON_TRUE, (size_t)-1};
791 json_t *json_false(void)
793 static json_t the_false = {JSON_FALSE, (size_t)-1};
798 json_t *json_null(void)
800 static json_t the_null = {JSON_NULL, (size_t)-1};
807 void json_delete(json_t *json)
809 if(json_is_object(json))
810 json_delete_object(json_to_object(json));
812 else if(json_is_array(json))
813 json_delete_array(json_to_array(json));
815 else if(json_is_string(json))
816 json_delete_string(json_to_string(json));
818 else if(json_is_integer(json))
819 json_delete_integer(json_to_integer(json));
821 else if(json_is_real(json))
822 json_delete_real(json_to_real(json));
824 /* json_delete is not called for true, false or null */
830 int json_equal(json_t *json1, json_t *json2)
835 if(json_typeof(json1) != json_typeof(json2))
838 /* this covers true, false and null as they are singletons */
842 if(json_is_object(json1))
843 return json_object_equal(json1, json2);
845 if(json_is_array(json1))
846 return json_array_equal(json1, json2);
848 if(json_is_string(json1))
849 return json_string_equal(json1, json2);
851 if(json_is_integer(json1))
852 return json_integer_equal(json1, json2);
854 if(json_is_real(json1))
855 return json_real_equal(json1, json2);
863 json_t *json_copy(json_t *json)
868 if(json_is_object(json))
869 return json_object_copy(json);
871 if(json_is_array(json))
872 return json_array_copy(json);
874 if(json_is_string(json))
875 return json_string_copy(json);
877 if(json_is_integer(json))
878 return json_integer_copy(json);
880 if(json_is_real(json))
881 return json_real_copy(json);
883 if(json_is_true(json) || json_is_false(json) || json_is_null(json))
889 json_t *json_deep_copy(json_t *json)
894 if(json_is_object(json))
895 return json_object_deep_copy(json);
897 if(json_is_array(json))
898 return json_array_deep_copy(json);
900 /* for the rest of the types, deep copying doesn't differ from
903 if(json_is_string(json))
904 return json_string_copy(json);
906 if(json_is_integer(json))
907 return json_integer_copy(json);
909 if(json_is_real(json))
910 return json_real_copy(json);
912 if(json_is_true(json) || json_is_false(json) || json_is_null(json))