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

Last change on this file since 17353 was 17353, checked in by dpiringe, 11 months ago

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File size: 3.1 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 void Inject(IItem item, JsonItem data) {
14      if (data.Reference != null) {
15        JsonItem.Merge(data, data.Reference);
16      }
17      InjectData(item, data);
18    }
19
20    public JsonItem Extract(IItem value) {
21      JsonItem data = ExtractData(value);
22      data.Name = String.IsNullOrEmpty(data.Name) ? value.ItemName : data.Name;
23      return data;
24    }
25   
26    public abstract void InjectData(IItem item, JsonItem data);
27    public abstract JsonItem ExtractData(IItem value);
28
29    #region Helper
30    protected ValueType CastValue<ValueType>(object obj) {
31      if (obj is JToken)
32        return (obj.Cast<JToken>()).ToObject<ValueType>();
33      else if (obj is IConvertible)
34        return Convert.ChangeType(obj, typeof(ValueType)).Cast<ValueType>();
35      else return (ValueType)obj;
36    }
37
38    protected IItem Instantiate(Type type, params object[] args) =>
39      (IItem)Activator.CreateInstance(type,args);
40
41    protected T Instantiate<T>(params object[] args) => (T)Instantiate(typeof(T), args);
42
43    protected object GetMaxValue(Type t) {
44      TypeCode typeCode = Type.GetTypeCode(t);
45
46      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
47        return 1.0d;
48
49      switch (typeCode) {
50        case TypeCode.Int16: return Int16.MaxValue;
51        case TypeCode.Int32: return Int32.MaxValue;
52        case TypeCode.Int64: return Int64.MaxValue;
53        case TypeCode.UInt16: return UInt16.MaxValue;
54        case TypeCode.UInt32: return UInt32.MaxValue;
55        case TypeCode.UInt64: return UInt64.MaxValue;
56        case TypeCode.Single: return Single.MaxValue;
57        case TypeCode.Double: return Double.MaxValue;
58        case TypeCode.Decimal: return Decimal.MaxValue;
59        case TypeCode.Byte: return Byte.MaxValue;
60        case TypeCode.Boolean: return true;
61        default: return GetDefaultValue(t);
62      }
63    }
64
65    protected object GetMinValue(Type t) {
66      TypeCode typeCode = Type.GetTypeCode(t);
67
68      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
69        return 0.0d;
70
71      switch (typeCode) {
72        case TypeCode.Int16: return Int16.MinValue;
73        case TypeCode.Int32: return Int32.MinValue;
74        case TypeCode.Int64: return Int64.MinValue;
75        case TypeCode.UInt16: return UInt16.MinValue;
76        case TypeCode.UInt32: return UInt32.MinValue;
77        case TypeCode.UInt64: return UInt64.MinValue;
78        case TypeCode.Single: return Single.MinValue;
79        case TypeCode.Double: return Double.MinValue;
80        case TypeCode.Decimal: return Decimal.MinValue;
81        case TypeCode.Byte: return Byte.MinValue;
82        case TypeCode.Boolean: return false;
83        default: return GetDefaultValue(t);
84      }
85    }
86
87    protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
88    #endregion
89  }
90}
Note: See TracBrowser for help on using the repository browser.