Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs @ 3025

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

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 3.5 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Persistence.Interfaces;
4using HeuristicLab.Persistence.Core;
5using HeuristicLab.Persistence.Auxiliary;
6using System.Collections.Generic;
7using System.Reflection;
8using System.Globalization;
9using System.Text;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Persistence.Default.CompositeSerializers {
13
14  [StorableClass]
15  public class Number2StringSerializer : ICompositeSerializer {
16
17    private static readonly List<Type> numberTypes =
18      new List<Type> {
19        typeof(bool),
20        typeof(byte),
21        typeof(sbyte),
22        typeof(short),
23        typeof(ushort),
24        typeof(int),
25        typeof(uint),
26        typeof(long),
27        typeof(ulong),
28        typeof(float),
29        typeof(double),
30        typeof(decimal),
31      };
32
33    private static readonly Dictionary<Type, MethodInfo> numberParsers;
34
35    static Number2StringSerializer() {
36      numberParsers = new Dictionary<Type, MethodInfo>();
37      foreach (var type in numberTypes) {
38        numberParsers[type] = type
39          .GetMethod("Parse", BindingFlags.Static | BindingFlags.Public,
40                     null, new[] { typeof(string) }, null);
41      }
42    }
43
44    public bool CanSerialize(Type type) {
45      return numberParsers.ContainsKey(type);
46    }
47
48    public string JustifyRejection(Type type) {
49      return string.Format("not a number type (one of {0})",
50        string.Join(", ", numberTypes.Select(n => n.Name).ToArray()));
51    }
52
53    public string Format(object obj) {
54      if (obj.GetType() == typeof(float))
55        return ((float)obj).ToString("r", CultureInfo.InvariantCulture);
56      if (obj.GetType() == typeof(double))
57        return ((double)obj).ToString("r", CultureInfo.InvariantCulture);
58      if (obj.GetType() == typeof(decimal))
59        return ((decimal)obj).ToString("r", CultureInfo.InvariantCulture);
60      return obj.ToString();
61    }
62
63    public object Parse(string stringValue, Type type) {
64      try {
65        return numberParsers[type]
66          .Invoke(null,
67              BindingFlags.Static | BindingFlags.PutRefDispProperty,
68                    null, new[] { stringValue }, CultureInfo.InvariantCulture);
69      } catch (FormatException e) {
70        throw new PersistenceException("Invalid element data during number parsing.", e);
71      } catch (OverflowException e) {
72        throw new PersistenceException("Overflow during number parsing.", e);
73      }
74    }
75
76
77    public int Priority {
78      get { return -100; }
79    }
80
81    public IEnumerable<Tag> CreateMetaInfo(object obj) {
82      yield return new Tag(Format(obj));
83    }
84
85    public IEnumerable<Tag> Decompose(object obj) {
86      // numbers are composed just of meta info
87      return new Tag[] { };
88    }
89
90    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
91      var it = metaInfo.GetEnumerator();
92      try {
93        it.MoveNext();
94        return Parse((string)it.Current.Value, type);
95      } catch (InvalidOperationException e) {
96        throw new PersistenceException(
97          String.Format("Insufficient meta information to reconstruct number of type {0}.",
98          type.VersionInvariantName()), e);
99      } catch (InvalidCastException e) {
100        throw new PersistenceException("Invalid meta information element type", e);
101      }
102    }
103
104    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
105      // numbers are composed just of meta info, no need to populate
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.