summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@openedhand.com>2007-12-25 11:53:04 +0000
committerEmmanuele Bassi <ebassi@openedhand.com>2007-12-25 11:53:04 +0000
commit8ec0c93715f6f43e4829a6f3ac534f9ec05e0363 (patch)
treea8e5bc5ebb57b8f44c69bc7382cd7e25ecc6b7b0
parent5bb6dd7b77dcd0d1996f484990223287d2144ff3 (diff)
downloadjson-glib-8ec0c93715f6f43e4829a6f3ac534f9ec05e0363.tar.gz
Provide a default implementation of JsonSerializable
The JsonSerializable interface can provide a default implementation, using the powers of GTypeInterface. This means that classes implementing the interface can opt to implement both, either or none of the JsonSerializable methods, and still be able to retain some basic functionality for the methods they decide not to implement.
-rw-r--r--json-glib/json-gobject.c208
1 files changed, 120 insertions, 88 deletions
diff --git a/json-glib/json-gobject.c b/json-glib/json-gobject.c
index 28d3dce..548a9ce 100644
--- a/json-glib/json-gobject.c
+++ b/json-glib/json-gobject.c
@@ -41,94 +41,6 @@
#include "json-parser.h"
#include "json-generator.h"
-GType
-json_serializable_get_type (void)
-{
- static GType iface_type = 0;
-
- if (!iface_type)
- iface_type =
- g_type_register_static_simple (G_TYPE_INTERFACE, "JsonSerializable",
- sizeof (JsonSerializableIface),
- NULL, 0, NULL, 0);
-
- return iface_type;
-}
-
-/**
- * json_serializable_serialize_property:
- * @serializable: a #JsonSerializable object
- * @property_name: the name of the property
- * @value: the value of the property
- * @pspec: a #GParamSpec
- *
- * Asks a #JsonSerializable implementation to serialize a #GObject
- * property into a #JsonNode object.
- *
- * Return value: a #JsonNode containing the serialize property
- */
-JsonNode *
-json_serializable_serialize_property (JsonSerializable *serializable,
- const gchar *property_name,
- const GValue *value,
- GParamSpec *pspec)
-{
- JsonSerializableIface *iface;
-
- g_return_val_if_fail (JSON_IS_SERIALIZABLE (serializable), NULL);
- g_return_val_if_fail (property_name != NULL, NULL);
- g_return_val_if_fail (value != NULL, NULL);
- g_return_val_if_fail (pspec != NULL, NULL);
-
- iface = JSON_SERIALIZABLE_GET_IFACE (serializable);
- if (!iface->serialize_property)
- return json_node_new (JSON_NODE_NULL);
-
- return iface->serialize_property (serializable, property_name, value, pspec);
-}
-
-/**
- * json_serializable_deserialize_property:
- * @serializable: a #JsonSerializable
- * @property_name: the name of the property
- * @value: a pointer to an uninitialized #GValue
- * @pspec: a #GParamSpec
- * @property_node: a #JsonNode containing the serialized property
- *
- * Asks a #JsonSerializable implementation to deserialize the
- * property contained inside @property_node into @value.
- *
- * Return value: %TRUE if the property was successfully deserialized.
- */
-gboolean
-json_serializable_deserialize_property (JsonSerializable *serializable,
- const gchar *property_name,
- GValue *value,
- GParamSpec *pspec,
- JsonNode *property_node)
-{
- JsonSerializableIface *iface;
-
- g_return_val_if_fail (JSON_IS_SERIALIZABLE (serializable), FALSE);
- g_return_val_if_fail (property_name != NULL, FALSE);
- g_return_val_if_fail (value != NULL, FALSE);
- g_return_val_if_fail (pspec != NULL, FALSE);
- g_return_val_if_fail (property_node != NULL, FALSE);
-
- iface = JSON_SERIALIZABLE_GET_IFACE (serializable);
- if (!iface->deserialize_property)
- {
- g_param_value_defaults (pspec, value);
- return TRUE;
- }
-
- return iface->deserialize_property (serializable,
- property_name,
- value,
- pspec,
- property_node);
-}
-
static gboolean
enum_from_string (GType type,
const gchar *string,
@@ -375,6 +287,8 @@ json_deserialize_pspec (GValue *value,
retval = FALSE;
break;
}
+
+ g_value_unset (&node_value);
break;
case JSON_NODE_NULL:
@@ -493,6 +407,124 @@ json_serialize_pspec (const GValue *real_value,
}
/**
+ * json_serializable_serialize_property:
+ * @serializable: a #JsonSerializable object
+ * @property_name: the name of the property
+ * @value: the value of the property
+ * @pspec: a #GParamSpec
+ *
+ * Asks a #JsonSerializable implementation to serialize a #GObject
+ * property into a #JsonNode object.
+ *
+ * Return value: a #JsonNode containing the serialize property
+ */
+JsonNode *
+json_serializable_serialize_property (JsonSerializable *serializable,
+ const gchar *property_name,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ JsonSerializableIface *iface;
+
+ g_return_val_if_fail (JSON_IS_SERIALIZABLE (serializable), NULL);
+ g_return_val_if_fail (property_name != NULL, NULL);
+ g_return_val_if_fail (value != NULL, NULL);
+ g_return_val_if_fail (pspec != NULL, NULL);
+
+ iface = JSON_SERIALIZABLE_GET_IFACE (serializable);
+
+ return iface->serialize_property (serializable, property_name, value, pspec);
+}
+
+/**
+ * json_serializable_deserialize_property:
+ * @serializable: a #JsonSerializable
+ * @property_name: the name of the property
+ * @value: a pointer to an uninitialized #GValue
+ * @pspec: a #GParamSpec
+ * @property_node: a #JsonNode containing the serialized property
+ *
+ * Asks a #JsonSerializable implementation to deserialize the
+ * property contained inside @property_node into @value.
+ *
+ * Return value: %TRUE if the property was successfully deserialized.
+ */
+gboolean
+json_serializable_deserialize_property (JsonSerializable *serializable,
+ const gchar *property_name,
+ GValue *value,
+ GParamSpec *pspec,
+ JsonNode *property_node)
+{
+ JsonSerializableIface *iface;
+
+ g_return_val_if_fail (JSON_IS_SERIALIZABLE (serializable), FALSE);
+ g_return_val_if_fail (property_name != NULL, FALSE);
+ g_return_val_if_fail (value != NULL, FALSE);
+ g_return_val_if_fail (pspec != NULL, FALSE);
+ g_return_val_if_fail (property_node != NULL, FALSE);
+
+ iface = JSON_SERIALIZABLE_GET_IFACE (serializable);
+
+ return iface->deserialize_property (serializable,
+ property_name,
+ value,
+ pspec,
+ property_node);
+}
+
+static gboolean
+json_serializable_real_deserialize (JsonSerializable *serializable,
+ const gchar *name,
+ GValue *value,
+ GParamSpec *pspec,
+ JsonNode *node)
+{
+ return json_deserialize_pspec (value, pspec, node);
+}
+
+static JsonNode *
+json_serializable_real_serialize (JsonSerializable *serializable,
+ const gchar *name,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ return json_serialize_pspec (value, pspec);
+}
+
+static void
+json_serializable_base_init (gpointer g_class,
+ gpointer data)
+{
+ static gboolean is_initialized = FALSE;
+
+ if (G_UNLIKELY (!is_initialized))
+ {
+ JsonSerializableIface *iface = g_class;
+
+ iface->serialize_property = json_serializable_real_serialize;
+ iface->deserialize_property = json_serializable_real_deserialize;
+
+ is_initialized = TRUE;
+ }
+}
+
+GType
+json_serializable_get_type (void)
+{
+ static GType iface_type = 0;
+
+ if (!iface_type)
+ iface_type =
+ g_type_register_static_simple (G_TYPE_INTERFACE, "JsonSerializable",
+ sizeof (JsonSerializableIface),
+ json_serializable_base_init,
+ 0, NULL, 0);
+
+ return iface_type;
+}
+
+/**
* json_construct_gobject:
* @gtype: the #GType of object to construct
* @data: a JSON data stream