8 #define max(a, b) ((a) > (b) ? (a) : (b))
10 #define container_of(ptr_, type_, member_) \
11 ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
15 hashtable_t *hashtable;
35 #define json_to_object(json_) container_of(json_, json_object_t, json)
36 #define json_to_array(json_) container_of(json_, json_array_t, json)
37 #define json_to_string(json_) container_of(json_, json_string_t, json)
38 #define json_to_number(json_) container_of(json_, json_number_t, json)
40 static inline void json_init(json_t *json, json_type type)
49 static unsigned int hash_string(const void *key)
51 const char *str = (const char *)key;
52 unsigned int hash = 5381;
55 while((c = (unsigned int)*str))
57 hash = ((hash << 5) + hash) + c;
64 static int string_equal(const void *key1, const void *key2)
66 return strcmp((const char *)key1, (const char *)key2) == 0;
69 static void value_decref(void *value)
71 json_decref((json_t *)value);
74 json_t *json_object(void)
76 json_object_t *object = malloc(sizeof(json_object_t));
79 json_init(&object->json, JSON_OBJECT);
82 hashtable_new(hash_string, string_equal, free, value_decref);
83 if(!object->hashtable)
91 static void json_delete_object(json_object_t *object)
93 hashtable_free(object->hashtable);
97 json_t *json_object_get(const json_t *json, const char *key)
99 json_object_t *object;
101 if(!json_is_object(json))
104 return hashtable_get(object->hashtable, key);
107 int json_object_del(json_t *json, const char *key)
109 json_object_t *object;
111 if(!json_is_object(json))
114 object = json_to_object(json);
115 return hashtable_del(object->hashtable, key);
118 int json_object_set(json_t *json, const char *key, json_t *value)
120 json_object_t *object;
122 if(!json_is_object(json))
125 object = json_to_object(json);
126 return hashtable_set(object->hashtable, strdup(key), json_incref(value));
132 json_t *json_array(void)
134 json_array_t *array = malloc(sizeof(json_array_t));
137 json_init(&array->json, JSON_ARRAY);
146 static void json_delete_array(json_array_t *array)
150 for(i = 0; i < array->entries; i++)
151 json_decref(array->table[i]);
157 unsigned int json_array_size(const json_t *json)
159 if(!json_is_array(json))
162 return json_to_array(json)->entries;
165 json_t *json_array_get(const json_t *json, unsigned int index)
168 if(!json_is_array(json))
170 array = json_to_array(json);
172 if(index >= array->size)
175 return array->table[index];
178 int json_array_set(json_t *json, unsigned int index, json_t *value)
181 if(!json_is_array(json))
183 array = json_to_array(json);
185 if(index >= array->size)
188 array->table[index] = json_incref(value);
192 int json_array_append(json_t *json, json_t *value)
195 if(!json_is_array(json))
197 array = json_to_array(json);
199 if(array->entries == array->size) {
200 array->size = max(8, array->size * 2);
201 array->table = realloc(array->table, array->size * sizeof(json_t *));
206 array->table[array->entries] = json_incref(value);
215 json_t *json_string(const char *value)
217 json_string_t *string = malloc(sizeof(json_string_t));
220 json_init(&string->json, JSON_STRING);
222 string->value = strdup(value);
223 return &string->json;
226 const char *json_string_value(const json_t *json)
228 if(!json_is_string(json))
231 return json_to_string(json)->value;
234 static void json_delete_string(json_string_t *string)
240 json_t *json_number(double value)
242 json_number_t *number = malloc(sizeof(json_number_t));
245 json_init(&number->json, JSON_NUMBER);
247 number->value = value;
248 return &number->json;
254 double json_number_value(const json_t *json)
256 if(!json_is_number(json))
259 return json_to_number(json)->value;
262 static void json_delete_number(json_number_t *number)
268 /*** simple values ***/
270 json_t *json_true(void)
272 static json_t the_true = {
276 return json_incref(&the_true);
280 json_t *json_false(void)
282 static json_t the_false = {
286 return json_incref(&the_false);
290 json_t *json_null(void)
292 static json_t the_null = {
296 return json_incref(&the_null);
302 void json_delete(json_t *json)
304 if(json_is_object(json))
305 json_delete_object(json_to_object(json));
307 else if(json_is_array(json))
308 json_delete_array(json_to_array(json));
310 else if(json_is_string(json))
311 json_delete_string(json_to_string(json));
313 else if(json_is_number(json))
314 json_delete_number(json_to_number(json));
316 /* json_delete is not called for true, false or null */