Free cookie consent management tool by TermsFeed Policy Generator

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

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

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

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