[longomatch] Add a way to serialize IStorable as local references



commit b5b94128610a4c3504ea3364182232c6ba04bddf
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Mon Mar 16 12:46:00 2015 +0100

    Add a way to serialize IStorable as local references

 LongoMatch.DB/CouchbaseStorage.cs    |    2 +-
 LongoMatch.DB/DocumentsSerializer.cs |   30 +++++++++++++++++-------------
 Tests/DB/TestStorage.cs              |    4 ++--
 3 files changed, 20 insertions(+), 16 deletions(-)
---
diff --git a/LongoMatch.DB/CouchbaseStorage.cs b/LongoMatch.DB/CouchbaseStorage.cs
index eaf9885..c0fe973 100644
--- a/LongoMatch.DB/CouchbaseStorage.cs
+++ b/LongoMatch.DB/CouchbaseStorage.cs
@@ -63,7 +63,7 @@ namespace LongoMatch.DB
                        Document doc = db.GetDocument (t.ID.ToString ());
                        doc.Update((UnsavedRevision newRevision) => 
                                {
-                                       JObject jo = DocumentsSerializer.SerializeObject (t, newRevision, 
true, null);
+                                       JObject jo = DocumentsSerializer.SerializeObject (t, newRevision, 
null);
                                        IDictionary<string, object> props = jo.ToObject<IDictionary<string, 
object>>();
                                        /* SetProperties sets a new properties dictionary, removing the 
attachments we
                                         * added in the serialization */
diff --git a/LongoMatch.DB/DocumentsSerializer.cs b/LongoMatch.DB/DocumentsSerializer.cs
index 728e9bc..4ec6b7a 100644
--- a/LongoMatch.DB/DocumentsSerializer.cs
+++ b/LongoMatch.DB/DocumentsSerializer.cs
@@ -37,14 +37,13 @@ namespace LongoMatch.DB
                /// </summary>
                /// <returns>A new object serialized.</returns>
                /// <param name="obj">The <c>IStorable</c> to serialize.</param>
-               /// <param name="preserveReferences">If set to <c>true</c> preserve references.</param>
-               /// <param name="refTypes">A list of types that should be serialized by ID.</param>
-               /// <param name="skipFields">Extra fields that shouldn't be serialized.</param>
-               public static JObject SerializeObject (IStorable obj, Revision rev, bool preserveReferences,
-                                                      Type[] refTypes)
+               /// <param name="rev">The document revision to serialize.</param>
+               /// <param name="localStorables">A list of <see cref="LongoMatch.Core.Interfaces.IStorable"/>
+               /// types that should be serialized as local referencies instead of by document ID.</param>
+               public static JObject SerializeObject (IStorable obj, Revision rev, List<Type> localStorables)
                {
                        JObject jo = JObject.FromObject (obj,
-                               GetSerializer (null, rev, preserveReferences, refTypes));
+                               GetSerializer (obj.GetType (), null, rev, localStorables));
                        jo["DocType"] = obj.GetType ().Name;
                        return jo;
                }
@@ -55,21 +54,26 @@ namespace LongoMatch.DB
                /// <returns>A new object deserialized.</returns>
                /// <param name="db">The <c>Database</c> where the Document is stored.</param>
                /// <param name="doc">The document to deserialize.</param>
+               /// <param name = "serializer">The serializer to use when deserializing the object</param>
                /// <typeparam name="T">The 1st type parameter.</typeparam>
-               public static T DeserializeObject<T> (Database db, Document doc)
+               public static T DeserializeObject<T> (Database db, Document doc, JsonSerializer serializer = 
null)
                {
                        JObject jo = JObject.FromObject (doc.Properties);
-                       return jo.ToObject<T> (GetSerializer (db, doc.CurrentRevision, true, null));
+                       if (serializer == null) {
+                               serializer = GetSerializer (typeof(T), db, doc.CurrentRevision, null);
+                       }
+                       return jo.ToObject<T> (serializer);
                }
 
-               static JsonSerializer GetSerializer (Database db, Revision rev,
-                                                    bool preserveReferences, Type[] refTypes)
+               static JsonSerializer GetSerializer (Type serType, Database db, Revision rev, List<Type> 
localTypes)
                {
+                       if (localTypes == null) {
+                               localTypes = new List<Type> ();
+                       }
+                       localTypes.Add (serType);
                        JsonSerializerSettings settings = new JsonSerializerSettings ();
                        settings.Formatting = Formatting.Indented;
-                       if (preserveReferences) {
-                               settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
-                       }
+                       settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
                        settings.TypeNameHandling = TypeNameHandling.Objects;
                        settings.ContractResolver = new ImagePropertiesContractResolver (rev);
                        settings.Converters.Add (new ImageConverter (rev));
diff --git a/Tests/DB/TestStorage.cs b/Tests/DB/TestStorage.cs
index 300eff5..5d5535e 100644
--- a/Tests/DB/TestStorage.cs
+++ b/Tests/DB/TestStorage.cs
@@ -68,7 +68,7 @@ namespace Tests.DB
                                ID = Guid.NewGuid (),
                        };
                        Document doc = db.CreateDocument ();
-                       JObject jo = DocumentsSerializer.SerializeObject (t, doc.CreateRevision (), true, 
null);
+                       JObject jo = DocumentsSerializer.SerializeObject (t, doc.CreateRevision (), null);
                        Assert.AreEqual (t.ID, jo.Value<Guid> ("ID"));
                        Assert.AreEqual ("StorableImageTest", jo.Value<string> ("DocType"));
                }
@@ -83,7 +83,7 @@ namespace Tests.DB
                        };
                        Document doc = db.CreateDocument ();
                        UnsavedRevision rev = doc.CreateRevision ();
-                       JObject jo = DocumentsSerializer.SerializeObject (t, rev, true, null);
+                       JObject jo = DocumentsSerializer.SerializeObject (t, rev, null);
                        Assert.IsNotNull (jo ["ID"]);
                        Assert.AreEqual ("attachment::Image1", jo ["Image1"].Value<string>());
                        Assert.AreEqual ("attachment::Image2", jo ["Image2"].Value<string>());


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