Changeset 3811
- Timestamp:
- 05/14/10 21:23:51 (15 years ago)
- 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 30 30 using System.Text; 31 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Persistence.Default.Xml.Primitive; 33 using HeuristicLab.Persistence.Default.Xml; 32 34 33 35 namespace HeuristicLab.Persistence.Default.CompositeSerializers { … … 44 46 public sealed class Number2StringSerializer : ICompositeSerializer { 45 47 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 }; 63 63 64 64 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; 70 68 } 71 69 } … … 79 77 /// </returns> 80 78 public bool CanSerialize(Type type) { 81 return number Parsers.ContainsKey(type);79 return numberSerializerMap.ContainsKey(type); 82 80 } 83 81 … … 92 90 public string JustifyRejection(Type type) { 93 91 return string.Format("not a number type (one of {0})", 94 string.Join(", ", number Types.Select(n => n.Name).ToArray()));92 string.Join(", ", numberSerializers.Select(n => n.SourceType.Name).ToArray())); 95 93 } 96 94 … … 101 99 /// <returns></returns> 102 100 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; 110 102 } 111 103 … … 118 110 public object Parse(string stringValue, Type type) { 119 111 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)); 124 113 } catch (FormatException e) { 125 114 throw new PersistenceException("Invalid element data during number parsing.", e); -
trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs
r3742 r3811 1041 1041 } 1042 1042 } 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 } 1043 1056 1044 1057
Note: See TracChangeset
for help on using the changeset viewer.