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_set(json_t *json, const char *key, json_t *value)
109 json_object_t *object;
111 if(!json_is_object(json))
114 object = json_to_object(json);
115 return hashtable_set(object->hashtable, strdup(key), json_incref(value));
118 int json_object_del(json_t *json, const char *key)
120 json_object_t *object;
122 if(!json_is_object(json))
125 object = json_to_object(json);
126 return hashtable_del(object->hashtable, key);
129 void *json_object_iter(json_t *json)
131 json_object_t *object;
133 if(!json_is_object(json))
136 object = json_to_object(json);
137 return hashtable_iter(object->hashtable);
140 void *json_object_iter_next(json_t *json, void *iter)
142 json_object_t *object;
144 if(!json_is_object(json) || iter == NULL)
147 object = json_to_object(json);
148 return hashtable_iter_next(object->hashtable, iter);
151 const char *json_object_iter_key(void *iter)
156 return (const char *)hashtable_iter_key(iter);
159 json_t *json_object_iter_value(void *iter)
164 return (json_t *)hashtable_iter_value(iter);
170 json_t *json_array(void)
172 json_array_t *array = malloc(sizeof(json_array_t));
175 json_init(&array->json, JSON_ARRAY);
184 static void json_delete_array(json_array_t *array)
188 for(i = 0; i < array->entries; i++)
189 json_decref(array->table[i]);
195 unsigned int json_array_size(const json_t *json)
197 if(!json_is_array(json))
200 return json_to_array(json)->entries;
203 json_t *json_array_get(const json_t *json, unsigned int index)
206 if(!json_is_array(json))
208 array = json_to_array(json);
210 if(index >= array->size)
213 return array->table[index];
216 int json_array_set(json_t *json, unsigned int index, json_t *value)
219 if(!json_is_array(json))
221 array = json_to_array(json);
223 if(index >= array->size)
226 array->table[index] = json_incref(value);
230 int json_array_append(json_t *json, json_t *value)
233 if(!json_is_array(json))
235 array = json_to_array(json);
237 if(array->entries == array->size) {
238 array->size = max(8, array->size * 2);
239 array->table = realloc(array->table, array->size * sizeof(json_t *));
244 array->table[array->entries] = json_incref(value);
253 json_t *json_string(const char *value)
255 json_string_t *string = malloc(sizeof(json_string_t));
258 json_init(&string->json, JSON_STRING);
260 string->value = strdup(value);
261 return &string->json;
264 const char *json_string_value(const json_t *json)
266 if(!json_is_string(json))
269 return json_to_string(json)->value;
272 static void json_delete_string(json_string_t *string)
278 json_t *json_number(double value)
280 json_number_t *number = malloc(sizeof(json_number_t));
283 json_init(&number->json, JSON_NUMBER);
285 number->value = value;
286 return &number->json;
292 double json_number_value(const json_t *json)
294 if(!json_is_number(json))
297 return json_to_number(json)->value;
300 static void json_delete_number(json_number_t *number)
306 /*** simple values ***/
308 json_t *json_true(void)
310 static json_t the_true = {
314 return json_incref(&the_true);
318 json_t *json_false(void)
320 static json_t the_false = {
324 return json_incref(&the_false);
328 json_t *json_null(void)
330 static json_t the_null = {
334 return json_incref(&the_null);
340 void json_delete(json_t *json)
342 if(json_is_object(json))
343 json_delete_object(json_to_object(json));
345 else if(json_is_array(json))
346 json_delete_array(json_to_array(json));
348 else if(json_is_string(json))
349 json_delete_string(json_to_string(json));
351 else if(json_is_number(json))
352 json_delete_number(json_to_number(json));
354 /* json_delete is not called for true, false or null */