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 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); // TODO: populate?
|
---|
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 | }
|
---|