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 json_t *json_object_get(const 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_get(&object->hashtable, key);
121 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
123 json_object_t *object;
128 if(!json_is_object(json))
133 object = json_to_object(json);
135 if(hashtable_set(&object->hashtable, strdup(key), value))
144 int json_object_set_nocheck(json_t *json, const char *key, json_t *value)
146 return json_object_set_new_nocheck(json, key, json_incref(value));
149 int json_object_set_new(json_t *json, const char *key, json_t *value)
151 if(!utf8_check_string(key, -1))
157 return json_object_set_new_nocheck(json, key, value);
160 int json_object_del(json_t *json, const char *key)
162 json_object_t *object;
164 if(!json_is_object(json))
167 object = json_to_object(json);
168 return hashtable_del(&object->hashtable, key);
171 void *json_object_iter(json_t *json)
173 json_object_t *object;
175 if(!json_is_object(json))
178 object = json_to_object(json);
179 return hashtable_iter(&object->hashtable);
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 (const char *)hashtable_iter_key(iter);
201 json_t *json_object_iter_value(void *iter)
206 return (json_t *)hashtable_iter_value(iter);
212 json_t *json_array(void)
214 json_array_t *array = malloc(sizeof(json_array_t));
217 json_init(&array->json, JSON_ARRAY);
222 array->table = malloc(array->size * sizeof(json_t *));
231 static void json_delete_array(json_array_t *array)
235 for(i = 0; i < array->entries; i++)
236 json_decref(array->table[i]);
242 unsigned int json_array_size(const json_t *json)
244 if(!json_is_array(json))
247 return json_to_array(json)->entries;
250 json_t *json_array_get(const json_t *json, unsigned int index)
253 if(!json_is_array(json))
255 array = json_to_array(json);
257 if(index >= array->entries)
260 return array->table[index];
263 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
270 if(!json_is_array(json))
275 array = json_to_array(json);
277 if(index >= array->entries)
283 json_decref(array->table[index]);
284 array->table[index] = value;
289 static void array_move(json_array_t *array, unsigned int dest,
290 unsigned int src, unsigned int count)
292 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
295 static void array_copy(json_t **dest, unsigned int dpos,
296 json_t **src, unsigned int spos,
299 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
302 static json_t **json_array_grow(json_array_t *array,
306 unsigned int new_size;
307 json_t **old_table, **new_table;
309 if(array->entries + amount <= array->size)
312 old_table = array->table;
314 new_size = max(array->size + amount, array->size * 2);
315 new_table = malloc(new_size * sizeof(json_t *));
319 array->size = new_size;
320 array->table = new_table;
323 array_copy(array->table, 0, old_table, 0, array->entries);
331 int json_array_append_new(json_t *json, json_t *value)
338 if(!json_is_array(json))
343 array = json_to_array(json);
345 if(!json_array_grow(array, 1, 1)) {
350 array->table[array->entries] = value;
356 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
364 if(!json_is_array(json)) {
368 array = json_to_array(json);
370 if(index > array->entries) {
375 old_table = json_array_grow(array, 1, 0);
381 if(old_table != array->table) {
382 array_copy(array->table, 0, old_table, 0, index);
383 array_copy(array->table, index + 1, old_table, index,
384 array->entries - index);
388 array_move(array, index + 1, index, array->entries - index);
390 array->table[index] = value;
396 int json_array_remove(json_t *json, unsigned int index)
400 if(!json_is_array(json))
402 array = json_to_array(json);
404 if(index >= array->entries)
407 json_decref(array->table[index]);
409 array_move(array, index, index + 1, array->entries - index);
415 int json_array_clear(json_t *json)
420 if(!json_is_array(json))
422 array = json_to_array(json);
424 for(i = 0; i < array->entries; i++)
425 json_decref(array->table[i]);
431 int json_array_extend(json_t *json, json_t *other_json)
433 json_array_t *array, *other;
436 if(!json_is_array(json) || !json_is_array(other_json))
438 array = json_to_array(json);
439 other = json_to_array(other_json);
441 if(!json_array_grow(array, other->entries, 1))
444 for(i = 0; i < other->entries; i++)
445 json_incref(other->table[i]);
447 array_copy(array->table, array->entries, other->table, 0, other->entries);
449 array->entries += other->entries;
456 json_t *json_string_nocheck(const char *value)
458 json_string_t *string;
463 string = malloc(sizeof(json_string_t));
466 json_init(&string->json, JSON_STRING);
468 string->value = strdup(value);
474 return &string->json;
477 json_t *json_string(const char *value)
479 if(!value || !utf8_check_string(value, -1))
482 return json_string_nocheck(value);
485 const char *json_string_value(const json_t *json)
487 if(!json_is_string(json))
490 return json_to_string(json)->value;
493 static void json_delete_string(json_string_t *string)
502 json_t *json_integer(int value)
504 json_integer_t *integer = malloc(sizeof(json_integer_t));
507 json_init(&integer->json, JSON_INTEGER);
509 integer->value = value;
510 return &integer->json;
513 int json_integer_value(const json_t *json)
515 if(!json_is_integer(json))
518 return json_to_integer(json)->value;
521 static void json_delete_integer(json_integer_t *integer)
529 json_t *json_real(double value)
531 json_real_t *real = malloc(sizeof(json_real_t));
534 json_init(&real->json, JSON_REAL);
540 double json_real_value(const json_t *json)
542 if(!json_is_real(json))
545 return json_to_real(json)->value;
548 static void json_delete_real (json_real_t *real)
556 double json_number_value(const json_t *json)
558 if(json_is_integer(json))
559 return json_integer_value(json);
560 else if(json_is_real(json))
561 return json_real_value(json);
567 /*** simple values ***/
569 json_t *json_true(void)
571 static json_t the_true = {
573 .refcount = (unsigned int)1
579 json_t *json_false(void)
581 static json_t the_false = {
583 .refcount = (unsigned int)1
589 json_t *json_null(void)
591 static json_t the_null = {
593 .refcount = (unsigned int)1
601 void json_delete(json_t *json)
603 if(json_is_object(json))
604 json_delete_object(json_to_object(json));
606 else if(json_is_array(json))
607 json_delete_array(json_to_array(json));
609 else if(json_is_string(json))
610 json_delete_string(json_to_string(json));
612 else if(json_is_integer(json))
613 json_delete_integer(json_to_integer(json));
615 else if(json_is_real(json))
616 json_delete_real(json_to_real(json));
618 /* json_delete is not called for true, false or null */