2 * Copyright (c) 2009 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 static unsigned int hash_string(const void *key)
30 const char *str = (const char *)key;
31 unsigned int hash = 5381;
34 while((c = (unsigned int)*str))
36 hash = ((hash << 5) + hash) + c;
43 static int string_equal(const void *key1, const void *key2)
45 return strcmp((const char *)key1, (const char *)key2) == 0;
48 static void value_decref(void *value)
50 json_decref((json_t *)value);
53 json_t *json_object(void)
55 json_object_t *object = malloc(sizeof(json_object_t));
58 json_init(&object->json, JSON_OBJECT);
60 if(hashtable_init(&object->hashtable, hash_string, string_equal,
72 static void json_delete_object(json_object_t *object)
74 hashtable_close(&object->hashtable);
78 unsigned int json_object_size(const json_t *json)
80 json_object_t *object;
82 if(!json_is_object(json))
85 object = json_to_object(json);
86 return object->hashtable.size;
89 json_t *json_object_get(const json_t *json, const char *key)
91 json_object_t *object;
93 if(!json_is_object(json))
96 object = json_to_object(json);
97 return hashtable_get(&object->hashtable, key);
100 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
102 json_object_t *object;
107 if(!json_is_object(json) || json == value)
112 object = json_to_object(json);
114 if(hashtable_set(&object->hashtable, strdup(key), value))
123 int json_object_set_new(json_t *json, const char *key, json_t *value)
125 if(!key || !utf8_check_string(key, -1))
131 return json_object_set_new_nocheck(json, key, value);
134 int json_object_del(json_t *json, const char *key)
136 json_object_t *object;
138 if(!json_is_object(json))
141 object = json_to_object(json);
142 return hashtable_del(&object->hashtable, key);
145 int json_object_clear(json_t *json)
147 json_object_t *object;
149 if(!json_is_object(json))
152 object = json_to_object(json);
153 hashtable_clear(&object->hashtable);
158 int json_object_update(json_t *object, json_t *other)
162 if(!json_is_object(object) || !json_is_object(other))
165 iter = json_object_iter(other);
170 key = json_object_iter_key(iter);
171 value = json_object_iter_value(iter);
173 if(json_object_set(object, key, value))
176 iter = json_object_iter_next(other, iter);
182 void *json_object_iter(json_t *json)
184 json_object_t *object;
186 if(!json_is_object(json))
189 object = json_to_object(json);
190 return hashtable_iter(&object->hashtable);
193 void *json_object_iter_next(json_t *json, void *iter)
195 json_object_t *object;
197 if(!json_is_object(json) || iter == NULL)
200 object = json_to_object(json);
201 return hashtable_iter_next(&object->hashtable, iter);
204 const char *json_object_iter_key(void *iter)
209 return (const char *)hashtable_iter_key(iter);
212 json_t *json_object_iter_value(void *iter)
217 return (json_t *)hashtable_iter_value(iter);
220 static int json_object_equal(json_t *object1, json_t *object2)
224 if(json_object_size(object1) != json_object_size(object2))
227 iter = json_object_iter(object1);
231 json_t *value1, *value2;
233 key = json_object_iter_key(iter);
234 value1 = json_object_iter_value(iter);
235 value2 = json_object_get(object2, key);
237 if(!json_equal(value1, value2))
240 iter = json_object_iter_next(object1, iter);
249 json_t *json_array(void)
251 json_array_t *array = malloc(sizeof(json_array_t));
254 json_init(&array->json, JSON_ARRAY);
259 array->table = malloc(array->size * sizeof(json_t *));
270 static void json_delete_array(json_array_t *array)
274 for(i = 0; i < array->entries; i++)
275 json_decref(array->table[i]);
281 unsigned int json_array_size(const json_t *json)
283 if(!json_is_array(json))
286 return json_to_array(json)->entries;
289 json_t *json_array_get(const json_t *json, unsigned int index)
292 if(!json_is_array(json))
294 array = json_to_array(json);
296 if(index >= array->entries)
299 return array->table[index];
302 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
309 if(!json_is_array(json) || json == value)
314 array = json_to_array(json);
316 if(index >= array->entries)
322 json_decref(array->table[index]);
323 array->table[index] = value;
328 static void array_move(json_array_t *array, unsigned int dest,
329 unsigned int src, unsigned int count)
331 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
334 static void array_copy(json_t **dest, unsigned int dpos,
335 json_t **src, unsigned int spos,
338 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
341 static json_t **json_array_grow(json_array_t *array,
345 unsigned int new_size;
346 json_t **old_table, **new_table;
348 if(array->entries + amount <= array->size)
351 old_table = array->table;
353 new_size = max(array->size + amount, array->size * 2);
354 new_table = malloc(new_size * sizeof(json_t *));
358 array->size = new_size;
359 array->table = new_table;
362 array_copy(array->table, 0, old_table, 0, array->entries);
370 int json_array_append_new(json_t *json, json_t *value)
377 if(!json_is_array(json) || json == value)
382 array = json_to_array(json);
384 if(!json_array_grow(array, 1, 1)) {
389 array->table[array->entries] = value;
395 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
403 if(!json_is_array(json) || json == value) {
407 array = json_to_array(json);
409 if(index > array->entries) {
414 old_table = json_array_grow(array, 1, 0);
420 if(old_table != array->table) {
421 array_copy(array->table, 0, old_table, 0, index);
422 array_copy(array->table, index + 1, old_table, index,
423 array->entries - index);
427 array_move(array, index + 1, index, array->entries - index);
429 array->table[index] = value;
435 int json_array_remove(json_t *json, unsigned int index)
439 if(!json_is_array(json))
441 array = json_to_array(json);
443 if(index >= array->entries)
446 json_decref(array->table[index]);
448 array_move(array, index, index + 1, array->entries - index);
454 int json_array_clear(json_t *json)
459 if(!json_is_array(json))
461 array = json_to_array(json);
463 for(i = 0; i < array->entries; i++)
464 json_decref(array->table[i]);
470 int json_array_extend(json_t *json, json_t *other_json)
472 json_array_t *array, *other;
475 if(!json_is_array(json) || !json_is_array(other_json))
477 array = json_to_array(json);
478 other = json_to_array(other_json);
480 if(!json_array_grow(array, other->entries, 1))
483 for(i = 0; i < other->entries; i++)
484 json_incref(other->table[i]);
486 array_copy(array->table, array->entries, other->table, 0, other->entries);
488 array->entries += other->entries;
492 static int json_array_equal(json_t *array1, json_t *array2)
494 unsigned int i, size;
496 size = json_array_size(array1);
497 if(size != json_array_size(array2))
500 for(i = 0; i < size; i++)
502 json_t *value1, *value2;
504 value1 = json_array_get(array1, i);
505 value2 = json_array_get(array2, i);
507 if(!json_equal(value1, value2))
517 json_t *json_string_nocheck(const char *value)
519 json_string_t *string;
524 string = malloc(sizeof(json_string_t));
527 json_init(&string->json, JSON_STRING);
529 string->value = strdup(value);
535 return &string->json;
538 json_t *json_string(const char *value)
540 if(!value || !utf8_check_string(value, -1))
543 return json_string_nocheck(value);
546 const char *json_string_value(const json_t *json)
548 if(!json_is_string(json))
551 return json_to_string(json)->value;
554 int json_string_set_nocheck(json_t *json, const char *value)
557 json_string_t *string;
563 string = json_to_string(json);
570 int json_string_set(json_t *json, const char *value)
572 if(!value || !utf8_check_string(value, -1))
575 return json_string_set_nocheck(json, value);
578 static void json_delete_string(json_string_t *string)
584 static int json_string_equal(json_t *string1, json_t *string2)
586 return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
591 json_t *json_integer(int value)
593 json_integer_t *integer = malloc(sizeof(json_integer_t));
596 json_init(&integer->json, JSON_INTEGER);
598 integer->value = value;
599 return &integer->json;
602 int json_integer_value(const json_t *json)
604 if(!json_is_integer(json))
607 return json_to_integer(json)->value;
610 int json_integer_set(json_t *json, int value)
612 if(!json_is_integer(json))
615 json_to_integer(json)->value = value;
620 static void json_delete_integer(json_integer_t *integer)
625 static int json_integer_equal(json_t *integer1, json_t *integer2)
627 return json_integer_value(integer1) == json_integer_value(integer2);
632 json_t *json_real(double value)
634 json_real_t *real = malloc(sizeof(json_real_t));
637 json_init(&real->json, JSON_REAL);
643 double json_real_value(const json_t *json)
645 if(!json_is_real(json))
648 return json_to_real(json)->value;
651 int json_real_set(json_t *json, double value)
653 if(!json_is_real(json))
656 json_to_real(json)->value = value;
661 static void json_delete_real(json_real_t *real)
666 static int json_real_equal(json_t *real1, json_t *real2)
668 return json_real_value(real1) == json_real_value(real2);
673 double json_number_value(const json_t *json)
675 if(json_is_integer(json))
676 return json_integer_value(json);
677 else if(json_is_real(json))
678 return json_real_value(json);
684 /*** simple values ***/
686 json_t *json_true(void)
688 static json_t the_true = {
690 .refcount = (unsigned int)1
696 json_t *json_false(void)
698 static json_t the_false = {
700 .refcount = (unsigned int)1
706 json_t *json_null(void)
708 static json_t the_null = {
710 .refcount = (unsigned int)1
718 void json_delete(json_t *json)
720 if(json_is_object(json))
721 json_delete_object(json_to_object(json));
723 else if(json_is_array(json))
724 json_delete_array(json_to_array(json));
726 else if(json_is_string(json))
727 json_delete_string(json_to_string(json));
729 else if(json_is_integer(json))
730 json_delete_integer(json_to_integer(json));
732 else if(json_is_real(json))
733 json_delete_real(json_to_real(json));
735 /* json_delete is not called for true, false or null */
741 int json_equal(json_t *json1, json_t *json2)
746 if(json_typeof(json1) != json_typeof(json2))
749 /* this covers true, false and null as they are singletons */
753 if(json_is_object(json1))
754 return json_object_equal(json1, json2);
756 if(json_is_array(json1))
757 return json_array_equal(json1, json2);
759 if(json_is_string(json1))
760 return json_string_equal(json1, json2);
762 if(json_is_integer(json1))
763 return json_integer_equal(json1, json2);
765 if(json_is_real(json1))
766 return json_real_equal(json1, json2);