7 #include "jansson_private.h"
11 #define container_of(ptr_, type_, member_) \
12 ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
16 hashtable_t hashtable;
41 #define json_to_object(json_) container_of(json_, json_object_t, json)
42 #define json_to_array(json_) container_of(json_, json_array_t, json)
43 #define json_to_string(json_) container_of(json_, json_string_t, json)
44 #define json_to_real(json_) container_of(json_, json_real_t, json)
45 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
47 static inline void json_init(json_t *json, json_type type)
56 static unsigned int hash_string(const void *key)
58 const char *str = (const char *)key;
59 unsigned int hash = 5381;
62 while((c = (unsigned int)*str))
64 hash = ((hash << 5) + hash) + c;
71 static int string_equal(const void *key1, const void *key2)
73 return strcmp((const char *)key1, (const char *)key2) == 0;
76 static void value_decref(void *value)
78 json_decref((json_t *)value);
81 json_t *json_object(void)
83 json_object_t *object = malloc(sizeof(json_object_t));
86 json_init(&object->json, JSON_OBJECT);
88 if(hashtable_init(&object->hashtable, hash_string, string_equal,
97 static void json_delete_object(json_object_t *object)
99 hashtable_close(&object->hashtable);
103 json_t *json_object_get(const json_t *json, const char *key)
105 json_object_t *object;
107 if(!json_is_object(json))
110 object = json_to_object(json);
111 return hashtable_get(&object->hashtable, key);
114 int json_object_set_nocheck(json_t *json, const char *key, json_t *value)
116 json_object_t *object;
118 if(!json_is_object(json))
121 object = json_to_object(json);
122 return hashtable_set(&object->hashtable, strdup(key), json_incref(value));
125 int json_object_set(json_t *json, const char *key, json_t *value)
127 if(!utf8_check_string(key, -1))
130 return json_object_set_nocheck(json, key, value);
133 int json_object_del(json_t *json, const char *key)
135 json_object_t *object;
137 if(!json_is_object(json))
140 object = json_to_object(json);
141 return hashtable_del(&object->hashtable, key);
144 void *json_object_iter(json_t *json)
146 json_object_t *object;
148 if(!json_is_object(json))
151 object = json_to_object(json);
152 return hashtable_iter(&object->hashtable);
155 void *json_object_iter_next(json_t *json, void *iter)
157 json_object_t *object;
159 if(!json_is_object(json) || iter == NULL)
162 object = json_to_object(json);
163 return hashtable_iter_next(&object->hashtable, iter);
166 const char *json_object_iter_key(void *iter)
171 return (const char *)hashtable_iter_key(iter);
174 json_t *json_object_iter_value(void *iter)
179 return (json_t *)hashtable_iter_value(iter);
185 json_t *json_array(void)
187 json_array_t *array = malloc(sizeof(json_array_t));
190 json_init(&array->json, JSON_ARRAY);
199 static void json_delete_array(json_array_t *array)
203 for(i = 0; i < array->entries; i++)
204 json_decref(array->table[i]);
210 unsigned int json_array_size(const json_t *json)
212 if(!json_is_array(json))
215 return json_to_array(json)->entries;
218 json_t *json_array_get(const json_t *json, unsigned int index)
221 if(!json_is_array(json))
223 array = json_to_array(json);
225 if(index >= array->size)
228 return array->table[index];
231 int json_array_set(json_t *json, unsigned int index, json_t *value)
234 if(!json_is_array(json))
236 array = json_to_array(json);
238 if(index >= array->size)
241 array->table[index] = json_incref(value);
245 int json_array_append(json_t *json, json_t *value)
248 if(!json_is_array(json))
250 array = json_to_array(json);
252 if(array->entries == array->size) {
253 array->size = max(8, array->size * 2);
254 array->table = realloc(array->table, array->size * sizeof(json_t *));
259 array->table[array->entries] = json_incref(value);
268 json_t *json_string_nocheck(const char *value)
270 json_string_t *string = malloc(sizeof(json_string_t));
273 json_init(&string->json, JSON_STRING);
275 string->value = strdup(value);
276 return &string->json;
279 json_t *json_string(const char *value)
281 if(!utf8_check_string(value, -1))
284 return json_string_nocheck(value);
287 const char *json_string_value(const json_t *json)
289 if(!json_is_string(json))
292 return json_to_string(json)->value;
295 static void json_delete_string(json_string_t *string)
304 json_t *json_integer(int value)
306 json_integer_t *integer = malloc(sizeof(json_integer_t));
309 json_init(&integer->json, JSON_INTEGER);
311 integer->value = value;
312 return &integer->json;
315 int json_integer_value(const json_t *json)
317 if(!json_is_integer(json))
320 return json_to_integer(json)->value;
323 static void json_delete_integer(json_integer_t *integer)
331 json_t *json_real(double value)
333 json_real_t *real = malloc(sizeof(json_real_t));
336 json_init(&real->json, JSON_REAL);
342 double json_real_value(const json_t *json)
344 if(!json_is_real(json))
347 return json_to_real(json)->value;
350 static void json_delete_real (json_real_t *real)
358 double json_number_value(const json_t *json)
360 if(json_is_integer(json))
361 return json_integer_value(json);
362 else if(json_is_real(json))
363 return json_real_value(json);
369 /*** simple values ***/
371 json_t *json_true(void)
373 static json_t the_true = {
377 return json_incref(&the_true);
381 json_t *json_false(void)
383 static json_t the_false = {
387 return json_incref(&the_false);
391 json_t *json_null(void)
393 static json_t the_null = {
397 return json_incref(&the_null);
403 void json_delete(json_t *json)
405 if(json_is_object(json))
406 json_delete_object(json_to_object(json));
408 else if(json_is_array(json))
409 json_delete_array(json_to_array(json));
411 else if(json_is_string(json))
412 json_delete_string(json_to_string(json));
414 else if(json_is_integer(json))
415 json_delete_integer(json_to_integer(json));
417 else if(json_is_real(json))
418 json_delete_real(json_to_real(json));
420 /* json_delete is not called for true, false or null */