summaryrefslogtreecommitdiff
path: root/json-glib/json-gboxed.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@linux.intel.com>2009-11-23 22:20:58 +0000
committerEmmanuele Bassi <ebassi@linux.intel.com>2009-11-23 22:20:58 +0000
commit3cf919e9c7f3201305a1a63a3c270e422a37efed (patch)
tree34d263f22b1e25f6557f8ce58c231244c6e0e96f /json-glib/json-gboxed.c
parent61d54cc9e2a3098e876e700a9248428f400a5368 (diff)
downloadjson-glib-3cf919e9c7f3201305a1a63a3c270e422a37efed.tar.gz
boxed: Split (de)serialization registration
A GBoxed type defined as: struct Boxed { int foo; gboolean bar; int baz; }; Can be represented either by a JSON object: { "foo" : 1, "bar" : true, "baz" : 3 } Or by a JSON array: [ 1, true, 3 ] The current function for registering a serialization and a deserialization pair does not allow registering more than one deserialization function - which means that there can only be one way to deserialize a GBoxed type into a specific JsonNode type. To allow having more than one JsonNodeType associated to a GBoxed type and a deserialization function we need to split out the registration of the serialization and deserialization functions into two distinct functions.
Diffstat (limited to 'json-glib/json-gboxed.c')
-rw-r--r--json-glib/json-gboxed.c115
1 files changed, 75 insertions, 40 deletions
diff --git a/json-glib/json-gboxed.c b/json-glib/json-gboxed.c
index fa1f9a3..fd2ebb9 100644
--- a/json-glib/json-gboxed.c
+++ b/json-glib/json-gboxed.c
@@ -86,8 +86,11 @@ struct _BoxedTransform
JsonBoxedDeserializeFunc deserialize;
};
-G_LOCK_DEFINE_STATIC (boxed_transforms);
-static GSList *boxed_transforms = NULL;
+G_LOCK_DEFINE_STATIC (boxed_serialize);
+static GSList *boxed_serialize = NULL;
+
+G_LOCK_DEFINE_STATIC (boxed_deserialize);
+static GSList *boxed_deserialize = NULL;
static gint
boxed_transforms_cmp (gconstpointer a,
@@ -114,8 +117,9 @@ boxed_transforms_find (gconstpointer a,
}
static BoxedTransform *
-lookup_boxed_transform (GType gboxed_type,
- JsonNodeType node_type)
+lookup_boxed_transform (GSList *transforms,
+ GType gboxed_type,
+ JsonNodeType node_type)
{
BoxedTransform lookup;
GSList *t;
@@ -123,7 +127,7 @@ lookup_boxed_transform (GType gboxed_type,
lookup.boxed_type = gboxed_type;
lookup.node_type = node_type;
- t = g_slist_find_custom (boxed_transforms, &lookup, boxed_transforms_find);
+ t = g_slist_find_custom (transforms, &lookup, boxed_transforms_find);
if (t == NULL)
return NULL;
@@ -131,60 +135,93 @@ lookup_boxed_transform (GType gboxed_type,
}
/**
- * json_boxed_register_transform_func:
+ * json_boxed_register_serialize_func:
* @gboxed_type: a boxed type
* @node_type: a node type
- * @serialize_func: (allow-none): serialization function for @boxed_type
- * into a #JsonNode of type @node_type; can be %NULL if @deserialize_func
- * is not %NULL
- * @deserialize_func: (allow-none): deserialization function for @boxed_type
- * from a #JsonNode of type @node_type; can be %NULL if @serialize_func
- * is not %NULL
+ * @serialize_func: serialization function for @boxed_type into
+ * a #JsonNode of type @node_type
*
- * Registers a serialization and deserialization functions for a #GBoxed
- * of type @gboxed_type to and from a #JsonNode of type @node_type
+ * Registers a serialization function for a #GBoxed of type @gboxed_type
+ * to a #JsonNode of type @node_type
*
* Since: 0.10
*/
void
-json_boxed_register_transform_func (GType gboxed_type,
- JsonNodeType node_type,
- JsonBoxedSerializeFunc serialize_func,
- JsonBoxedDeserializeFunc deserialize_func)
+json_boxed_register_serialize_func (GType gboxed_type,
+ JsonNodeType node_type,
+ JsonBoxedSerializeFunc serialize_func)
{
BoxedTransform *t;
g_return_if_fail (G_TYPE_IS_BOXED (gboxed_type));
g_return_if_fail (G_TYPE_IS_ABSTRACT (gboxed_type) == FALSE);
- if (serialize_func == NULL)
- g_return_if_fail (deserialize_func != NULL);
+ G_LOCK (boxed_serialize);
+
+ t = lookup_boxed_transform (boxed_serialize, gboxed_type, node_type);
+ if (t == NULL)
+ {
+ t = g_slice_new (BoxedTransform);
+
+ t->boxed_type = gboxed_type;
+ t->node_type = node_type;
+ t->serialize = serialize_func;
+
+ boxed_serialize = g_slist_insert_sorted (boxed_serialize, t,
+ boxed_transforms_cmp);
+ }
+ else
+ g_warning ("A serialization function for the boxed type %s into "
+ "JSON nodes of type %s already exists",
+ g_type_name (gboxed_type),
+ json_node_type_get_name (node_type));
+
+ G_UNLOCK (boxed_serialize);
+}
+
+/**
+ * json_boxed_register_deserialize_func:
+ * @gboxed_type: a boxed type
+ * @node_type: a node type
+ * @deserialize_func: deserialization function for @boxed_type from
+ * a #JsonNode of type @node_type
+ *
+ * Registers a deserialization function for a #GBoxed of type @gboxed_type
+ * from a #JsonNode of type @node_type
+ *
+ * Since: 0.10
+ */
+void
+json_boxed_register_deserialize_func (GType gboxed_type,
+ JsonNodeType node_type,
+ JsonBoxedDeserializeFunc deserialize_func)
+{
+ BoxedTransform *t;
- if (deserialize_func == NULL)
- g_return_if_fail (serialize_func != NULL);
+ g_return_if_fail (G_TYPE_IS_BOXED (gboxed_type));
+ g_return_if_fail (G_TYPE_IS_ABSTRACT (gboxed_type) == FALSE);
- G_LOCK (boxed_transforms);
+ G_LOCK (boxed_deserialize);
- t = lookup_boxed_transform (gboxed_type, node_type);
+ t = lookup_boxed_transform (boxed_deserialize, gboxed_type, node_type);
if (t == NULL)
{
t = g_slice_new (BoxedTransform);
t->boxed_type = gboxed_type;
t->node_type = node_type;
- t->serialize = serialize_func;
t->deserialize = deserialize_func;
- boxed_transforms = g_slist_insert_sorted (boxed_transforms, t,
- boxed_transforms_cmp);
+ boxed_deserialize = g_slist_insert_sorted (boxed_deserialize, t,
+ boxed_transforms_cmp);
}
else
- g_warning ("A transformation for the boxed type %s into "
+ g_warning ("A deserialization function for the boxed type %s from "
"JSON nodes of type %s already exists",
g_type_name (gboxed_type),
json_node_type_get_name (node_type));
- G_UNLOCK (boxed_transforms);
+ G_UNLOCK (boxed_deserialize);
}
/**
@@ -212,8 +249,8 @@ json_boxed_can_serialize (GType gboxed_type,
g_return_val_if_fail (G_TYPE_IS_BOXED (gboxed_type), FALSE);
g_return_val_if_fail (G_TYPE_IS_ABSTRACT (gboxed_type) == FALSE, FALSE);
- t = lookup_boxed_transform (gboxed_type, -1);
- if (t != NULL && t->serialize != NULL)
+ t = lookup_boxed_transform (boxed_serialize, gboxed_type, -1);
+ if (t != NULL)
{
if (node_type)
*node_type = t->node_type;
@@ -245,8 +282,8 @@ json_boxed_can_deserialize (GType gboxed_type,
g_return_val_if_fail (G_TYPE_IS_BOXED (gboxed_type), FALSE);
g_return_val_if_fail (G_TYPE_IS_ABSTRACT (gboxed_type) == FALSE, FALSE);
- t = lookup_boxed_transform (gboxed_type, node_type);
- if (t != NULL && t->deserialize != NULL)
+ t = lookup_boxed_transform (boxed_deserialize, gboxed_type, node_type);
+ if (t != NULL)
return TRUE;
return FALSE;
@@ -255,11 +292,10 @@ json_boxed_can_deserialize (GType gboxed_type,
/**
* json_boxed_serialize:
* @gboxed_type: a boxed type
- * @node_type: a #JsonNode type
* @boxed: a pointer to a #GBoxed of type @gboxed_type
*
* Serializes @boxed, a pointer to a #GBoxed of type @gboxed_type,
- * into a #JsonNode of type @node_type
+ * into a #JsonNode
*
* Return value: a #JsonNode with the serialization of the boxed
* type, or %NULL if serialization either failed or was not
@@ -268,9 +304,8 @@ json_boxed_can_deserialize (GType gboxed_type,
* Since: 0.10
*/
JsonNode *
-json_boxed_serialize (GType gboxed_type,
- JsonNodeType node_type,
- gconstpointer boxed)
+json_boxed_serialize (GType gboxed_type,
+ gconstpointer boxed)
{
BoxedTransform *t;
@@ -278,7 +313,7 @@ json_boxed_serialize (GType gboxed_type,
g_return_val_if_fail (G_TYPE_IS_ABSTRACT (gboxed_type) == FALSE, NULL);
g_return_val_if_fail (boxed != NULL, NULL);
- t = lookup_boxed_transform (gboxed_type, node_type);
+ t = lookup_boxed_transform (boxed_serialize, gboxed_type, -1);
if (t != NULL && t->serialize != NULL)
return t->serialize (boxed);
@@ -310,7 +345,7 @@ json_boxed_deserialize (GType gboxed_type,
node_type = json_node_get_node_type (node);
- t = lookup_boxed_transform (gboxed_type, node_type);
+ t = lookup_boxed_transform (boxed_deserialize, gboxed_type, node_type);
if (t != NULL && t->deserialize != NULL)
return t->deserialize (node);