From 8f8ce87730fc0bf102a707e84c4f6106b215cfab Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Wed, 28 Oct 2009 16:23:39 +0000 Subject: gobject: Use from/to data naming convention MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Be more GLib-like, and use __from_data() __to_data() Instead of the homebrew "construct" and "serialize", when dealing with string buffers. This means: • adding json_gobject_from_data() to deprecate json_construct_gobject() • adding json_gobject_to_data() to deprecate json_serialize_gobject() The json_construct_gobject() function also contains a mistake: it uses gsize with the special value of -1 meaning "slurp the whole string", but gsize is an unsigned type. The newly added json_gobject_from_data() correctly uses gssize instead. --- json-glib/json-gobject.c | 63 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'json-glib/json-gobject.c') diff --git a/json-glib/json-gobject.c b/json-glib/json-gobject.c index 53ee8fb..a9ecfe7 100644 --- a/json-glib/json-gobject.c +++ b/json-glib/json-gobject.c @@ -750,7 +750,7 @@ json_gobject_serialize (GObject *gobject) * json_construct_gobject: * @gtype: the #GType of object to construct * @data: a JSON data stream - * @length: length of the data stream, or -1 if it is NUL-terminated + * @length: length of the data stream * @error: return location for a #GError, or %NULL * * Deserializes a JSON data stream and creates the corresponding @@ -764,12 +764,42 @@ json_gobject_serialize (GObject *gobject) * Return value: a #GObject or %NULL * * Since: 0.4 + * + * Deprecated: 0.10: Use json_gobject_from_data() instead */ GObject * json_construct_gobject (GType gtype, const gchar *data, gsize length, GError **error) +{ + return json_gobject_from_data (gtype, data, strlen (data), error); +} + +/** + * json_gobject_from_data: + * @gtype: the #GType of object to construct + * @data: a JSON data stream + * @length: length of the data stream, or -1 if it is NUL-terminated + * @error: return location for a #GError, or %NULL + * + * Deserializes a JSON data stream and creates the corresponding + * #GObject class. If @gtype implements the #JsonSerializableIface + * interface, it will be asked to deserialize all the JSON members + * into the respective properties; otherwise, the default implementation + * will be used to translate the compatible JSON native types. + * + * Note: the JSON data stream must be an object declaration. + * + * Return value: a #GObject or %NULL + * + * Since: 0.10 + */ +GObject * +json_gobject_from_data (GType gtype, + const gchar *data, + gssize length, + GError **error) { JsonParser *parser; JsonNode *root; @@ -805,7 +835,7 @@ json_construct_gobject (GType gtype, return NULL; } - retval = json_gobject_new (gtype, json_node_get_object (root)); + retval = json_gobject_deserialize (gtype, root); g_object_unref (parser); @@ -823,10 +853,36 @@ json_construct_gobject (GType gtype, * translate the compatible types into JSON native types. * * Return value: a JSON data stream representing the passed #GObject + * + * Deprecated: 0.10: Use json_gobject_to_data() instead */ gchar * json_serialize_gobject (GObject *gobject, gsize *length) +{ + return json_gobject_to_data (gobject, length); +} + +/** + * json_serialize_gobject: + * @gobject: a #GObject + * @length: (out): return value for the length of the buffer, or %NULL + * + * Serializes a #GObject into a JSON data stream, iterating recursively + * over each property. + * + * If @gobject implements the #JsonSerializableIface interface, it will + * be asked to serialize all its properties; otherwise, the default + * implementation will be use to translate the compatible types into + * JSON native types. + * + * Return value: a JSON data stream representing the passed #GObject + * + * Since: 0.10 + */ +gchar * +json_gobject_to_data (GObject *gobject, + gsize *length) { JsonGenerator *gen; JsonNode *root; @@ -834,8 +890,7 @@ json_serialize_gobject (GObject *gobject, g_return_val_if_fail (G_OBJECT (gobject), NULL); - root = json_node_new (JSON_NODE_OBJECT); - json_node_take_object (root, json_gobject_dump (gobject)); + root = json_gobject_serialize (gobject); gen = g_object_new (JSON_TYPE_GENERATOR, "root", root, -- cgit v1.2.1