[longomatch] Use Json for seralization everywhere with Json.NET



commit 03bd16da7aa2466455ec2f1c8ac301ad263819a4
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Jan 30 22:05:57 2014 +0100

    Use Json for seralization everywhere with Json.NET

 LongoMatch.Core/Common/Enums.cs                    |    6 ++
 LongoMatch.Core/Common/SerializableObject.cs       |   76 ++++++++++++++++++--
 LongoMatch.Core/Config.cs                          |    4 +-
 LongoMatch.Core/LongoMatch.Core.mdp                |    3 +-
 LongoMatch.Core/Store/Category.cs                  |    4 +
 LongoMatch.Core/Store/Play.cs                      |    8 ++
 LongoMatch.Core/Store/Project.cs                   |    7 ++-
 LongoMatch.Core/Store/ProjectDescription.cs        |    3 +
 LongoMatch.Core/Store/SubCategory.cs               |    2 +
 LongoMatch.Core/Store/TagStore.cs                  |    4 +
 .../Store/Templates/CategoriesTemplate.cs          |    5 ++
 LongoMatch.Core/Store/Templates/TeamTemplate.cs    |    3 +-
 LongoMatch.Core/Store/TimeNode.cs                  |    2 +
 LongoMatch.Core/Store/TimelineNode.cs              |    5 ++
 14 files changed, 122 insertions(+), 10 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Enums.cs b/LongoMatch.Core/Common/Enums.cs
index dc57dc4..20d960a 100644
--- a/LongoMatch.Core/Common/Enums.cs
+++ b/LongoMatch.Core/Common/Enums.cs
@@ -21,6 +21,12 @@ using System;
 namespace LongoMatch.Common
 {
 
+       public enum SerializationType {
+               Binary,
+               Xml,
+               Json
+       }
+               
 
        public enum ProjectType {
                CaptureProject,
diff --git a/LongoMatch.Core/Common/SerializableObject.cs b/LongoMatch.Core/Common/SerializableObject.cs
index d32f466..4d8c497 100644
--- a/LongoMatch.Core/Common/SerializableObject.cs
+++ b/LongoMatch.Core/Common/SerializableObject.cs
@@ -17,18 +17,21 @@
 //
 using System;
 using System.IO;
+using System.Collections.Generic;
+using System.Linq;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Xml.Serialization;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+using LongoMatch.Interfaces;
+using System.Reflection;
+using LongoMatch.Store.Templates;
+using Newtonsoft.Json.Converters;
 
 namespace LongoMatch.Common
 {
        public class SerializableObject
        {
-               public enum SerializationType {
-                       Binary,
-                       Xml
-               }
-               
                public static void Save<T>(T obj, Stream stream,
                                           SerializationType type=SerializationType.Binary) {
                        switch (type) {
@@ -40,6 +43,11 @@ namespace LongoMatch.Common
                                XmlSerializer xmlformatter = new XmlSerializer(typeof(T));
                                xmlformatter.Serialize(stream, obj);
                                break;
+                       case SerializationType.Json:
+                               StreamWriter sw = new StreamWriter (stream);
+                               sw.Write (JsonConvert.SerializeObject (obj, JsonSettings));
+                               sw.Flush();
+                               break;
                        }
                }
                
@@ -61,6 +69,9 @@ namespace LongoMatch.Common
                        case SerializationType.Xml:
                                XmlSerializer xmlformatter = new XmlSerializer(typeof(T));
                                return (T) xmlformatter.Deserialize(stream);
+                       case SerializationType.Json:
+                               StreamReader sr = new StreamReader (stream);
+                               return JsonConvert.DeserializeObject<T> (sr.ReadToEnd(), JsonSettings);
                        default:
                                throw new Exception();
                        }
@@ -86,6 +97,61 @@ namespace LongoMatch.Common
                                }
                        }
                }
+               
+               static JsonSerializerSettings JsonSettings {
+                       get{
+                               JsonSerializerSettings settings = new JsonSerializerSettings ();
+                               settings.Formatting = Formatting.Indented;
+                               settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
+                               settings.TypeNameHandling = TypeNameHandling.Objects;
+                               settings.ContractResolver = new ListObjectContractResolver ();
+                               settings.Converters.Add (new VersionConverter ());
+                               return settings;
+                       }
+               }
+       }
+       
+       public class ListObjectContractResolver : DefaultContractResolver
+       {
+               /* To serialize/desarialize List objects by including private fields
+                * _size and _items */
+               public ListObjectContractResolver()
+               {
+               }
+               
+               protected override IList<JsonProperty> CreateProperties (Type type, MemberSerialization 
memberSerialization)
+               {
+                       IList<JsonProperty> props = new List<JsonProperty>();
+                       
+                       props = base.CreateProperties (type, memberSerialization);
+                       if (typeof(ISubCategory).IsAssignableFrom (type) ||
+                           type == typeof(Categories) ||
+                           type == typeof(TeamTemplate) ||
+                           type == typeof(SubCategoryTemplate))
+                       {
+                               JsonProperty itprop;
+                               BindingFlags flags;
+                               
+                               props = props.Where (p => p.PropertyName != "Count").ToList();
+                               flags =  base.DefaultMembersSearchFlags;
+                               IList<JsonProperty> allprops = new List<JsonProperty>();
+                               base.DefaultMembersSearchFlags = flags | 
System.Reflection.BindingFlags.NonPublic;
+                               allprops = base.CreateProperties (type, MemberSerialization.Fields);
+                               base.DefaultMembersSearchFlags = flags;
+                               itprop = allprops.FirstOrDefault (p => p.PropertyName == "_items");
+                               if (itprop != null) {
+                                       props.Add (itprop);
+                               }
+                               itprop = allprops.FirstOrDefault (p => p.PropertyName == "_size");
+                               if (itprop != null) {
+                                       props.Add (itprop);
+                               }
+                       }
+                       
+                       return props;
+               }
        }
+
+
 }
 
diff --git a/LongoMatch.Core/Config.cs b/LongoMatch.Core/Config.cs
index 80eb501..8d77cf5 100644
--- a/LongoMatch.Core/Config.cs
+++ b/LongoMatch.Core/Config.cs
@@ -33,7 +33,7 @@ namespace LongoMatch
                        if (File.Exists(Config.ConfigFile)) {
                                Log.Information ("Loading config from " + Config.ConfigFile);
                                try {
-                                       state = SerializableObject.Load<ConfigState>(Config.ConfigFile, 
SerializableObject.SerializationType.Xml);
+                                       state = SerializableObject.LoadSafe<ConfigState>(Config.ConfigFile);
                                } catch (Exception ex) {
                                        Log.Error ("Error loading config");
                                        Log.Exception (ex);
@@ -49,7 +49,7 @@ namespace LongoMatch
                
                public static void Save () {
                        try {
-                               SerializableObject.Save(state, Config.ConfigFile, 
SerializableObject.SerializationType.Xml); 
+                               SerializableObject.Save(state, Config.ConfigFile); 
                        } catch (Exception ex) {
                                Log.Error ("Errro saving config");
                                Log.Exception (ex);
diff --git a/LongoMatch.Core/LongoMatch.Core.mdp b/LongoMatch.Core/LongoMatch.Core.mdp
index f1c5e79..9ee404a 100644
--- a/LongoMatch.Core/LongoMatch.Core.mdp
+++ b/LongoMatch.Core/LongoMatch.Core.mdp
@@ -121,5 +121,6 @@
     <ProjectReference type="Gac" localcopy="True" refto="gdk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Gac" localcopy="True" refto="gtk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Gac" localcopy="True" refto="System.Drawing, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a" />
+    <ProjectReference type="Gac" localcopy="False" refto="Newtonsoft.Json, Version=5.0.0.0, Culture=neutral, 
PublicKeyToken=b9a188c8922137c6" />
   </References>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/LongoMatch.Core/Store/Category.cs b/LongoMatch.Core/Store/Category.cs
index 9438f71..f42aec9 100644
--- a/LongoMatch.Core/Store/Category.cs
+++ b/LongoMatch.Core/Store/Category.cs
@@ -23,6 +23,7 @@ using System.Drawing;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 using Mono.Unix;
+using Newtonsoft.Json;
 
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
@@ -38,6 +39,7 @@ namespace LongoMatch.Store
        public class Category:TimeNode, ISerializable
        {
 
+               [JsonProperty ("UUID")]
                private Guid _UUID;
 
                #region Constructors
@@ -54,6 +56,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Unique ID for this category
                /// </summary>
+               [JsonIgnore]
                public Guid UUID {
                        get {
                                return _UUID;
@@ -125,6 +128,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Sort method string used for the UI
                /// </summary>
+               [JsonIgnore]
                public string SortMethodString {
                        get {
                                switch(SortMethod) {
diff --git a/LongoMatch.Core/Store/Play.cs b/LongoMatch.Core/Store/Play.cs
index 32c57dc..1cbaaab 100644
--- a/LongoMatch.Core/Store/Play.cs
+++ b/LongoMatch.Core/Store/Play.cs
@@ -24,6 +24,7 @@ using System.Linq;
 using Mono.Unix;
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
+using Newtonsoft.Json;
 
 namespace LongoMatch.Store
 {
@@ -78,6 +79,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Start frame number
                /// </summary>
+               [JsonIgnore]
                public uint StartFrame {
                        get {
                                return (uint)(Start.MSeconds * Fps / 1000);
@@ -90,6 +92,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Stop frame number
                /// </summary>
+               [JsonIgnore]
                public uint StopFrame {
                        get {
                                return (uint)(Stop.MSeconds * Fps / 1000);
@@ -102,6 +105,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Get the key frame number if this play as key frame drawing or 0
                /// </summary>
+               [JsonIgnore]
                public uint KeyFrame {
                        get {
                                if(HasDrawings)
@@ -127,6 +131,7 @@ namespace LongoMatch.Store
                }
 
                /* FIXME: Keep this until we support multiple drawings */
+               [JsonIgnore]
                public Drawing KeyFrameDrawing {
                        get {
                                if(Drawings.Count > 0)
@@ -144,6 +149,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Get wether the play has at least a frame drawing
                /// </summary>
+               [JsonIgnore]
                public bool HasDrawings {
                        get {
                                return Drawings.Count > 0;
@@ -153,6 +159,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Central frame number using (stopFrame-startFrame)/2
                /// </summary>
+               [JsonIgnore]
                public uint CentralFrame {
                        get {
                                return StopFrame-((TotalFrames)/2);
@@ -162,6 +169,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Number of frames inside the play's boundaries
                /// </summary>
+               [JsonIgnore]
                public uint TotalFrames {
                        get {
                                return StopFrame-StartFrame;
diff --git a/LongoMatch.Core/Store/Project.cs b/LongoMatch.Core/Store/Project.cs
index e63c6ea..81f84b8 100644
--- a/LongoMatch.Core/Store/Project.cs
+++ b/LongoMatch.Core/Store/Project.cs
@@ -22,12 +22,13 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using Newtonsoft.Json;
+using Mono.Unix;
 
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
 using LongoMatch.Store;
 using LongoMatch.Store.Templates;
-using Mono.Unix;
 
 namespace LongoMatch.Store
 {
@@ -47,8 +48,10 @@ namespace LongoMatch.Store
        public class Project : IComparable
        {
 
+               [JsonProperty ("UUID")]
                readonly Guid _UUID;
                ProjectDescription description;
+               [JsonProperty ("Timeline")]
                List<Play> timeline;
 
                #region Constructors
@@ -66,6 +69,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Unique ID for the project
                /// </summary>
+               [JsonIgnore]
                public Guid UUID {
                        get {
                                return _UUID;
@@ -115,6 +119,7 @@ namespace LongoMatch.Store
                        }
                }
                
+               [JsonIgnore]
                public IEnumerable<IGrouping<Category, Play>> PlaysGroupedByCategory {
                        get {
                                return timeline.GroupBy(play => play.Category);
diff --git a/LongoMatch.Core/Store/ProjectDescription.cs b/LongoMatch.Core/Store/ProjectDescription.cs
index 1e0a430..3d2ab5b 100644
--- a/LongoMatch.Core/Store/ProjectDescription.cs
+++ b/LongoMatch.Core/Store/ProjectDescription.cs
@@ -17,6 +17,7 @@
 //
 
 using System;
+using Newtonsoft.Json;
 using LongoMatch.Store;
 
 namespace LongoMatch.Store
@@ -39,6 +40,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Title of the project
                /// </summary>
+               [JsonIgnore]
                public String Title {
                        get {
                                if (File == null)
@@ -115,6 +117,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// String representing the video format like "widhtxheight fps"
                /// </summary>
+               [JsonIgnore]
                public String Format {
                        get {
                                if (File == null)
diff --git a/LongoMatch.Core/Store/SubCategory.cs b/LongoMatch.Core/Store/SubCategory.cs
index 97443ce..f05f8e8 100644
--- a/LongoMatch.Core/Store/SubCategory.cs
+++ b/LongoMatch.Core/Store/SubCategory.cs
@@ -19,6 +19,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using Mono.Unix;
+using Newtonsoft.Json;
 
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
@@ -40,6 +41,7 @@ namespace LongoMatch.Store
        /// Goal category to extends its tags.
        /// </summary>
        [Serializable]
+       [JsonObject]
        public class SubCategory<T>: List<T>, ISubCategory
        {
 
diff --git a/LongoMatch.Core/Store/TagStore.cs b/LongoMatch.Core/Store/TagStore.cs
index e736baa..92d8323 100644
--- a/LongoMatch.Core/Store/TagStore.cs
+++ b/LongoMatch.Core/Store/TagStore.cs
@@ -18,6 +18,8 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using Newtonsoft.Json;
+
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
 using LongoMatch.Store;
@@ -25,6 +27,7 @@ using LongoMatch.Store;
 namespace LongoMatch.Store
 {
        [Serializable]
+       [JsonObject (MemberSerialization = MemberSerialization.OptIn)]
        public class TagsStore<T, W> where T:ITag<W>
        {
                protected List<T> tagsList;
@@ -33,6 +36,7 @@ namespace LongoMatch.Store
                        tagsList = new List<T>();
                }
                
+               [JsonProperty ("Tags")]
                public List<T> Tags {
                        get{
                                return tagsList;
diff --git a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs 
b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
index 395efec..c5efe27 100644
--- a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
@@ -21,6 +21,7 @@ using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Linq;
+using Newtonsoft.Json;
 
 using Mono.Unix;
 using LongoMatch.Common;
@@ -40,6 +41,7 @@ namespace LongoMatch.Store.Templates
        /// The <see cref="LongoMatch.DB.Project"/> must handle all the changes
        /// </summary>
        [Serializable]
+       [JsonObject]
        public class Categories: List<Category>, ITemplate, ITemplate<Category>
        {
                /* Database additions */
@@ -84,8 +86,11 @@ namespace LongoMatch.Store.Templates
                }
                
                /* Keep this for backwards compatiblity with 0.18.11 */
+               [JsonIgnore]
                public Image FieldBackgroundImage {get; set;}
+               [JsonIgnore]
                public Image HalfFieldBackgroundImage {get; set;}
+               [JsonIgnore]
                public Image GoalBackgroundImage {get; set;}
 
                public Image FieldBackground {
diff --git a/LongoMatch.Core/Store/Templates/TeamTemplate.cs b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
index 4b67795..29d2919 100644
--- a/LongoMatch.Core/Store/Templates/TeamTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
@@ -20,6 +20,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using Mono.Unix;
+using Newtonsoft.Json;
 
 using LongoMatch.Common;
 using LongoMatch.Interfaces;
@@ -27,7 +28,7 @@ using LongoMatch.Interfaces;
 namespace LongoMatch.Store.Templates
 {
        [Serializable]
-
+       [JsonObject]
        public class TeamTemplate: List<Player>, ITemplate<Player>
        {
                private byte[] thumbnailBuf;
diff --git a/LongoMatch.Core/Store/TimeNode.cs b/LongoMatch.Core/Store/TimeNode.cs
index 8a1eb19..36267f9 100644
--- a/LongoMatch.Core/Store/TimeNode.cs
+++ b/LongoMatch.Core/Store/TimeNode.cs
@@ -20,6 +20,7 @@
 
 using System;
 using System.Collections.Generic;
+using Newtonsoft.Json;
 using LongoMatch.Store;
 
 namespace LongoMatch.Store
@@ -66,6 +67,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Duration (stop_time - start_time)
                /// </summary>
+               [JsonIgnore]
                public Time Duration {
                        get {
                                return Stop-Start;
diff --git a/LongoMatch.Core/Store/TimelineNode.cs b/LongoMatch.Core/Store/TimelineNode.cs
index 5571418..a731af2 100644
--- a/LongoMatch.Core/Store/TimelineNode.cs
+++ b/LongoMatch.Core/Store/TimelineNode.cs
@@ -16,6 +16,7 @@
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 // 
 using System;
+using Newtonsoft.Json;
 using LongoMatch.Interfaces;
 
 namespace LongoMatch.Store
@@ -42,6 +43,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Start frame number
                /// </summary>
+               [JsonIgnore]
                public uint StartFrame {
                        get {
                                return (uint)(Start.MSeconds * Fps / 1000);
@@ -54,6 +56,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Stop frame number
                /// </summary>
+               [JsonIgnore]
                public uint StopFrame {
                        get {
                                return (uint)(Stop.MSeconds * Fps / 1000);
@@ -74,6 +77,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Central frame number using (stopFrame-startFrame)/2
                /// </summary>
+               [JsonIgnore]
                public uint CentralFrame {
                        get {
                                return StopFrame-((TotalFrames)/2);
@@ -83,6 +87,7 @@ namespace LongoMatch.Store
                /// <summary>
                /// Number of frames inside the play's boundaries
                /// </summary>
+               [JsonIgnore]
                public uint TotalFrames {
                        get {
                                return StopFrame-StartFrame;


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