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;
125 if(!json_is_object(json))
130 object = json_to_object(json);
132 if(hashtable_set(&object->hashtable, strdup(key), value))
141 int json_object_set_nocheck(json_t *json, const char *key, json_t *value)
143 return json_object_set_new_nocheck(json, key, json_incref(value));
146 int json_object_set_new(json_t *json, const char *key, json_t *value)
148 if(!utf8_check_string(key, -1))
154 return json_object_set_new_nocheck(json, key, value);
157 int json_object_del(json_t *json, const char *key)
159 json_object_t *object;
161 if(!json_is_object(json))
164 object = json_to_object(json);
165 return hashtable_del(&object->hashtable, key);
168 void *json_object_iter(json_t *json)
170 json_object_t *object;
172 if(!json_is_object(json))
175 object = json_to_object(json);
176 return hashtable_iter(&object->hashtable);
179 void *json_object_iter_next(json_t *json, void *iter)
181 json_object_t *object;
183 if(!json_is_object(json) || iter == NULL)
186 object = json_to_object(json);
187 return hashtable_iter_next(&object->hashtable, iter);
190 const char *json_object_iter_key(void *iter)
195 return (const char *)hashtable_iter_key(iter);
198 json_t *json_object_iter_value(void *iter)
203 return (json_t *)hashtable_iter_value(iter);
209 json_t *json_array(void)
211 json_array_t *array = malloc(sizeof(json_array_t));
214 json_init(&array->json, JSON_ARRAY);
223 static void json_delete_array(json_array_t *array)
227 for(i = 0; i < array->entries; i++)
228 json_decref(array->table[i]);
234 unsigned int json_array_size(const json_t *json)
236 if(!json_is_array(json))
239 return json_to_array(json)->entries;
242 json_t *json_array_get(const json_t *json, unsigned int index)
245 if(!json_is_array(json))
247 array = json_to_array(json);
249 if(index >= array->entries)
252 return array->table[index];
255 int json_array_set_new(json_t *json, unsigned int index, json_t *value)
258 if(!json_is_array(json))
263 array = json_to_array(json);
265 if(index >= array->entries)
271 json_decref(array->table[index]);
272 array->table[index] = value;
277 int json_array_append_new(json_t *json, json_t *value)
280 if(!json_is_array(json))
285 array = json_to_array(json);
287 if(array->entries == array->size) {
288 array->size = max(8, array->size * 2);
289 array->table = realloc(array->table, array->size * sizeof(json_t *));
297 array->table[array->entries] = value;
306 json_t *json_string_nocheck(const char *value)
308 json_string_t *string = malloc(sizeof(json_string_t));
311 json_init(&string->json, JSON_STRING);
313 string->value = strdup(value);
314 return &string->json;
317 json_t *json_string(const char *value)
319 if(!utf8_check_string(value, -1))
322 return json_string_nocheck(value);
325 const char *json_string_value(const json_t *json)
327 if(!json_is_string(json))
330 return json_to_string(json)->value;
333 static void json_delete_string(json_string_t *string)
342 json_t *json_integer(int value)
344 json_integer_t *integer = malloc(sizeof(json_integer_t));
347 json_init(&integer->json, JSON_INTEGER);
349 integer->value = value;
350 return &integer->json;
353 int json_integer_value(const json_t *json)
355 if(!json_is_integer(json))
358 return json_to_integer(json)->value;
361 static void json_delete_integer(json_integer_t *integer)
369 json_t *json_real(double value)
371 json_real_t *real = malloc(sizeof(json_real_t));
374 json_init(&real->json, JSON_REAL);
380 double json_real_value(const json_t *json)
382 if(!json_is_real(json))
385 return json_to_real(json)->value;
388 static void json_delete_real (json_real_t *real)
396 double json_number_value(const json_t *json)
398 if(json_is_integer(json))
399 return json_integer_value(json);
400 else if(json_is_real(json))
401 return json_real_value(json);
407 /*** simple values ***/
409 json_t *json_true(void)
411 static json_t the_true = {
415 return json_incref(&the_true);
419 json_t *json_false(void)
421 static json_t the_false = {
425 return json_incref(&the_false);
429 json_t *json_null(void)
431 static json_t the_null = {
435 return json_incref(&the_null);
441 void json_delete(json_t *json)
443 if(json_is_object(json))
444 json_delete_object(json_to_object(json));
446 else if(json_is_array(json))
447 json_delete_array(json_to_array(json));
449 else if(json_is_string(json))
450 json_delete_string(json_to_string(json));
452 else if(json_is_integer(json))
453 json_delete_integer(json_to_integer(json));
455 else if(json_is_real(json))
456 json_delete_real(json_to_real(json));
458 /* json_delete is not called for true, false or null */