summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@openedhand.com>2007-11-13 10:51:31 +0000
committerEmmanuele Bassi <ebassi@openedhand.com>2007-11-13 10:51:31 +0000
commit198ed839424dc7791d22dede152f4d7abc16a8b2 (patch)
tree926af169c0d0aefaf0afcf8ea6ea434cf8a725ef
parent10e937a68bd802a4f5948d900aa2201344cfc138 (diff)
downloadjson-glib-gobject-deserialize.tar.gz
Use the fallback value-to-node generator even for serializablesgobject-deserialize
To avoid reimplementing the same code all over again, if the implementation of the serialize_property virtual function of JsonSerializable returns NULL we will fall back to the simple value-to-node generator we provide for non-serializable classes.
-rw-r--r--json-glib/json-gobject.c17
-rw-r--r--tests/test-07.c13
2 files changed, 12 insertions, 18 deletions
diff --git a/json-glib/json-gobject.c b/json-glib/json-gobject.c
index c5dfe36..3357910 100644
--- a/json-glib/json-gobject.c
+++ b/json-glib/json-gobject.c
@@ -472,6 +472,8 @@ json_serialize_gobject (GObject *gobject,
gsize *length)
{
JsonSerializableIface *iface = NULL;
+ JsonSerializable *serializable = NULL;
+ gboolean serialize_property = FALSE;
JsonGenerator *gen;
JsonNode *root;
JsonObject *object;
@@ -482,7 +484,11 @@ json_serialize_gobject (GObject *gobject,
g_return_val_if_fail (G_OBJECT (gobject), NULL);
if (JSON_IS_SERIALIZABLE (gobject))
- iface = JSON_SERIALIZABLE_GET_IFACE (gobject);
+ {
+ serializable = JSON_SERIALIZABLE (gobject);
+ iface = JSON_SERIALIZABLE_GET_IFACE (gobject);
+ serialize_property = (iface->serialize_property != NULL);
+ }
object = json_object_new ();
@@ -495,7 +501,7 @@ json_serialize_gobject (GObject *gobject,
{
GParamSpec *pspec = pspecs[i];
GValue value = { 0, };
- JsonNode *node;
+ JsonNode *node = NULL;
/* read only what we can */
if (!(pspec->flags & G_PARAM_READABLE))
@@ -504,15 +510,14 @@ json_serialize_gobject (GObject *gobject,
g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
g_object_get_property (gobject, pspec->name, &value);
- if (iface && iface->serialize_property)
+ if (serialize_property)
{
- JsonSerializable *serializable = JSON_SERIALIZABLE (gobject);
-
node = iface->serialize_property (serializable, pspec->name,
&value,
pspec);
}
- else
+
+ if (!node)
node = json_serialize_pspec (&value, pspec);
if (node)
diff --git a/tests/test-07.c b/tests/test-07.c
index fe5dafa..0dda4f6 100644
--- a/tests/test-07.c
+++ b/tests/test-07.c
@@ -98,7 +98,7 @@ test_object_serialize_property (JsonSerializable *serializable,
const GValue *value,
GParamSpec *pspec)
{
- JsonNode *retval;
+ JsonNode *retval = NULL;
if (strcmp (name, "blah") == 0)
{
@@ -123,17 +123,6 @@ test_object_serialize_property (JsonSerializable *serializable,
test_boxed_free (boxed);
}
- else
- {
- GValue copy = { 0, };
-
- retval = json_node_new (JSON_NODE_VALUE);
-
- g_value_init (&copy, G_PARAM_SPEC_VALUE_TYPE (pspec));
- g_value_copy (value, &copy);
- json_node_set_value (retval, &copy);
- g_value_unset (&copy);
- }
return retval;
}