diff options
author | Emmanuele Bassi <ebassi@openedhand.com> | 2007-10-01 12:57:46 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@openedhand.com> | 2007-10-01 12:57:46 +0100 |
commit | 88b11be5eec1da769328e93189bc2f3316c9cd0a (patch) | |
tree | 5ed59cecd38b1a51a8bbcf9964948d0e7e1fbc4b /json-glib/json-object.c | |
parent | c11ebd32f73a1e21d6097bf9eba8e12f7e35497a (diff) | |
download | json-glib-88b11be5eec1da769328e93189bc2f3316c9cd0a.tar.gz |
Add JsonNode, a generic container for JSON types
This huge commit removes JsonData and adds JsonNode, the generic container
for fundamental and complex data types extracted from a JSON stream. The
contents of a JsonNode can be extracted from it in form of a GValue for
fundamental types (integers, floats, strings, booleans) or in form of
JsonObject and JsonArray objects. JsonObject and JsonArray now accept
JsonNodes instead of GValues.
The JsonParser object builds the data model tree when parsing a JSON stream;
the tree can be recursed by getting the root node and walking it using the
GValue API for the fundamental types and the objects/arrays API for complex
types.
The API has been updated and the tests now recurse through the generated
data model tree.
Diffstat (limited to 'json-glib/json-object.c')
-rw-r--r-- | json-glib/json-object.c | 44 |
1 files changed, 12 insertions, 32 deletions
diff --git a/json-glib/json-object.c b/json-glib/json-object.c index aa4d517..0aa7855 100644 --- a/json-glib/json-object.c +++ b/json-glib/json-object.c @@ -7,7 +7,7 @@ /** * SECTION:json-object - * @short_description: a JSON Object type + * @short_description: a JSON object representation * * #JsonObject is a boxed type representing a JSON object data type. Each * JSON object can have zero or more members, and each member is accessed @@ -19,7 +19,7 @@ struct _JsonObject { GHashTable *members; - volatile guint ref_count; + volatile gint ref_count; }; JsonObject * @@ -32,7 +32,7 @@ json_object_new (void) object->ref_count = 1; object->members = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, - g_free); + (GDestroyNotify) json_node_free); return object; } @@ -57,21 +57,6 @@ json_object_ref (JsonObject *object) return object; } -static void -object_unset_members (gpointer key, - gpointer value, - gpointer user_data) -{ - GValue *gvalue = value; - - /* we copy the contents of the GValues, so we need to unset it - * before actually freeing them along with the hash table - */ - - if (G_VALUE_TYPE (gvalue) != 0) /* we allow unset values */ - g_value_unset (gvalue); -} - /** * json_object_unref: * @object: a #JsonObject @@ -93,7 +78,6 @@ json_object_unref (JsonObject *object) g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1); else { - g_hash_table_foreach (object->members, object_unset_members, NULL); g_hash_table_destroy (object->members); object->members = NULL; @@ -102,27 +86,23 @@ json_object_unref (JsonObject *object) } void -json_object_add_member (JsonObject *object, - const gchar *member_name, - const GValue *value) +json_object_add_member (JsonObject *object, + const gchar *member_name, + JsonNode *node) { - GValue *copy; - g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); - g_return_if_fail (value != NULL); + g_return_if_fail (node != NULL); if (json_object_has_member (object, member_name)) { - g_warning ("JsonObject already has a `%s' member", member_name); + g_warning ("JsonObject already has a `%s' member of type `%s'", + member_name, + json_node_type_name (node)); return; } - copy = g_new (GValue, 1); - g_value_init (copy, G_VALUE_TYPE (value)); - g_value_copy (value, copy); - - g_hash_table_replace (object->members, g_strdup (member_name), copy); + g_hash_table_replace (object->members, g_strdup (member_name), node); } /** @@ -155,7 +135,7 @@ json_object_get_members (JsonObject *object) * Return value: a pointer to the value for the requested object * member, or %NULL */ -GValue * +JsonNode * json_object_get_member (JsonObject *object, const gchar *member_name) { |