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_nocheck(json_t *json, const char *key, json_t *value)
125 return json_object_set_new_nocheck(json, key, json_incref(value));
128 int json_object_set_new(json_t *json, const char *key, json_t *value)
130 if(!key || !utf8_check_string(key, -1))
136 return json_object_set_new_nocheck(json, key, value);
139 int json_object_del(json_t *json, const char *key)
141 json_object_t *object;
143 if(!json_is_object(json))
146 object = json_to_object(json);
147 return hashtable_del(&object->hashtable, key);
150 int json_object_clear(json_t *json)
152 json_object_t *object;
154 if(!json_is_object(json))
157 object = json_to_object(json);
158 hashtable_clear(&object->hashtable);
163 int json_object_update(json_t *object, json_t *other)
167 if(!json_is_object(object) || !json_is_object(other))
170 iter = json_object_iter(other);
175 key = json_object_iter_key(iter);
176 value = json_object_iter_value(iter);
178 if(json_object_set(object, key, value))
181 iter = json_object_iter_next(other, iter);
187 void *json_object_iter(json_t *json)
189 json_object_t *object;
191 if(!json_is_object(json))
194 object = json_to_object(json);
195 return hashtable_iter(&object->hashtable);
198 void *json_object_iter_next(json_t *json, void *iter)
200 json_object_t *object;
202 if(!json_is_object(json) || iter == NULL)
205 object = json_to_object(json);
206 return hashtable_iter_next(&object->hashtable, iter);
209 const char *json_object_iter_key(void *iter)
214 return (const char *)hashtable_iter_key(iter);
217 json_t *json_object_iter_value(void *iter)
222 return (json_t *)hashtable_iter_value(iter);
228 json_t *json_array(void)
230 json_array_t *array = malloc(sizeof(json_array_t));
233 json_init(&array->json, JSON_ARRAY);
238 array->table = malloc(array->size * sizeof(json_t *));
249 static void json_delete_array(json_array_t *array)
253 for(i = 0; i < array->entries; i++)
254 json_decref(array->table[i]);
260 unsigned int json_array_size(const json_t *json)
262 if(!json_is_array(json))
265 return json_to_array(json)->entries;
268 json_t *json_array_get(const json_t *json, unsigned int index)
271 if(!json_is_array(json))
273 array = json_to_array(json);
275 if(index >= array->entries)
278 return array->table[index];
281 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
288 if(!json_is_array(json) || json == value)
293 array = json_to_array(json);
295 if(index >= array->entries)
301 json_decref(array->table[index]);
302 array->table[index] = value;
307 static void array_move(json_array_t *array, unsigned int dest,
308 unsigned int src, unsigned int count)
310 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
313 static void array_copy(json_t **dest, unsigned int dpos,
314 json_t **src, unsigned int spos,
317 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
320 static json_t **json_array_grow(json_array_t *array,
324 unsigned int new_size;
325 json_t **old_table, **new_table;
327 if(array->entries + amount <= array->size)
330 old_table = array->table;
332 new_size = max(array->size + amount, array->size * 2);
333 new_table = malloc(new_size * sizeof(json_t *));
337 array->size = new_size;
338 array->table = new_table;
341 array_copy(array->table, 0, old_table, 0, array->entries);
349 int json_array_append_new(json_t *json, json_t *value)
356 if(!json_is_array(json) || json == value)
361 array = json_to_array(json);
363 if(!json_array_grow(array, 1, 1)) {
368 array->table[array->entries] = value;
374 int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
382 if(!json_is_array(json) || json == value) {
386 array = json_to_array(json);
388 if(index > array->entries) {
393 old_table = json_array_grow(array, 1, 0);
399 if(old_table != array->table) {
400 array_copy(array->table, 0, old_table, 0, index);
401 array_copy(array->table, index + 1, old_table, index,
402 array->entries - index);
406 array_move(array, index + 1, index, array->entries - index);
408 array->table[index] = value;
414 int json_array_remove(json_t *json, unsigned int index)
418 if(!json_is_array(json))
420 array = json_to_array(json);
422 if(index >= array->entries)
425 json_decref(array->table[index]);
427 array_move(array, index, index + 1, array->entries - index);
433 int json_array_clear(json_t *json)
438 if(!json_is_array(json))
440 array = json_to_array(json);
442 for(i = 0; i < array->entries; i++)
443 json_decref(array->table[i]);
449 int json_array_extend(json_t *json, json_t *other_json)
451 json_array_t *array, *other;
454 if(!json_is_array(json) || !json_is_array(other_json))
456 array = json_to_array(json);
457 other = json_to_array(other_json);
459 if(!json_array_grow(array, other->entries, 1))
462 for(i = 0; i < other->entries; i++)
463 json_incref(other->table[i]);
465 array_copy(array->table, array->entries, other->table, 0, other->entries);
467 array->entries += other->entries;
474 json_t *json_string_nocheck(const char *value)
476 json_string_t *string;
481 string = malloc(sizeof(json_string_t));
484 json_init(&string->json, JSON_STRING);
486 string->value = strdup(value);
492 return &string->json;
495 json_t *json_string(const char *value)
497 if(!value || !utf8_check_string(value, -1))
500 return json_string_nocheck(value);
503 const char *json_string_value(const json_t *json)
505 if(!json_is_string(json))
508 return json_to_string(json)->value;
511 int json_string_set(json_t *json, const char *value)
514 json_string_t *string;
516 if(!json_is_string(json) || !value || !utf8_check_string(value, -1))
523 string = json_to_string(json);
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 */