/*** object ***/
-/* From http://www.cse.yorku.ca/~oz/hash.html */
-size_t jsonp_hash_str(const void *ptr)
-{
- const char *str = (const char *)ptr;
-
- size_t hash = 5381;
- size_t c;
-
- while((c = (size_t)*str))
- {
- hash = ((hash << 5) + hash) + c;
- str++;
- }
-
- return hash;
-}
-
-int jsonp_str_equal(const void *ptr1, const void *ptr2)
-{
- return strcmp((const char *)ptr1, (const char *)ptr2) == 0;
-}
-
-/* This macro just returns a pointer that's a few bytes backwards from
- string. This makes it possible to pass a pointer to object_key_t
- when only the string inside it is used, without actually creating
- an object_key_t instance. */
-#define string_to_key(string) container_of(string, object_key_t, key)
-
-static size_t hash_key(const void *ptr)
-{
- return jsonp_hash_str(((const object_key_t *)ptr)->key);
-}
-
-static int key_equal(const void *ptr1, const void *ptr2)
-{
- return jsonp_str_equal(((const object_key_t *)ptr1)->key,
- ((const object_key_t *)ptr2)->key);
-}
-
-static void value_decref(void *value)
-{
- json_decref((json_t *)value);
-}
-
json_t *json_object(void)
{
json_object_t *object = jsonp_malloc(sizeof(json_object_t));
return NULL;
json_init(&object->json, JSON_OBJECT);
- if(hashtable_init(&object->hashtable,
- hash_key, key_equal,
- jsonp_free, value_decref))
+ if(hashtable_init(&object->hashtable))
{
jsonp_free(object);
return NULL;
return NULL;
object = json_to_object(json);
- return hashtable_get(&object->hashtable, string_to_key(key));
+ return hashtable_get(&object->hashtable, key);
}
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
{
json_object_t *object;
- object_key_t *k;
- if(!key || !value)
+ if(!value)
return -1;
- if(!json_is_object(json) || json == value)
+ if(!key || !json_is_object(json) || json == value)
{
json_decref(value);
return -1;
}
object = json_to_object(json);
- /* offsetof(...) returns the size of object_key_t without the
- last, flexible member. This way, the correct amount is
- allocated. */
- k = jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1);
- if(!k)
- return -1;
-
- k->serial = object->serial++;
- strcpy(k->key, key);
-
- if(hashtable_set(&object->hashtable, k, value))
+ if(hashtable_set(&object->hashtable, key, object->serial++, value))
{
json_decref(value);
return -1;
return -1;
object = json_to_object(json);
- return hashtable_del(&object->hashtable, string_to_key(key));
+ return hashtable_del(&object->hashtable, key);
}
int json_object_clear(json_t *json)
return -1;
object = json_to_object(json);
+
hashtable_clear(&object->hashtable);
+ object->serial = 0;
return 0;
}
return NULL;
object = json_to_object(json);
- return hashtable_iter_at(&object->hashtable, string_to_key(key));
+ return hashtable_iter_at(&object->hashtable, key);
}
void *json_object_iter_next(json_t *json, void *iter)
return hashtable_iter_next(&object->hashtable, iter);
}
-const object_key_t *jsonp_object_iter_fullkey(void *iter)
-{
- if(!iter)
- return NULL;
-
- return hashtable_iter_key(iter);
-}
-
const char *json_object_iter_key(void *iter)
{
if(!iter)
return NULL;
- return jsonp_object_iter_fullkey(iter)->key;
+ return hashtable_iter_key(iter);
}
json_t *json_object_iter_value(void *iter)
int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
{
- json_object_t *object;
-
if(!json_is_object(json) || !iter || !value)
return -1;
- object = json_to_object(json);
- hashtable_iter_set(&object->hashtable, iter, value);
-
+ hashtable_iter_set(iter, value);
return 0;
}
char *dup;
json_string_t *string;
+ if(!json_is_string(json) || !value)
+ return -1;
+
dup = jsonp_strdup(value);
if(!dup)
return -1;