Free cookie consent management tool by TermsFeed Policy Generator

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

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

make most serializers internal and complete API documentation (#548)

File size: 1.6 KB
RevLine 
[1542]1using System.Collections;
2using System.Text;
3using HeuristicLab.Persistence.Interfaces;
4using System;
[1625]5using HeuristicLab.Persistence.Core;
[1853]6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1542]7
8namespace HeuristicLab.Persistence.Default.Xml.Compact {
[1566]9
[3017]10  [StorableClass]
[3036]11  internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
[1542]12
13    protected virtual string Separator { get { return ";"; } }
14    protected abstract void Add(IEnumerable enumeration, object o);
[1564]15    protected abstract IEnumerable Instantiate();
[1542]16    protected abstract string FormatValue(object o);
17    protected abstract object ParseValue(string o);
18
[1564]19    public override XmlString Format(T t) {
[1542]20      StringBuilder sb = new StringBuilder();
[1564]21      foreach (var value in (IEnumerable)t) {
[1542]22        sb.Append(FormatValue(value));
23        sb.Append(Separator);
24      }
[1564]25      return new XmlString(sb.ToString());
[1542]26    }
27
[1564]28    public override T Parse(XmlString x) {
[1625]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);
[1853]40      }
[1542]41    }
42  }
[1566]43
[1542]44}
Note: See TracBrowser for help on using the repository browser.