[17263] | 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;
|
---|
[17353] | 7 | using HeuristicLab.Data;
|
---|
[17263] | 8 | using Newtonsoft.Json.Linq;
|
---|
| 9 |
|
---|
[17284] | 10 | namespace HeuristicLab.JsonInterface {
|
---|
[17281] | 11 | public abstract class BaseConverter : IJsonItemConverter
|
---|
[17263] | 12 | {
|
---|
[17394] | 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);
|
---|
[17275] | 19 | }
|
---|
[17266] | 20 |
|
---|
[17394] | 21 | public JsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
| 22 | JsonItem data = new JsonItem() { Name = value.ItemName };
|
---|
| 23 | Populate(value, data, root);
|
---|
[17353] | 24 | return data;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[17394] | 27 | public abstract void InjectData(IItem item, JsonItem data, IJsonItemConverter root);
|
---|
[17395] | 28 | public abstract void Populate(IItem value, JsonItem item, IJsonItemConverter root); // TODO: populate?
|
---|
[17266] | 29 |
|
---|
| 30 | #region Helper
|
---|
[17263] | 31 | protected ValueType CastValue<ValueType>(object obj) {
|
---|
| 32 | if (obj is JToken)
|
---|
[17394] | 33 | return ((JToken)obj).ToObject<ValueType>();
|
---|
[17266] | 34 | else if (obj is IConvertible)
|
---|
[17394] | 35 | return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
|
---|
[17266] | 36 | else return (ValueType)obj;
|
---|
[17263] | 37 | }
|
---|
| 38 |
|
---|
| 39 | protected IItem Instantiate(Type type, params object[] args) =>
|
---|
| 40 | (IItem)Activator.CreateInstance(type,args);
|
---|
| 41 |
|
---|
[17353] | 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) {
|
---|
[17379] | 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;
|
---|
[17353] | 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) {
|
---|
[17379] | 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;
|
---|
[17353] | 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;
|
---|
[17266] | 89 | #endregion
|
---|
[17263] | 90 | }
|
---|
| 91 | }
|
---|