Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
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 abstract int Priority { get; }
14    public abstract Type ConvertableType { get; }
15
16    public void Inject(IItem item, JsonItem data, IJsonItemConverter root) {
17
18      InjectData(item, data, root);
19    }
20
21    public JsonItem Extract(IItem value, IJsonItemConverter root) {
22      JsonItem data = new JsonItem() { Name = value.ItemName };
23      Populate(value, data, root);
24      return data;
25    }
26   
27    public abstract void InjectData(IItem item, JsonItem data, IJsonItemConverter root);
28    public abstract void Populate(IItem value, JsonItem item, IJsonItemConverter root);
29
30    #region Helper
31    protected ValueType CastValue<ValueType>(object obj) {
32      if (obj is JToken)
33        return ((JToken)obj).ToObject<ValueType>();
34      else if (obj is IConvertible)
35        return (ValueType)Convert.ChangeType(obj, typeof(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 short.MaxValue;
52        case TypeCode.Int32: return int.MaxValue;
53        case TypeCode.Int64: return long.MaxValue;
54        case TypeCode.UInt16: return ushort.MaxValue;
55        case TypeCode.UInt32: return uint.MaxValue;
56        case TypeCode.UInt64: return ulong.MaxValue;
57        case TypeCode.Single: return float.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 short.MinValue;
74        case TypeCode.Int32: return int.MinValue;
75        case TypeCode.Int64: return long.MinValue;
76        case TypeCode.UInt16: return ushort.MinValue;
77        case TypeCode.UInt32: return uint.MinValue;
78        case TypeCode.UInt64: return ulong.MinValue;
79        case TypeCode.Single: return float.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    #endregion
90  }
91}
Note: See TracBrowser for help on using the repository browser.