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);
223 json_t *json_array(void)
225 json_array_t *array = malloc(sizeof(json_array_t));
228 json_init(&array->json, JSON_ARRAY);
233 array->table = malloc(array->size * sizeof(json_t *));
244 static void json_delete_array(json_array_t *array)
248 for(i = 0; i < array->entries; i++)
249 json_decref(array->table[i]);
255 unsigned int json_array_size(const json_t *json)
257 if(!json_is_array(json))
260 return json_to_array(json)->entries;
263 json_t *json_array_get(const json_t *json, unsigned int index)
266 if(!json_is_array(json))
268 array = json_to_array(json);
270 if(index >= array->entries)
273 return array->table[index];
276 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
283 if(!json_is_array(json) || json == value)
288 array = json_to_array(json);
290 if(index >= array->entries)
296 json_decref(array->table[index]);
297 array->table[index] = value;
302 static void array_move(json_array_t *array, unsigned int dest,
303 unsigned int src, unsigned int count)
305 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
308 static void array_copy(json_t **dest, unsigned int dpos,
309 json_t **src, unsigned int spos,
312 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
315 static json_t **json_array_grow(json_array_t *array,
319 unsigned int new_size;
320 json_t **old_table, **new_table;
322 if(array->entries + amount <= array->size)
325 old_table = array->table;
327 new_size = max(array->size + amount, array->size * 2);
328 new_table = malloc(new_size * sizeof(json_t *));
332 array->size = new_size;
333 array->table = new_table;
336 array_copy(array->table, 0, old_table, 0, array->entries);
344 int json_array_append_new(json_t *json, json_t *value)
351 if(!json_is_array(json) || json == value)
356 array = json_to_array(json);
358 if(!json_array_grow(array, 1, 1)) {
363 array->table[array->entries] = value;
369 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
377 if(!json_is_array(json) || json == value) {
381 array = json_to_array(json);
383 if(index > array->entries) {
388 old_table = json_array_grow(array, 1, 0);
394 if(old_table != array->table) {
395 array_copy(array->table, 0, old_table, 0, index);
396 array_copy(array->table, index + 1, old_table, index,
397 array->entries - index);
401 array_move(array, index + 1, index, array->entries - index);
403 array->table[index] = value;
409 int json_array_remove(json_t *json, unsigned int index)
413 if(!json_is_array(json))
415 array = json_to_array(json);
417 if(index >= array->entries)
420 json_decref(array->table[index]);
422 array_move(array, index, index + 1, array->entries - index);
428 int json_array_clear(json_t *json)
433 if(!json_is_array(json))
435 array = json_to_array(json);
437 for(i = 0; i < array->entries; i++)
438 json_decref(array->table[i]);
444 int json_array_extend(json_t *json, json_t *other_json)
446 json_array_t *array, *other;
449 if(!json_is_array(json) || !json_is_array(other_json))
451 array = json_to_array(json);
452 other = json_to_array(other_json);
454 if(!json_array_grow(array, other->entries, 1))
457 for(i = 0; i < other->entries; i++)
458 json_incref(other->table[i]);
460 array_copy(array->table, array->entries, other->table, 0, other->entries);
462 array->entries += other->entries;
469 json_t *json_string_nocheck(const char *value)
471 json_string_t *string;
476 string = malloc(sizeof(json_string_t));
479 json_init(&string->json, JSON_STRING);
481 string->value = strdup(value);
487 return &string->json;
490 json_t *json_string(const char *value)
492 if(!value || !utf8_check_string(value, -1))
495 return json_string_nocheck(value);
498 const char *json_string_value(const json_t *json)
500 if(!json_is_string(json))
503 return json_to_string(json)->value;
506 int json_string_set_nocheck(json_t *json, const char *value)
509 json_string_t *string;
515 string = json_to_string(json);
522 int json_string_set(json_t *json, const char *value)
524 if(!value || !utf8_check_string(value, -1))
527 return json_string_set_nocheck(json, value);
530 static void json_delete_string(json_string_t *string)
539 json_t *json_integer(int value)
541 json_integer_t *integer = malloc(sizeof(json_integer_t));
544 json_init(&integer->json, JSON_INTEGER);
546 integer->value = value;
547 return &integer->json;
550 int json_integer_value(const json_t *json)
552 if(!json_is_integer(json))
555 return json_to_integer(json)->value;
558 int json_integer_set(json_t *json, int value)
560 if(!json_is_integer(json))
563 json_to_integer(json)->value = value;
568 static void json_delete_integer(json_integer_t *integer)
576 json_t *json_real(double value)
578 json_real_t *real = malloc(sizeof(json_real_t));
581 json_init(&real->json, JSON_REAL);
587 double json_real_value(const json_t *json)
589 if(!json_is_real(json))
592 return json_to_real(json)->value;
595 int json_real_set(json_t *json, double value)
597 if(!json_is_real(json))
600 json_to_real(json)->value = value;
605 static void json_delete_real(json_real_t *real)
613 double json_number_value(const json_t *json)
615 if(json_is_integer(json))
616 return json_integer_value(json);
617 else if(json_is_real(json))
618 return json_real_value(json);
624 /*** simple values ***/
626 json_t *json_true(void)
628 static json_t the_true = {
630 .refcount = (unsigned int)1
636 json_t *json_false(void)
638 static json_t the_false = {
640 .refcount = (unsigned int)1
646 json_t *json_null(void)
648 static json_t the_null = {
650 .refcount = (unsigned int)1
658 void json_delete(json_t *json)
660 if(json_is_object(json))
661 json_delete_object(json_to_object(json));
663 else if(json_is_array(json))
664 json_delete_array(json_to_array(json));
666 else if(json_is_string(json))
667 json_delete_string(json_to_string(json));
669 else if(json_is_integer(json))
670 json_delete_integer(json_to_integer(json));
672 else if(json_is_real(json))
673 json_delete_real(json_to_real(json));
675 /* json_delete is not called for true, false or null */