1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Text;
|
---|
5 | using System.Globalization;
|
---|
6 | namespace Persistence {
|
---|
7 |
|
---|
8 | public interface IPrimitiveSerializer {
|
---|
9 | Type Type { get; }
|
---|
10 | object Serialize(object o);
|
---|
11 | object DeSerialize(object o);
|
---|
12 | }
|
---|
13 |
|
---|
14 | public class String2XMLSerializer : IPrimitiveSerializer {
|
---|
15 | public Type Type { get { return typeof(string); } }
|
---|
16 | public object Serialize(object o) {
|
---|
17 | return "<![CDATA[" +
|
---|
18 | ((string)o).Replace("]]>", "]]]]><![CDATA[>") +
|
---|
19 | "]]>";
|
---|
20 | }
|
---|
21 | public object DeSerialize(object o) {
|
---|
22 | StringBuilder sb = new StringBuilder();
|
---|
23 | foreach (string s in ((string)o).Split(
|
---|
24 | new[] { "<![CDATA[", "]]>" },
|
---|
25 | StringSplitOptions.RemoveEmptyEntries)) {
|
---|
26 | sb.Append(s);
|
---|
27 | }
|
---|
28 | return sb.ToString();
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public class Int2XMLSerializer : IPrimitiveSerializer {
|
---|
33 | public Type Type { get { return typeof(int); } }
|
---|
34 | public object Serialize(object o) {
|
---|
35 | return ((int)o).ToString();
|
---|
36 | }
|
---|
37 | public object DeSerialize(object o) {
|
---|
38 | return int.Parse((string)o);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public class Double2XmlSerializer : IPrimitiveSerializer {
|
---|
43 | public Type Type { get { return typeof(double); } }
|
---|
44 | public object Serialize(object o) {
|
---|
45 | return ((double)o).ToString("r", CultureInfo.InvariantCulture);
|
---|
46 | }
|
---|
47 | public object DeSerialize(object o) {
|
---|
48 | return double.Parse((string)o, CultureInfo.InvariantCulture);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public class Boolean2XmlSerializer : IPrimitiveSerializer {
|
---|
53 | public Type Type { get { return typeof(bool); } }
|
---|
54 | public object Serialize(object o) {
|
---|
55 | return ((bool)o).ToString();
|
---|
56 | }
|
---|
57 | public object DeSerialize(object o) {
|
---|
58 | return bool.Parse((string)o);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public class DateTime2XmlSerializer : IPrimitiveSerializer {
|
---|
63 | public Type Type { get { return typeof(DateTime); } }
|
---|
64 | public object Serialize(object o) {
|
---|
65 | return ((DateTime)o).Ticks.ToString();
|
---|
66 | }
|
---|
67 | public object DeSerialize(object o) {
|
---|
68 | return new DateTime(long.Parse((string)o));
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public abstract class NumberArray2XmlSerializer : IPrimitiveSerializer {
|
---|
73 | public abstract Type Type { get; }
|
---|
74 | protected virtual string Separator { get { return ";"; } }
|
---|
75 | protected abstract string formatValue(object o);
|
---|
76 | protected abstract object parseValue(string o);
|
---|
77 | public object Serialize(object obj) {
|
---|
78 | Array a = (Array)obj;
|
---|
79 | StringBuilder sb = new StringBuilder();
|
---|
80 | sb.Append(a.Rank);
|
---|
81 | for (int i = 0; i < a.Rank; i++) {
|
---|
82 | sb.Append(Separator);
|
---|
83 | sb.Append(a.GetLength(i));
|
---|
84 | }
|
---|
85 | foreach (object o in a) {
|
---|
86 | sb.Append(Separator);
|
---|
87 | sb.Append(formatValue(o));
|
---|
88 | }
|
---|
89 | return sb.ToString();
|
---|
90 | }
|
---|
91 | public object DeSerialize(object o) {
|
---|
92 | IEnumerator values =
|
---|
93 | ((string) o)
|
---|
94 | .Split(new[] {Separator},
|
---|
95 | StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
|
---|
96 | values.MoveNext();
|
---|
97 | int rank = int.Parse((string)values.Current);
|
---|
98 | int[] lengths = new int[rank];
|
---|
99 | for ( int i = 0; i<rank; i++ ) {
|
---|
100 | values.MoveNext();
|
---|
101 | lengths[i] = int.Parse((string)values.Current);
|
---|
102 | }
|
---|
103 | Array a = Array.CreateInstance(this.Type.GetElementType(), lengths);
|
---|
104 | int[] positions = new int[rank];
|
---|
105 | while ( values.MoveNext() ) {
|
---|
106 | a.SetValue(parseValue((string)values.Current), positions);
|
---|
107 | positions[0] += 1;
|
---|
108 | for ( int i = 0; i<rank-1; i++) {
|
---|
109 | if (positions[i] >= lengths[i]) {
|
---|
110 | positions[i] = 0;
|
---|
111 | positions[i + 1] += 1;
|
---|
112 | } else {
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return a;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | public class IntArray2XmlSerializer : NumberArray2XmlSerializer {
|
---|
121 | public override Type Type { get { return typeof(int[]); } }
|
---|
122 | protected override string formatValue(object o) { return o.ToString(); }
|
---|
123 | protected override object parseValue(string o) { return int.Parse(o); }
|
---|
124 | }
|
---|
125 | public class Int2DArray2XmlSerializer : IntArray2XmlSerializer {
|
---|
126 | public override Type Type { get { return typeof (int[,]); } }
|
---|
127 | }
|
---|
128 | public class Int3DArray2XmlSerializer : IntArray2XmlSerializer {
|
---|
129 | public override Type Type { get { return typeof(int[,,]); } }
|
---|
130 | }
|
---|
131 | public class DoubleArray2XmlSerializer : NumberArray2XmlSerializer {
|
---|
132 | public override Type Type { get { return typeof(double[]); } }
|
---|
133 | protected override string formatValue(object o) { return ((double) o).ToString("r"); }
|
---|
134 | protected override object parseValue(string o) { return double.Parse(o); }
|
---|
135 | }
|
---|
136 | public class Double2DArray2XmlSerializer : DoubleArray2XmlSerializer {
|
---|
137 | public override Type Type { get { return typeof(double[,]); } }
|
---|
138 | }
|
---|
139 | public class Double3DArray2XmlSerializer : DoubleArray2XmlSerializer {
|
---|
140 | public override Type Type { get { return typeof(double[,,]); } }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public abstract class NumberEnumeration2XmlSerializer : IPrimitiveSerializer {
|
---|
144 | public abstract Type Type { get; }
|
---|
145 | protected virtual string Separator { get { return ";"; } }
|
---|
146 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
147 | protected abstract object Instantiate();
|
---|
148 | protected abstract string formatValue(object o);
|
---|
149 | protected abstract object parseValue(string o);
|
---|
150 | public object Serialize(object o) {
|
---|
151 | StringBuilder sb = new StringBuilder();
|
---|
152 | foreach (var value in (IEnumerable) o) {
|
---|
153 | sb.Append(formatValue(value));
|
---|
154 | sb.Append(Separator);
|
---|
155 | }
|
---|
156 | return sb.ToString();
|
---|
157 | }
|
---|
158 | public object DeSerialize(object o) {
|
---|
159 | IEnumerable enumeration = (IEnumerable) Instantiate();
|
---|
160 | string[] values = ((string) o).Split(new[] {Separator}, StringSplitOptions.RemoveEmptyEntries);
|
---|
161 | foreach ( var value in values ) {
|
---|
162 | Add(enumeration, parseValue(value));
|
---|
163 | }
|
---|
164 | return enumeration;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | public class IntList2XmlSerializer : NumberEnumeration2XmlSerializer {
|
---|
168 | public override Type Type { get { return typeof(List<int>); } }
|
---|
169 | protected override void Add(IEnumerable enumeration, object o) { ((List<int>)enumeration).Add((int)o); }
|
---|
170 | protected override object Instantiate() { return new List<int>(); }
|
---|
171 | protected override string formatValue(object o) { return o.ToString(); }
|
---|
172 | protected override object parseValue(string o) { return int.Parse(o); }
|
---|
173 | }
|
---|
174 | } |
---|