Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs @ 17407

Last change on this file since 17407 was 17407, checked in by dpiringe, 4 years ago

#3026:

  • simplified converter inheritance:
    • BaseConverter now only has Inject and Extract from IJsonItemConverter as abstract methods
    • removed ParameterBaseConverter
    • concrete converters have to initialize their JsonItem now -> enables better handling with different types of JsonItem
File size: 2.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface {
11  public abstract class BaseConverter : IJsonItemConverter
12  {
13    public abstract int Priority { get; }
14    public abstract Type ConvertableType { get; }
15
16    public abstract void Inject(IItem item, IJsonItem data, IJsonItemConverter root);
17    public abstract IJsonItem Extract(IItem value, IJsonItemConverter root);
18
19    #region Helper
20    protected ValueType CastValue<ValueType>(object obj) {
21      if (obj is JToken)
22        return ((JToken)obj).ToObject<ValueType>();
23      else if (obj is IConvertible)
24        return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
25      else return (ValueType)obj;
26    }
27
28    protected IItem Instantiate(Type type, params object[] args) =>
29      (IItem)Activator.CreateInstance(type,args);
30
31    protected T Instantiate<T>(params object[] args) => (T)Instantiate(typeof(T), args);
32
33    protected object GetMaxValue(Type t) {
34      TypeCode typeCode = Type.GetTypeCode(t);
35
36      if (t.IsEqualTo(typeof(PercentValue)))
37        return 1.0d;
38
39      switch (typeCode) {
40        case TypeCode.Int16: return short.MaxValue;
41        case TypeCode.Int32: return int.MaxValue;
42        case TypeCode.Int64: return long.MaxValue;
43        case TypeCode.UInt16: return ushort.MaxValue;
44        case TypeCode.UInt32: return uint.MaxValue;
45        case TypeCode.UInt64: return ulong.MaxValue;
46        case TypeCode.Single: return float.MaxValue;
47        case TypeCode.Double: return double.MaxValue;
48        case TypeCode.Decimal: return decimal.MaxValue;
49        case TypeCode.Byte: return byte.MaxValue;
50        case TypeCode.Boolean: return true;
51        default: return GetDefaultValue(t);
52      }
53    }
54
55    protected object GetMinValue(Type t) {
56      TypeCode typeCode = Type.GetTypeCode(t);
57
58      if (t.IsEqualTo(typeof(PercentValue)))
59        return 0.0d;
60
61      switch (typeCode) {
62        case TypeCode.Int16: return short.MinValue;
63        case TypeCode.Int32: return int.MinValue;
64        case TypeCode.Int64: return long.MinValue;
65        case TypeCode.UInt16: return ushort.MinValue;
66        case TypeCode.UInt32: return uint.MinValue;
67        case TypeCode.UInt64: return ulong.MinValue;
68        case TypeCode.Single: return float.MinValue;
69        case TypeCode.Double: return double.MinValue;
70        case TypeCode.Decimal: return decimal.MinValue;
71        case TypeCode.Byte: return byte.MinValue;
72        case TypeCode.Boolean: return false;
73        default: return GetDefaultValue(t);
74      }
75    }
76
77    protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
78    #endregion
79  }
80}
Note: See TracBrowser for help on using the repository browser.