1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using HeuristicLab.Persistence.Core;
|
---|
5 | using HeuristicLab.Persistence.Auxiliary;
|
---|
6 | using System.Collections.Generic;
|
---|
7 | using System.Reflection;
|
---|
8 | using System.Globalization;
|
---|
9 | using System.Text;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace 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 | } |
---|