Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • extended the BaseConverter class to set the type of the extracted value and removed the assignment of type in all other converters
  • fixed a bug in ConfigurableConverter -> now it correctly iterates through all items from an IEnumerable and calls the extract callback
  • MultiCheckedOperatorConverter:
    • deleted unnecessary code line
    • now adds operators to parameters (instead of operator list, because the operator list will get removed in a future commit)
    • does not set the path of its added items anymore
  • ParameterBaseConverter removed unnecessary code lines
  • deleted ParameterBaseConverter
  • reimplemented StorableConverter to test it again (still not really useable, because it bloats the template massively)
  • fixed a pathing bug in ValueParameterConverter (extended ValueRangeConverter, ValueTypeArrayConverter, ValueTypeMatrixConverter, ValueTypeValueConverter to archive this)
  • JCGenerator now only writes a single array with parameters (only FreeParameters) and saves a .hl File to test a new type of implementation of the whole JsonInterface
  • fixed a bug in JsonItem -> now it replaces the name in path of the item (not the whole path as it was before)
File size: 3.2 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      data.Type = string.IsNullOrEmpty(data.Type) ? value.GetType().AssemblyQualifiedName : data.Type;
24      return data;
25    }
26   
27    public abstract void InjectData(IItem item, JsonItem data);
28    public abstract JsonItem ExtractData(IItem value);
29
30    #region Helper
31    protected ValueType CastValue<ValueType>(object obj) {
32      if (obj is JToken)
33        return (obj.Cast<JToken>()).ToObject<ValueType>();
34      else if (obj is IConvertible)
35        return Convert.ChangeType(obj, typeof(ValueType)).Cast<ValueType>();
36      else return (ValueType)obj;
37    }
38
39    protected IItem Instantiate(Type type, params object[] args) =>
40      (IItem)Activator.CreateInstance(type,args);
41
42    protected T Instantiate<T>(params object[] args) => (T)Instantiate(typeof(T), args);
43
44    protected object GetMaxValue(Type t) {
45      TypeCode typeCode = Type.GetTypeCode(t);
46
47      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
48        return 1.0d;
49
50      switch (typeCode) {
51        case TypeCode.Int16: return Int16.MaxValue;
52        case TypeCode.Int32: return Int32.MaxValue;
53        case TypeCode.Int64: return Int64.MaxValue;
54        case TypeCode.UInt16: return UInt16.MaxValue;
55        case TypeCode.UInt32: return UInt32.MaxValue;
56        case TypeCode.UInt64: return UInt64.MaxValue;
57        case TypeCode.Single: return Single.MaxValue;
58        case TypeCode.Double: return Double.MaxValue;
59        case TypeCode.Decimal: return Decimal.MaxValue;
60        case TypeCode.Byte: return Byte.MaxValue;
61        case TypeCode.Boolean: return true;
62        default: return GetDefaultValue(t);
63      }
64    }
65
66    protected object GetMinValue(Type t) {
67      TypeCode typeCode = Type.GetTypeCode(t);
68
69      if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
70        return 0.0d;
71
72      switch (typeCode) {
73        case TypeCode.Int16: return Int16.MinValue;
74        case TypeCode.Int32: return Int32.MinValue;
75        case TypeCode.Int64: return Int64.MinValue;
76        case TypeCode.UInt16: return UInt16.MinValue;
77        case TypeCode.UInt32: return UInt32.MinValue;
78        case TypeCode.UInt64: return UInt64.MinValue;
79        case TypeCode.Single: return Single.MinValue;
80        case TypeCode.Double: return Double.MinValue;
81        case TypeCode.Decimal: return Decimal.MinValue;
82        case TypeCode.Byte: return Byte.MinValue;
83        case TypeCode.Boolean: return false;
84        default: return GetDefaultValue(t);
85      }
86    }
87
88    protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
89
90    #endregion
91  }
92}
Note: See TracBrowser for help on using the repository browser.