Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs @ 2115

Last change on this file since 2115 was 1853, checked in by epitzer, 16 years ago

Fix EmptyStorableClass attributes. (#603)

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