diff options
author | Emmanuele Bassi <ebassi@linux.intel.com> | 2009-10-28 16:23:39 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@linux.intel.com> | 2009-10-28 16:23:39 +0000 |
commit | 8f8ce87730fc0bf102a707e84c4f6106b215cfab (patch) | |
tree | 974c53123f5b9873e85c976a98185643120f2563 /json-glib/json-gobject.c | |
parent | 00b4d200849e232cd904d23d3593d6f95252b483 (diff) | |
download | json-glib-8f8ce87730fc0bf102a707e84c4f6106b215cfab.tar.gz |
gobject: Use from/to data naming convention
Be more GLib-like, and use
<namespace>_<type>_from_data()
<namespace>_<type>_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.
Diffstat (limited to 'json-glib/json-gobject.c')
-rw-r--r-- | json-glib/json-gobject.c | 63 |
1 files changed, 59 insertions, 4 deletions
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,6 +764,8 @@ 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, @@ -771,6 +773,34 @@ json_construct_gobject (GType gtype, 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; GError *parse_error; @@ -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,19 +853,44 @@ 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; gchar *data; 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, |