[gxml/serialization: 2/10] Fixing recursive XmlObjectModel serialization on Serializable properties



commit 13ddd94dec7994bf5839723334fc9f3578e39eca
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 22 16:33:12 2013 -0500

    Fixing recursive XmlObjectModel serialization on Serializable properties

 gxml/DomNode.vala           |    3 +-
 gxml/Serializable.vala      |   31 ++++++++++++++++++++++-------
 gxml/Serialization.vala     |   45 +++++++++++++++++++++++-------------------
 test/SerializationTest.vala |   17 +++++++++++++--
 4 files changed, 64 insertions(+), 32 deletions(-)
---
diff --git a/gxml/DomNode.vala b/gxml/DomNode.vala
index 7afae88..fe20154 100644
--- a/gxml/DomNode.vala
+++ b/gxml/DomNode.vala
@@ -297,7 +297,7 @@ namespace GXml {
                        return null;
                }
 
-               private string _str;
+               private string _str = "DomNode";
                /**
                 * Provides a string representation of this node.
                 *
@@ -310,6 +310,7 @@ namespace GXml {
                // TODO: indicate in C that the return value must be freed.
                // TODO: ask Colin Walters about storing docs in GIR files (might have not been him)
                public virtual string to_string (bool format = false, int level = 0) {
+                       GLib.message ("At DomNode.to_string()");
                        _str = "DomNode(%d:%s)".printf (this.node_type, this.node_name);
                        return _str;
                }
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index cca867d..965a625 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -106,13 +106,23 @@ namespace GXml {
                 *
                 * @doc an GXml.Document object to serialise to 
                 */
-               public virtual DomNode? serialize (Document doc) throws DomError
+               public virtual DomNode? serialize (DomNode node) throws DomError
                {
+                       Document doc;
+                       if (node is Document)
+                               doc = (Document) node;
+                       else
+                               doc = node.owner_document;
+
                        serialized_xml_node = doc.create_element (this.get_type ().name ());
                        foreach (ParamSpec spec in list_serializable_properties ()) {
-                               serialize_property (spec, doc);
+                               GLib.message ("Serializing: " + spec.name + 
+                                             " on node: " + serialized_xml_node.node_name);
+                               serialize_property (spec);
+                               GLib.message ("Done");
                        }
                        serialized_xml_node.node_value = serialized_xml_node_value;
+                       node.append_child (serialized_xml_node);
                        return serialized_xml_node;
                }
                /**
@@ -198,19 +208,22 @@ namespace GXml {
                 * @param doc the { link GXml.Document} the returned { link GXml.DomNode} should belong to
                 * @return a new { link GXml.DomNode}, or `null`
                 */
-               public virtual GXml.DomNode? serialize_property (GLib.ParamSpec spec,
-                                                                GXml.Document doc)
+               public virtual GXml.DomNode? serialize_property (GLib.ParamSpec spec)
                                                                 throws DomError
                {
+                       Document doc = serialized_xml_node.owner_document;
                        var prop = find_property_spec (spec.name);
-                       if (prop == null)
+                       if (prop == null) {
+                               GLib.warning ("No such property: " + spec.name);
                                return null;
-                       if (prop.value_type == typeof (Serializable)) {
+                       }
+                       if (prop.value_type.is_a (typeof (Serializable))) {
+                       GLib.message ("Is a Serializable Property");
                                var v = Value (typeof (Object));
                                get_property (spec.name, ref v);
                                var obj = (Serializable) v.get_object ();
-                               var node = obj.serialize (doc);
-                               serialized_xml_node.append_child (node);
+                               var node = obj.serialize (serialized_xml_node);
+                               return node;
                        }
                        Value oval = Value (spec.value_type);
                        get_property (spec.name, ref oval);
@@ -282,6 +295,8 @@ namespace GXml {
                                                                                                              
                                                                  get_class 
().find_property("serialized-xml-node"));
                                ignored_serializable_properties.set ("serialized-xml-node-value",
                                                                                                              
                                                                  get_class 
().find_property("serialized-xml-node-value"));
+                               ignored_serializable_properties.set ("serializable-property-use-blurb",
+                                                                                                             
                                                                  get_class 
().find_property("serializable-property-use-blurb"));
                        }
                        if (unknown_serializable_property == null) {
                                unknown_serializable_property = new HashTable<string,GXml.DomNode> (str_hash, 
str_equal);
diff --git a/gxml/Serialization.vala b/gxml/Serialization.vala
index 2485330..0bfd732 100644
--- a/gxml/Serialization.vala
+++ b/gxml/Serialization.vala
@@ -88,7 +88,13 @@ namespace GXml {
                 * { link GLib.Value} can transform into a string, and
                 * operates recursively.
                 */
-               private static GXml.DomNode serialize_property (GLib.Object object, ParamSpec prop_spec, 
GXml.Document doc) throws SerializationError, DomError {
+               private static GXml.DomNode serialize_property (GLib.Object object,
+                                                               ParamSpec prop_spec,
+                                                               GXml.Document doc)
+                                                               throws Error, 
+                                                                      SerializationError,
+                                                                      DomError
+               {
                        Type type;
                        Value value;
                        DomNode value_node;
@@ -204,12 +210,21 @@ namespace GXml {
                 * @param object A { link GLib.Object} to serialize
                 * @return a { link GXml.DomNode} representing the serialized `object`
                 */
-               public static GXml.DomNode serialize_object (GLib.Object object) throws SerializationError {
+               public static GXml.DomNode serialize_object (GLib.Object object) 
+                                                            throws Error,
+                                                                   SerializationError,
+                                                                   DomError
+               {
                        Document doc;
+                       if (object is Serializable)
+                       {
+                               doc = new Document ();
+                               return ((Serializable) object).serialize (doc);
+                       }
+
                        Element root;
                        ParamSpec[] prop_specs;
                        Element prop;
-                       Serializable serializable = null;
                        DomNode value_prop = null;
                        string oid;
 
@@ -229,10 +244,6 @@ namespace GXml {
                                        return doc.document_element;
                                }
 
-                               if (object.get_type ().is_a (typeof (Serializable))) {
-                                       serializable = (Serializable)object;
-                               }
-
                                /* Create an XML Document to return the object
                                   in.  TODO: consider just returning an
                                   <Object> node; but then we'd probably want
@@ -250,11 +261,7 @@ namespace GXml {
                                /* TODO: make sure we don't use an out param for our returned list
                                   size in our interface's list_properties (), using
                                   [CCode (array_length_type = "guint")] */
-                               if (serializable != null) {
-                                       prop_specs = serializable.list_serializable_properties ();
-                               } else {
-                                       prop_specs = object.get_class ().list_properties ();
-                               }
+                               prop_specs = object.get_class ().list_properties ();
 
                                /* Exam the properties of the object and store
                                   them with their name, type and value in XML
@@ -266,13 +273,7 @@ namespace GXml {
                                        prop.set_attribute ("ptype", prop_spec.value_type.name ());
                                        prop.set_attribute ("pname", prop_spec.name);
 
-                                       value_prop = null;
-                                       if (serializable != null) {
-                                               value_prop = serializable.serialize_property (prop_spec, doc);
-                                       }
-                                       if (value_prop == null) {
-                                               value_prop = Serialization.serialize_property (object, 
prop_spec, doc);
-                                       }
+                                       value_prop = Serialization.serialize_property (object, prop_spec, 
doc);
 
                                        prop.append_child (value_prop);
                                        root.append_child (prop);
@@ -297,7 +298,11 @@ namespace GXml {
                 * strings back to other types, we use our own function to do
                 * that.
                 */
-               private static void deserialize_property (ParamSpec spec, Element prop_elem, out Value val) 
throws SerializationError {
+               private static void deserialize_property (ParamSpec spec,
+                                                         Element prop_elem,
+                                                         out Value val)
+                                                         throws SerializationError
+               {
                        Type type;
 
                        type = spec.value_type;
diff --git a/test/SerializationTest.vala b/test/SerializationTest.vala
index f96950a..dddcdbc 100644
--- a/test/SerializationTest.vala
+++ b/test/SerializationTest.vala
@@ -308,7 +308,12 @@ class SerializationTest : GXmlTest {
 
                                try {
                                        fruit_xml = Serialization.serialize_object (fruit);
-
+                                       GLib.message (fruit_xml.to_string ());
+                                       if (fruit_xml == null) {
+                                               GLib.message ("Object not serialized!");
+                                               assert_not_reached ();
+                                       }
+                                       GLib.message ("Fruit: " + fruit_xml.to_string ());
                                        regex = new Regex (expectation);
                                        if (! regex.match (fruit_xml.to_string ())) {
                                                Test.message ("Expected [%s] but found [%s]",
@@ -316,11 +321,14 @@ class SerializationTest : GXmlTest {
                                                assert_not_reached ();
                                        }
                                } catch (RegexError e) {
-                                       Test.message ("Regular expression [%s] for test failed: %s",
+                                       GLib.message ("Regular expression [%s] for test failed: %s",
                                                      expectation, e.message);
                                        assert_not_reached ();
                                } catch (GXml.SerializationError e) {
-                                       Test.message ("%s", e.message);
+                                       GLib.message ("%s", e.message);
+                                       assert_not_reached ();
+                               } catch (Error e) {
+                                       GLib.message (e.message);
                                        assert_not_reached ();
                                }
 
@@ -577,7 +585,10 @@ class SerializationTest : GXmlTest {
                                        } catch (GXml.SerializationError e) {
                                                Test.message ("%s", e.message);
                                                assert_not_reached ();
+                                       } catch (Error e) {
+                                               Test.message (e.message);
                                        }
+                                       
                                });
                        Test.add_func ("/gxml/serialization/xml_deserialize_fields", () => {
                                        /* TODO: expecting this one to fail right now,


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