1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Collections;
|
---|
5 | using System.IO;
|
---|
6 |
|
---|
7 | namespace Persistence {
|
---|
8 |
|
---|
9 | public class Root {
|
---|
10 |
|
---|
11 | [Storable]
|
---|
12 | public int[] i = new int[] { 3, 4, 5, 6 };
|
---|
13 | [Storable]
|
---|
14 | public string s;
|
---|
15 | [Storable]
|
---|
16 | public ArrayList intArray = new ArrayList(new int[] { 1, 2, 3 });
|
---|
17 | [Storable]
|
---|
18 | public List<int> intList = new List<int>(new int[] { 321, 312, 321 });
|
---|
19 | [Storable]
|
---|
20 | public Custom c;
|
---|
21 | [Storable]
|
---|
22 | public List<Root> selfReferences;
|
---|
23 | [Storable]
|
---|
24 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
25 | [Storable]
|
---|
26 | public bool boolean = true;
|
---|
27 | [Storable]
|
---|
28 | public DateTime dateTime = new DateTime();
|
---|
29 | [Storable]
|
---|
30 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
|
---|
31 | [Storable]
|
---|
32 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public class Custom {
|
---|
36 | [Storable]
|
---|
37 | public int i;
|
---|
38 | [Storable]
|
---|
39 | public Root r;
|
---|
40 | [Storable]
|
---|
41 | public string name = "Serial";
|
---|
42 | }
|
---|
43 |
|
---|
44 | public class Manager {
|
---|
45 |
|
---|
46 | private DateTime lastLoadTime;
|
---|
47 | [Storable]
|
---|
48 | private DateTime lastLoadTimePersistence {
|
---|
49 | get { return lastLoadTime; }
|
---|
50 | set { this.lastLoadTime = DateTime.Now; }
|
---|
51 | }
|
---|
52 | [Storable]
|
---|
53 | private Nullable<double> dbl = null;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public class NewSerializationTest {
|
---|
57 |
|
---|
58 | public static void Test1() {
|
---|
59 | Root r = new Root();
|
---|
60 | r.selfReferences = new List<Root>();
|
---|
61 | r.selfReferences.Add(r);
|
---|
62 | r.selfReferences.Add(r);
|
---|
63 | r.c = new Custom();
|
---|
64 | r.c.r = r;
|
---|
65 | r.dict.Add("one", 1);
|
---|
66 | r.dict.Add("two", 2);
|
---|
67 | r.dict.Add("three", 3);
|
---|
68 | Serializer s = new Serializer(r);
|
---|
69 | Persistence.XmlFormatter xmlFormatter = new Persistence.XmlFormatter();
|
---|
70 | StreamWriter writer = new StreamWriter("test.xml");
|
---|
71 | foreach (ISerializationToken token in s) {
|
---|
72 | string line = xmlFormatter.Format(token);
|
---|
73 | writer.Write(line);
|
---|
74 | Console.Out.Write(line);
|
---|
75 | }
|
---|
76 | writer.Close();
|
---|
77 | XmlParser parser = new XmlParser(new StreamReader("test.xml"));
|
---|
78 | DeSerializer deSerializer = new DeSerializer();
|
---|
79 | object o = deSerializer.DeSerialize(parser);
|
---|
80 | Console.Out.WriteLine(Util.AutoFormat(o, true));
|
---|
81 | }
|
---|
82 |
|
---|
83 | public static void Test2() {
|
---|
84 | Manager m = new Manager();
|
---|
85 | Serializer s = new Serializer(m);
|
---|
86 | Persistence.XmlFormatter xmlFormatter = new Persistence.XmlFormatter();
|
---|
87 | StreamWriter writer = new StreamWriter("test2.xml");
|
---|
88 | foreach (ISerializationToken token in s) {
|
---|
89 | string line = xmlFormatter.Format(token);
|
---|
90 | writer.Write(line);
|
---|
91 | Console.Out.Write(line);
|
---|
92 | }
|
---|
93 | writer.Close();
|
---|
94 | XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
|
---|
95 | DeSerializer deSerializer = new DeSerializer();
|
---|
96 | object o = deSerializer.DeSerialize(parser);
|
---|
97 | Console.Out.WriteLine(Util.AutoFormat(o, true));
|
---|
98 | }
|
---|
99 |
|
---|
100 | public static void Main() {
|
---|
101 | //Test1();
|
---|
102 | Test2();
|
---|
103 | Console.In.ReadLine();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|