using System; using System.Collections; using System.Collections.Generic; using System.IO; using HeuristicLab.Persistence.Core; using HeuristicLab.Persistence.Default.Xml; using HeuristicLab.Persistence.Interfaces; namespace HeuristicLab.Persistence.Test { public class RootBase { [Storable] private string baseString = "Serial"; } public class Root : RootBase { [Storable] public int[] i = new[] { 3, 4, 5, 6 }; [Storable] public string s; [Storable] public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); [Storable] public List intList = new List(new[] { 321, 312, 321 }); [Storable] public Custom c; [Storable] public List selfReferences; [Storable] public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; [Storable] public bool boolean = true; [Storable] public DateTime dateTime; [Storable] public KeyValuePair kvp = new KeyValuePair("Serial", 123); [Storable] public Dictionary dict = new Dictionary(); [Storable(DefaultValue = "default")] public string uninitialized; } public class Custom { [Storable] public int i; [Storable] public Root r; [Storable] public string name = "Serial"; } public class CloneableRoot { public int[] i = new[] { 3, 4, 5, 6 }; public string s; public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); public List intList = new List(new[] { 321, 312, 321 }); public CloneableCustom c; public List selfReferences; public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; public bool boolean = true; public DateTime dateTime; public KeyValuePair kvp = new KeyValuePair("test key", 123); public Dictionary dict = new Dictionary(); public object Clone(Dictionary twins) { if (twins.ContainsKey(this)) return twins[this]; CloneableRoot cr = new CloneableRoot(); twins.Add(this, cr); cr.i = i; cr.s = s; cr.intArray = new ArrayList(intArray); cr.intList = new List(intList); cr.c = (CloneableCustom)c.Clone(twins); cr.selfReferences = new List(); for (int j = 0; j < selfReferences.Count; j++) { cr.selfReferences.Add(this); } cr.multiDimArray = (double[,])multiDimArray.Clone(); cr.dateTime = new DateTime(dateTime.Ticks); cr.kvp = new KeyValuePair(kvp.Key, kvp.Value); cr.dict = new Dictionary(dict); return cr; } } public class CloneableCustom { public int i; public CloneableRoot r; public string name = "Serial"; public object Clone(Dictionary twins) { if (twins.ContainsKey(this)) return twins[this]; CloneableCustom cc = new CloneableCustom(); twins.Add(this, cc); cc.i = i; cc.r = (CloneableRoot)r.Clone(twins); cc.name = name; return cc; } } public class Manager { private DateTime lastLoadTime; [Storable] private DateTime lastLoadTimePersistence { get { return lastLoadTime; } // ReSharper disable ValueParameterNotUsed set { lastLoadTime = DateTime.Now; } // ReSharper restore ValueParameterNotUsed } [Storable] private double? dbl; } public class StorableObject { [Storable] Dictionary dict; public void Init() { dict = new Dictionary(); for (int i = 0; i < 1000000; i++) { dict.Add(i, i.ToString()); } } } public class CloneableObject : ICloneable { Dictionary dict; public void Init() { dict = new Dictionary(); for (int i = 0; i < 1000000; i++) { dict.Add(i, i.ToString()); } } public object Clone() { CloneableObject clone = new CloneableObject { dict = new Dictionary(dict) }; return clone; } } public class NewSerializationTest { public static void Test1() { Root r = new Root(); r.selfReferences = new List {r, r}; r.c = new Custom {r = r}; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); XmlGenerator.Serialize(r, "test"); object o = XmlParser.DeSerialize("test"); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public static void Test2() { Manager m = new Manager(); Serializer s = new Serializer(m, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); XmlGenerator xmlGenerator = new XmlGenerator(); StreamWriter writer = new StreamWriter("test2.xml"); foreach (ISerializationToken token in s) { string line = xmlGenerator.Format(token); writer.Write(line); Console.Out.Write(line); } writer.Close(); XmlParser parser = new XmlParser(new StreamReader("test2.xml")); DeSerializer deSerializer = new DeSerializer( XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); object o = deSerializer.DeSerialize(parser); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public class Base { [Storable] private int baseInt = 3; } public class Derived: Base { [Storable] private int derivedInt = 5; } public static void Main() { Test1(); //Test2(); //SpeedTest(); //SpeedTest2(); Console.In.ReadLine(); } } }