Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs @ 2115

Last change on this file since 2115 was 1853, checked in by epitzer, 16 years ago

Fix EmptyStorableClass attributes. (#603)

File size: 1.6 KB
Line 
1using System.Collections;
2using System.Text;
3using HeuristicLab.Persistence.Interfaces;
4using System;
5using HeuristicLab.Persistence.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Persistence.Default.Xml.Compact {
9
10  [EmptyStorableClass]
11  public abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
12
13    protected virtual string Separator { get { return ";"; } }
14    protected abstract void Add(IEnumerable enumeration, object o);
15    protected abstract IEnumerable Instantiate();
16    protected abstract string FormatValue(object o);
17    protected abstract object ParseValue(string o);
18
19    public override XmlString Format(T t) {
20      StringBuilder sb = new StringBuilder();
21      foreach (var value in (IEnumerable)t) {
22        sb.Append(FormatValue(value));
23        sb.Append(Separator);
24      }
25      return new XmlString(sb.ToString());
26    }
27
28    public override T Parse(XmlString x) {
29      try {
30        IEnumerable enumeration = Instantiate();
31        string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
32        foreach (var value in values) {
33          Add(enumeration, ParseValue(value));
34        }
35        return (T)enumeration;
36      } catch (InvalidCastException e) {
37        throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
38      } catch (OverflowException e) {
39        throw new PersistenceException("Overflow during element parsing while trying to reconstruct number enumerable.", e);
40      }
41    }
42  }
43
44}
Note: See TracBrowser for help on using the repository browser.