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"
18 #define container_of(ptr_, type_, member_) \
19 ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
23 hashtable_t hashtable;
48 #define json_to_object(json_) container_of(json_, json_object_t, json)
49 #define json_to_array(json_) container_of(json_, json_array_t, json)
50 #define json_to_string(json_) container_of(json_, json_string_t, json)
51 #define json_to_real(json_) container_of(json_, json_real_t, json)
52 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
54 static inline void json_init(json_t *json, json_type type)
63 static unsigned int hash_string(const void *key)
65 const char *str = (const char *)key;
66 unsigned int hash = 5381;
69 while((c = (unsigned int)*str))
71 hash = ((hash << 5) + hash) + c;
78 static int string_equal(const void *key1, const void *key2)
80 return strcmp((const char *)key1, (const char *)key2) == 0;
83 static void value_decref(void *value)
85 json_decref((json_t *)value);
88 json_t *json_object(void)
90 json_object_t *object = malloc(sizeof(json_object_t));
93 json_init(&object->json, JSON_OBJECT);
95 if(hashtable_init(&object->hashtable, hash_string, string_equal,
101 return &object->json;
104 static void json_delete_object(json_object_t *object)
106 hashtable_close(&object->hashtable);
110 unsigned int json_object_size(const json_t *json)
112 json_object_t *object;
114 if(!json_is_object(json))
117 object = json_to_object(json);
118 return object->hashtable.size;
121 json_t *json_object_get(const json_t *json, const char *key)
123 json_object_t *object;
125 if(!json_is_object(json))
128 object = json_to_object(json);
129 return hashtable_get(&object->hashtable, key);
132 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
134 json_object_t *object;
139 if(!json_is_object(json))
144 object = json_to_object(json);
146 if(hashtable_set(&object->hashtable, strdup(key), value))
155 int json_object_set_nocheck(json_t *json, const char *key, json_t *value)
157 return json_object_set_new_nocheck(json, key, json_incref(value));
160 int json_object_set_new(json_t *json, const char *key, json_t *value)
162 if(!key || !utf8_check_string(key, -1))
168 return json_object_set_new_nocheck(json, key, value);
171 int json_object_del(json_t *json, const char *key)
173 json_object_t *object;
175 if(!json_is_object(json))
178 object = json_to_object(json);
179 return hashtable_del(&object->hashtable, key);
182 int json_object_clear(json_t *json)
184 json_object_t *object;
186 if(!json_is_object(json))
189 object = json_to_object(json);
190 hashtable_clear(&object->hashtable);
195 int json_object_update(json_t *object, json_t *other)
199 if(!json_is_object(object) || !json_is_object(other))
202 iter = json_object_iter(other);
207 key = json_object_iter_key(iter);
208 value = json_object_iter_value(iter);
210 if(json_object_set(object, key, value))
213 iter = json_object_iter_next(other, iter);
219 void *json_object_iter(json_t *json)
221 json_object_t *object;
223 if(!json_is_object(json))
226 object = json_to_object(json);
227 return hashtable_iter(&object->hashtable);
230 void *json_object_iter_next(json_t *json, void *iter)
232 json_object_t *object;
234 if(!json_is_object(json) || iter == NULL)
237 object = json_to_object(json);
238 return hashtable_iter_next(&object->hashtable, iter);
241 const char *json_object_iter_key(void *iter)
246 return (const char *)hashtable_iter_key(iter);
249 json_t *json_object_iter_value(void *iter)
254 return (json_t *)hashtable_iter_value(iter);
260 json_t *json_array(void)
262 json_array_t *array = malloc(sizeof(json_array_t));
265 json_init(&array->json, JSON_ARRAY);
270 array->table = malloc(array->size * sizeof(json_t *));
279 static void json_delete_array(json_array_t *array)
283 for(i = 0; i < array->entries; i++)
284 json_decref(array->table[i]);
290 unsigned int json_array_size(const json_t *json)
292 if(!json_is_array(json))
295 return json_to_array(json)->entries;
298 json_t *json_array_get(const json_t *json, unsigned int index)
301 if(!json_is_array(json))
303 array = json_to_array(json);
305 if(index >= array->entries)
308 return array->table[index];
311 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
318 if(!json_is_array(json))
323 array = json_to_array(json);
325 if(index >= array->entries)
331 json_decref(array->table[index]);
332 array->table[index] = value;
337 static void array_move(json_array_t *array, unsigned int dest,
338 unsigned int src, unsigned int count)
340 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
343 static void array_copy(json_t **dest, unsigned int dpos,
344 json_t **src, unsigned int spos,
347 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
350 static json_t **json_array_grow(json_array_t *array,
354 unsigned int new_size;
355 json_t **old_table, **new_table;
357 if(array->entries + amount <= array->size)
360 old_table = array->table;
362 new_size = max(array->size + amount, array->size * 2);
363 new_table = malloc(new_size * sizeof(json_t *));
367 array->size = new_size;
368 array->table = new_table;
371 array_copy(array->table, 0, old_table, 0, array->entries);
379 int json_array_append_new(json_t *json, json_t *value)
386 if(!json_is_array(json))
391 array = json_to_array(json);
393 if(!json_array_grow(array, 1, 1)) {
398 array->table[array->entries] = value;
404 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
412 if(!json_is_array(json)) {
416 array = json_to_array(json);
418 if(index > array->entries) {
423 old_table = json_array_grow(array, 1, 0);
429 if(old_table != array->table) {
430 array_copy(array->table, 0, old_table, 0, index);
431 array_copy(array->table, index + 1, old_table, index,
432 array->entries - index);
436 array_move(array, index + 1, index, array->entries - index);
438 array->table[index] = value;
444 int json_array_remove(json_t *json, unsigned int index)
448 if(!json_is_array(json))
450 array = json_to_array(json);
452 if(index >= array->entries)
455 json_decref(array->table[index]);
457 array_move(array, index, index + 1, array->entries - index);
463 int json_array_clear(json_t *json)
468 if(!json_is_array(json))
470 array = json_to_array(json);
472 for(i = 0; i < array->entries; i++)
473 json_decref(array->table[i]);
479 int json_array_extend(json_t *json, json_t *other_json)
481 json_array_t *array, *other;
484 if(!json_is_array(json) || !json_is_array(other_json))
486 array = json_to_array(json);
487 other = json_to_array(other_json);
489 if(!json_array_grow(array, other->entries, 1))
492 for(i = 0; i < other->entries; i++)
493 json_incref(other->table[i]);
495 array_copy(array->table, array->entries, other->table, 0, other->entries);
497 array->entries += other->entries;
504 json_t *json_string_nocheck(const char *value)
506 json_string_t *string;
511 string = malloc(sizeof(json_string_t));
514 json_init(&string->json, JSON_STRING);
516 string->value = strdup(value);
522 return &string->json;
525 json_t *json_string(const char *value)
527 if(!value || !utf8_check_string(value, -1))
530 return json_string_nocheck(value);
533 const char *json_string_value(const json_t *json)
535 if(!json_is_string(json))
538 return json_to_string(json)->value;
541 int json_string_set(const json_t *json, const char *value)
544 json_string_t *string;
546 if(!json_is_string(json) || !value || !utf8_check_string(value, -1))
553 string = json_to_string(json);
560 static void json_delete_string(json_string_t *string)
569 json_t *json_integer(int value)
571 json_integer_t *integer = malloc(sizeof(json_integer_t));
574 json_init(&integer->json, JSON_INTEGER);
576 integer->value = value;
577 return &integer->json;
580 int json_integer_value(const json_t *json)
582 if(!json_is_integer(json))
585 return json_to_integer(json)->value;
588 int json_integer_set(const json_t *json, int value)
590 if(!json_is_integer(json))
593 json_to_integer(json)->value = value;
598 static void json_delete_integer(json_integer_t *integer)
606 json_t *json_real(double value)
608 json_real_t *real = malloc(sizeof(json_real_t));
611 json_init(&real->json, JSON_REAL);
617 double json_real_value(const json_t *json)
619 if(!json_is_real(json))
622 return json_to_real(json)->value;
625 int json_real_set(const json_t *json, double value)
627 if(!json_is_real(json))
630 json_to_real(json)->value = value;
635 static void json_delete_real(json_real_t *real)
643 double json_number_value(const json_t *json)
645 if(json_is_integer(json))
646 return json_integer_value(json);
647 else if(json_is_real(json))
648 return json_real_value(json);
654 /*** simple values ***/
656 json_t *json_true(void)
658 static json_t the_true = {
660 .refcount = (unsigned int)1
666 json_t *json_false(void)
668 static json_t the_false = {
670 .refcount = (unsigned int)1
676 json_t *json_null(void)
678 static json_t the_null = {
680 .refcount = (unsigned int)1
688 void json_delete(json_t *json)
690 if(json_is_object(json))
691 json_delete_object(json_to_object(json));
693 else if(json_is_array(json))
694 json_delete_array(json_to_array(json));
696 else if(json_is_string(json))
697 json_delete_string(json_to_string(json));
699 else if(json_is_integer(json))
700 json_delete_integer(json_to_integer(json));
702 else if(json_is_real(json))
703 json_delete_real(json_to_real(json));
705 /* json_delete is not called for true, false or null */