Free cookie consent management tool by TermsFeed Policy Generator

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

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

Split classes into more files. (#506)

File size: 1.0 KB
Line 
1using System;
2using System.Text;
3namespace Persistence {
4
5  public interface IPrimitiveSerializer {
6    Type Type { get; }
7    object Serialize(object o);
8    object DeSerialize(object o);
9  }
10
11  public class String2XMLSerializer : IPrimitiveSerializer {
12    public Type Type { get { return typeof(string); } }
13    public object Serialize(object o) {
14      return "<![CDATA[" +
15        ((string)o).Replace("]]>", "]]]]><![CDATA[>") +
16        "]]>";
17    }
18    public object DeSerialize(object o) {
19      StringBuilder sb = new StringBuilder();
20      foreach (string s in ((string)o).Split(
21        new string[] { "<![CDATA[", "]]>" },
22        StringSplitOptions.RemoveEmptyEntries)) {
23        sb.Append(s);
24      }
25      return sb.ToString();
26    }
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}
Note: See TracBrowser for help on using the repository browser.