1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using Newtonsoft.Json.Linq;
|
---|
9 |
|
---|
10 | namespace 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 |
|
---|
37 | if (t.IsEqualTo(typeof(PercentValue)))
|
---|
38 | return 1.0d;
|
---|
39 |
|
---|
40 | if(t == typeof(IntValue)) {
|
---|
41 | return int.MaxValue;
|
---|
42 | }
|
---|
43 |
|
---|
44 | switch (typeCode) {
|
---|
45 | case TypeCode.Int16: return short.MaxValue;
|
---|
46 | case TypeCode.Int32: return int.MaxValue;
|
---|
47 | case TypeCode.Int64: return long.MaxValue;
|
---|
48 | case TypeCode.UInt16: return ushort.MaxValue;
|
---|
49 | case TypeCode.UInt32: return uint.MaxValue;
|
---|
50 | case TypeCode.UInt64: return ulong.MaxValue;
|
---|
51 | case TypeCode.Single: return float.MaxValue;
|
---|
52 | case TypeCode.Double: return double.MaxValue;
|
---|
53 | case TypeCode.Decimal: return decimal.MaxValue;
|
---|
54 | case TypeCode.Byte: return byte.MaxValue;
|
---|
55 | case TypeCode.Boolean: return true;
|
---|
56 | default: return GetDefaultValue(t);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected object GetMinValue(Type t) {
|
---|
61 | TypeCode typeCode = Type.GetTypeCode(t);
|
---|
62 |
|
---|
63 | if (t.IsEqualTo(typeof(PercentValue)))
|
---|
64 | return 0.0d;
|
---|
65 |
|
---|
66 | if (t == typeof(IntValue)) {
|
---|
67 | return int.MinValue;
|
---|
68 | }
|
---|
69 |
|
---|
70 | switch (typeCode) {
|
---|
71 | case TypeCode.Int16: return short.MinValue;
|
---|
72 | case TypeCode.Int32: return int.MinValue;
|
---|
73 | case TypeCode.Int64: return long.MinValue;
|
---|
74 | case TypeCode.UInt16: return ushort.MinValue;
|
---|
75 | case TypeCode.UInt32: return uint.MinValue;
|
---|
76 | case TypeCode.UInt64: return ulong.MinValue;
|
---|
77 | case TypeCode.Single: return float.MinValue;
|
---|
78 | case TypeCode.Double: return double.MinValue;
|
---|
79 | case TypeCode.Decimal: return decimal.MinValue;
|
---|
80 | case TypeCode.Byte: return byte.MinValue;
|
---|
81 | case TypeCode.Boolean: return false;
|
---|
82 | default: return GetDefaultValue(t);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
|
---|
87 | #endregion
|
---|
88 | }
|
---|
89 | }
|
---|