Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3811


Ignore:
Timestamp:
05/14/10 21:23:51 (15 years ago)
Author:
epitzer
Message:

correct and speed-up serialization for number enumerables (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs

    r3742 r3811  
    3030using System.Text;
    3131using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.Persistence.Default.Xml.Primitive;
     33using HeuristicLab.Persistence.Default.Xml;
    3234
    3335namespace HeuristicLab.Persistence.Default.CompositeSerializers {
     
    4446  public sealed class Number2StringSerializer : ICompositeSerializer {
    4547
    46     private static readonly List<Type> numberTypes =
    47       new List<Type> {
    48         typeof(bool),
    49         typeof(byte),
    50         typeof(sbyte),
    51         typeof(short),
    52         typeof(ushort),
    53         typeof(int),
    54         typeof(uint),
    55         typeof(long),
    56         typeof(ulong),
    57         typeof(float),
    58         typeof(double),
    59         typeof(decimal),
    60       };
    61 
    62     private static readonly Dictionary<Type, MethodInfo> numberParsers;
     48    private static readonly Dictionary<Type, IPrimitiveSerializer> numberSerializerMap;
     49    private static readonly List<IPrimitiveSerializer> numberSerializers = new List<IPrimitiveSerializer> {
     50      new Bool2XmlSerializer(),
     51      new Byte2XmlSerializer(),
     52      new SByte2XmlSerializer(),
     53      new Short2XmlSerializer(),
     54      new UShort2XmlSerializer(),
     55      new Int2XmlSerializer(),
     56      new UInt2XmlSerializer(),
     57      new Long2XmlSerializer(),
     58      new ULong2XmlSerializer(),
     59      new Float2XmlSerializer(),
     60      new Double2XmlSerializer(),
     61      new Decimal2XmlSerializer(),
     62    };
    6363
    6464    static Number2StringSerializer() {
    65       numberParsers = new Dictionary<Type, MethodInfo>();
    66       foreach (var type in numberTypes) {
    67         numberParsers[type] = type
    68           .GetMethod("Parse", BindingFlags.Static | BindingFlags.Public,
    69                      null, new[] { typeof(string) }, null);
     65      numberSerializerMap = new Dictionary<Type,IPrimitiveSerializer>();
     66      foreach (var s in numberSerializers) {
     67        numberSerializerMap[s.SourceType] = s;
    7068      }
    7169    }
     
    7977    /// </returns>
    8078    public bool CanSerialize(Type type) {
    81       return numberParsers.ContainsKey(type);
     79      return numberSerializerMap.ContainsKey(type);
    8280    }
    8381
     
    9290    public string JustifyRejection(Type type) {
    9391      return string.Format("not a number type (one of {0})",
    94         string.Join(", ", numberTypes.Select(n => n.Name).ToArray()));
     92        string.Join(", ", numberSerializers.Select(n => n.SourceType.Name).ToArray()));
    9593    }
    9694
     
    10199    /// <returns></returns>
    102100    public string Format(object obj) {
    103       if (obj.GetType() == typeof(float))
    104         return ((float)obj).ToString("r", CultureInfo.InvariantCulture);
    105       if (obj.GetType() == typeof(double))
    106         return ((double)obj).ToString("r", CultureInfo.InvariantCulture);
    107       if (obj.GetType() == typeof(decimal))
    108         return ((decimal)obj).ToString("r", CultureInfo.InvariantCulture);
    109       return obj.ToString();
     101      return ((XmlString)numberSerializerMap[obj.GetType()].Format(obj)).Data;
    110102    }
    111103
     
    118110    public object Parse(string stringValue, Type type) {
    119111      try {
    120         return numberParsers[type]
    121           .Invoke(null,
    122               BindingFlags.Static | BindingFlags.PutRefDispProperty,
    123                     null, new[] { stringValue }, CultureInfo.InvariantCulture);
     112        return numberSerializerMap[type].Parse(new XmlString(stringValue));
    124113      } catch (FormatException e) {
    125114        throw new PersistenceException("Invalid element data during number parsing.", e);
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r3742 r3811  
    10411041      }
    10421042    }
     1043
     1044    [TestMethod]
     1045    public void TestSpecialNumbers() {
     1046      List<double> specials = new List<double>() { 1.0 / 0, -1.0 / 0, 0.0 / 0 };
     1047      Assert.IsTrue(double.IsPositiveInfinity(specials[0]));
     1048      Assert.IsTrue(double.IsNegativeInfinity(specials[1]));
     1049      Assert.IsTrue(double.IsNaN(specials[2]));
     1050      XmlGenerator.Serialize(specials, tempFile);
     1051      List<double> newSpecials = XmlParser.Deserialize<List<double>>(tempFile);
     1052      Assert.IsTrue(double.IsPositiveInfinity(newSpecials[0]));
     1053      Assert.IsTrue(double.IsNegativeInfinity(newSpecials[1]));
     1054      Assert.IsTrue(double.IsNaN(newSpecials[2]));
     1055    }
    10431056   
    10441057
Note: See TracChangeset for help on using the changeset viewer.