Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/PrimitiveSerializers.cs @ 1312

Last change on this file since 1312 was 1312, checked in by epitzer, 15 years ago

Serialization of double values. (#506)

File size: 1.4 KB
Line 
1using System;
2using System.Text;
3using System.Globalization;
4namespace Persistence {
5
6  public interface IPrimitiveSerializer {
7    Type Type { get; }
8    object Serialize(object o);
9    object DeSerialize(object o);
10  }
11
12  public class String2XMLSerializer : IPrimitiveSerializer {
13    public Type Type { get { return typeof(string); } }
14    public object Serialize(object o) {
15      return "<![CDATA[" +
16        ((string)o).Replace("]]>", "]]]]><![CDATA[>") +
17        "]]>";
18    }
19    public object DeSerialize(object o) {
20      StringBuilder sb = new StringBuilder();
21      foreach (string s in ((string)o).Split(
22        new string[] { "<![CDATA[", "]]>" },
23        StringSplitOptions.RemoveEmptyEntries)) {
24        sb.Append(s);
25      }
26      return sb.ToString();
27    }
28  }
29  public class Int2XMLSerializer : IPrimitiveSerializer {
30    public Type Type { get { return typeof(int); } }
31    public object Serialize(object o) {
32      return ((int)o).ToString();
33    }
34    public object DeSerialize(object o) {
35      return int.Parse((string)o);
36    }
37  }
38  public class Double2XmlSerializer : IPrimitiveSerializer {   
39    public Type Type { get { return typeof(double); } }       
40    public object Serialize(object o) {
41      return ((double)o).ToString("r", CultureInfo.InvariantCulture);     
42    }
43    public object DeSerialize(object o) {
44      return double.Parse((string)o, CultureInfo.InvariantCulture);
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.