[gxml/gsoc2013: 136/150] Serialization.vala: API BREAK: change serialize_object and deserialize_object to work with actual GX



commit 5e5ece1d1ab727408cad940626895320754d0a3e
Author: Richard Schwarting <aquarichy gmail com>
Date:   Tue Aug 6 16:08:12 2013 +0200

    Serialization.vala: API BREAK: change serialize_object and deserialize_object to work with actual 
GXmlDocuments, not just their root nodes.  This is necessary because a GXmlNode is not valid without a 
reference to its GXmlDocument.

 gxml/Serialization.vala |   69 ++++++++++++++++++++++++----------------------
 1 files changed, 36 insertions(+), 33 deletions(-)
---
diff --git a/gxml/Serialization.vala b/gxml/Serialization.vala
index 651b648..a676ee9 100644
--- a/gxml/Serialization.vala
+++ b/gxml/Serialization.vala
@@ -168,8 +168,9 @@ namespace GXml {
                                           on the interface 'GeeBidirSortedSet' */
                                }
                                child_object = value.get_object ();
-                               value_node = Serialization.serialize_object (child_object); // catch 
serialisation errors?
-                               // TODO: caller will append_child; can we cross documents like this?  
Probably not :D want to be able to steal?, attributes seem to get lost
+                               Document value_doc = Serialization.serialize_object (child_object); // catch 
serialisation errors?
+
+                               value_node = doc.copy_node (value_doc.document_element);
                        } else if (type.name () == "gpointer") {
                                GLib.warning ("DEBUG: skipping gpointer with name '%s' of object '%s'", 
prop_spec.name, object.get_type ().name ());
                                value_node = doc.create_text_node (prop_spec.name);
@@ -180,16 +181,15 @@ namespace GXml {
                        return value_node;
                }
 
-               private static List<Document> _docs;
-
                /**
-                * Serializes a { link GLib.Object} into a { link GXml.Node}.
+                * Serializes a { link GLib.Object} into a { link GXml.Document}.
                 *
-                * This takes a { link GLib.Object} and serializes it into a
-                * { link GXml.Node} which can be saved to disk or
-                * transferred over a network.  It handles serialization of
-                * primitive properties and some more complex ones like enums,
-                * other { link GLib.Object}s recursively, and some collections.
+                * This takes a { link GLib.Object} and serializes it
+                * into a { link GXml.Document} which can be saved to
+                * disk or transferred over a network.  It handles
+                * serialization of primitive properties and some more
+                * complex ones like enums, other { link GLib.Object}s
+                * recursively, and some collections.
                 *
                 * The serialization process can be customised for an object
                 * by having the object implement the { link GXml.Serializable}
@@ -203,9 +203,9 @@ namespace GXml {
                 * unsupported, or the property isn't known to the object).
                 *
                 * @param object A { link GLib.Object} to serialize
-                * @return a { link GXml.Node} representing the serialized `object`
+                * @return a { link GXml.Document} representing the serialized `object`
                 */
-               public static GXml.Node serialize_object (GLib.Object object) throws SerializationError {
+               public static GXml.Document serialize_object (GLib.Object object) throws SerializationError {
                        Document doc;
                        Element root;
                        ParamSpec[] prop_specs;
@@ -219,16 +219,17 @@ namespace GXml {
                        Serialization.init_caches ();
 
                        try {
+                               doc = new Document ();
+
                                // first, check if its been serialised already, and if so, just return an 
ObjectRef element for it.
                                if (oid != "" && Serialization.serialize_cache.contains (oid)) {
                                        // GLib.message ("cache hit on oid %s", oid);
-                                       doc = new Document ();
-                                       Serialization._docs.append (doc);
+with actual GXmlDocuments, not just their root nodes.  This is necessary because a GXmlNode is not valid 
without a reference to its GXmlDocument.
                                        root = doc.create_element ("ObjectRef");
                                        doc.append_child (root);
                                        root.set_attribute ("otype", object.get_type ().name ());
                                        root.set_attribute ("oid", oid);
-                                       return doc.document_element;
+                                       return doc;
                                }
 
                                if (object.get_type ().is_a (typeof (Serializable))) {
@@ -240,9 +241,6 @@ namespace GXml {
                                   <Object> node; but then we'd probably want
                                   a separate document for it to already be a
                                   part of as its owner_document. */
-                               doc = new Document ();
-                               Serialization._docs.append (doc);
-
                                root = doc.create_element ("Object");
                                doc.append_child (root);
                                root.set_attribute ("otype", object.get_type ().name ());
@@ -292,7 +290,7 @@ namespace GXml {
                                Serialization.print_debug (doc, object);
                        }
 
-                       return doc.document_element; // user can get Document through .owner_document
+                       return doc;
                }
 
                /*
@@ -335,7 +333,7 @@ namespace GXml {
                                prop_elem_child = prop_elem.first_child;
 
                                try {
-                                       property_object = Serialization.deserialize_object (prop_elem_child);
+                                       property_object = Serialization.deserialize_object_from_node 
(prop_elem_child);
                                        val.set_object (property_object);
                                        transformed = true;
                                } catch (GXml.SerializationError e) {
@@ -378,31 +376,33 @@ namespace GXml {
                }
 
                /**
-                * Deserialize a { link GXml.Node} back into a { link GLib.Object}.
+                * Deserialize a { link GXml.Document} back into a { link GLib.Object}.
                 *
-                * This deserializes a { link GXml.Node} back into a { link GLib.Object}.  The
-                * { link GXml.Node} must represented a { link GLib.Object} as serialized by
-                * { link GXml.Serialization}.  The types of the objects that are
-                * being deserialized must be known to the system
-                * deserializing them or a { link GXml.SerializationError} will
-                * result.
+                * This deserializes a { link GXml.Document} back into a
+                * { link GLib.Object}.  The { link GXml.Document}
+                * must represent a { link GLib.Object} as serialized
+                * by { link GXml.Serialization}.  The types of the
+                * objects that are being deserialized must be known
+                * to the system deserializing them or a
+                * { link GXml.SerializationError} will result.
                 *
                 * @param node { link GXml.Node} representing a { link GLib.Object}
                 * @return the deserialized { link GLib.Object}
                 */
-               public static GLib.Object deserialize_object (Node node) throws SerializationError {
+               public static GLib.Object deserialize_object (GXml.Document doc) throws SerializationError {
+                       return deserialize_object_from_node (doc.document_element);
+               }
+               internal static GLib.Object deserialize_object_from_node (GXml.Node obj_node) throws 
SerializationError {
                        Element obj_elem;
-
                        string otype;
                        string oid;
                        Type type;
                        Object obj;
                        unowned ObjectClass obj_class;
                        ParamSpec[] specs;
-                       //bool property_found;
                        Serializable serializable = null;
 
-                       obj_elem = (Element)node;
+                       obj_elem = (Element)obj_node;
 
                        // If the object has been deserialised before, get it from cache
                        oid = obj_elem.get_attribute ("oid");
@@ -475,7 +475,10 @@ namespace GXml {
                                                        } else {
                                                                obj.set_property (pname, val);
                                                        }
-                                                       // TODO: should we make a note that for implementing 
{get,set}_property in the interface, they should specify override (in Vala)?  What about in C?  Need to test 
which one gets called in which situations (yeah, already read the tutorial)
+                                                       /* TODO: should we make a note that for implementing 
{get,set}_property in
+                                                          the interface, they should specify override (in 
Vala)?  What about in C?
+                                                          Need to test which one gets called in which 
situations (yeah, already read
+                                                          the tutorial) */
                                                }
                                        } catch (SerializationError.UNSUPPORTED_TYPE e) {
                                                err = new SerializationError.UNSUPPORTED_TYPE ("Cannot 
deserialize object '%s's property '%s' with type '%s/%s': %s\nXML [%s]", otype, spec.name, 
spec.value_type.name (), spec.value_type.to_string (), e.message, obj_elem.to_string ());
@@ -595,4 +598,4 @@ namespace GXml {
                        }
                }
        }
-}      
\ No newline at end of file
+}


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