Changeset 10896


Ignore:
Timestamp:
05/27/14 10:44:12 (5 years ago)
Author:
epitzer
Message:

#1802 make the Number2StringSerializer accept nullable numbers

Location:
trunk/sources
Files:
2 edited

Legend:

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

    r9456 r10896  
    7878    /// </returns>
    7979    public bool CanSerialize(Type type) {
    80       return numberSerializerMap.ContainsKey(type);
     80      return numberSerializerMap.ContainsKey(Nullable.GetUnderlyingType(type) ?? type);
    8181    }
    8282
     
    9090    /// </returns>
    9191    public string JustifyRejection(Type type) {
    92       return string.Format("not a number type (one of {0})",
     92      return string.Format("not a (nullable) number type (one of {0})",
    9393        string.Join(", ", numberSerializers.Select(n => n.SourceType.Name).ToArray()));
    9494    }
     
    100100    /// <returns></returns>
    101101    public string Format(object obj) {
    102       return ((XmlString)numberSerializerMap[obj.GetType()].Format(obj)).Data;
     102      if (obj == null) return "null";
     103      Type type = obj.GetType();
     104      return ((XmlString)numberSerializerMap[Nullable.GetUnderlyingType(type) ?? type].Format(obj)).Data;
    103105    }
    104106
     
    110112    /// <returns></returns>
    111113    public object Parse(string stringValue, Type type) {
     114      if (stringValue == "null") return null;
    112115      try {
    113         return numberSerializerMap[type].Parse(new XmlString(stringValue));
     116        return numberSerializerMap[Nullable.GetUnderlyingType(type) ?? type].Parse(new XmlString(stringValue));
    114117      }
    115118      catch (FormatException e) {
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCases.cs

    r10895 r10896  
    14641464    }
    14651465
     1466    [TestMethod]
     1467    [TestCategory("Persistence")]
     1468    [TestProperty("Time", "short")]
     1469    public void TestOptionalNumberEnumerable() {
     1470      var values = new List<double?> {0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1};
     1471      XmlGenerator.Serialize(values, tempFile);
     1472      var newValues = (List<double?>) XmlParser.Deserialize(tempFile);
     1473      CollectionAssert.AreEqual(values, newValues);
     1474    }
     1475
    14661476
    14671477    [ClassInitialize]
Note: See TracChangeset for help on using the changeset viewer.