[json-glib] gobject: Use from/to data naming convention



commit 8f8ce87730fc0bf102a707e84c4f6106b215cfab
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Wed Oct 28 16:23:39 2009 +0000

    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.

 json-glib/json-gobject.c |   63 +++++++++++++++++++++++++++++++++++++++++++---
 json-glib/json-gobject.h |   10 +++++++
 2 files changed, 69 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,
diff --git a/json-glib/json-gobject.h b/json-glib/json-gobject.h
index d294702..8b6568d 100644
--- a/json-glib/json-gobject.h
+++ b/json-glib/json-gobject.h
@@ -121,12 +121,22 @@ JsonNode *json_gobject_serialize             (GObject                  *gobject)
 GObject * json_gobject_deserialize           (GType                     gtype,
                                               JsonNode                 *node);
 
+GObject * json_gobject_from_data             (GType                     gtype,
+                                              const gchar              *data,
+                                              gssize                    length,
+                                              GError                  **error);
+gchar *   json_gobject_to_data               (GObject                  *gobject,
+                                              gsize                    *length);
+
+#ifndef JSON_DISABLE_DEPRECATED
 GObject * json_construct_gobject   (GType         gtype,
                                     const gchar  *data,
                                     gsize         length,
                                     GError      **error);
 gchar *   json_serialize_gobject   (GObject      *gobject,
                                     gsize        *length) G_GNUC_MALLOC;
+#endif
+
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]