[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 |
|
---|
[17407] | 16 | public abstract void Inject(IItem item, IJsonItem data, IJsonItemConverter root);
|
---|
| 17 | public abstract IJsonItem Extract(IItem value, IJsonItemConverter root);
|
---|
[17394] | 18 |
|
---|
[17266] | 19 | #region Helper
|
---|
[17263] | 20 | protected ValueType CastValue<ValueType>(object obj) {
|
---|
| 21 | if (obj is JToken)
|
---|
[17394] | 22 | return ((JToken)obj).ToObject<ValueType>();
|
---|
[17266] | 23 | else if (obj is IConvertible)
|
---|
[17394] | 24 | return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
|
---|
[17266] | 25 | else return (ValueType)obj;
|
---|
[17263] | 26 | }
|
---|
| 27 |
|
---|
| 28 | protected IItem Instantiate(Type type, params object[] args) =>
|
---|
| 29 | (IItem)Activator.CreateInstance(type,args);
|
---|
| 30 |
|
---|
[17433] | 31 |
|
---|
[17353] | 32 | protected object GetMaxValue(Type t) {
|
---|
| 33 | TypeCode typeCode = Type.GetTypeCode(t);
|
---|
| 34 |
|
---|
[17404] | 35 | if (t.IsEqualTo(typeof(PercentValue)))
|
---|
[17353] | 36 | return 1.0d;
|
---|
| 37 |
|
---|
[17410] | 38 | if(t == typeof(IntValue)) {
|
---|
| 39 | return int.MaxValue;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[17353] | 42 | switch (typeCode) {
|
---|
[17379] | 43 | case TypeCode.Int16: return short.MaxValue;
|
---|
| 44 | case TypeCode.Int32: return int.MaxValue;
|
---|
| 45 | case TypeCode.Int64: return long.MaxValue;
|
---|
| 46 | case TypeCode.UInt16: return ushort.MaxValue;
|
---|
| 47 | case TypeCode.UInt32: return uint.MaxValue;
|
---|
| 48 | case TypeCode.UInt64: return ulong.MaxValue;
|
---|
| 49 | case TypeCode.Single: return float.MaxValue;
|
---|
| 50 | case TypeCode.Double: return double.MaxValue;
|
---|
| 51 | case TypeCode.Decimal: return decimal.MaxValue;
|
---|
| 52 | case TypeCode.Byte: return byte.MaxValue;
|
---|
[17353] | 53 | case TypeCode.Boolean: return true;
|
---|
| 54 | default: return GetDefaultValue(t);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected object GetMinValue(Type t) {
|
---|
| 59 | TypeCode typeCode = Type.GetTypeCode(t);
|
---|
| 60 |
|
---|
[17404] | 61 | if (t.IsEqualTo(typeof(PercentValue)))
|
---|
[17353] | 62 | return 0.0d;
|
---|
| 63 |
|
---|
[17410] | 64 | if (t == typeof(IntValue)) {
|
---|
| 65 | return int.MinValue;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[17353] | 68 | switch (typeCode) {
|
---|
[17379] | 69 | case TypeCode.Int16: return short.MinValue;
|
---|
| 70 | case TypeCode.Int32: return int.MinValue;
|
---|
| 71 | case TypeCode.Int64: return long.MinValue;
|
---|
| 72 | case TypeCode.UInt16: return ushort.MinValue;
|
---|
| 73 | case TypeCode.UInt32: return uint.MinValue;
|
---|
| 74 | case TypeCode.UInt64: return ulong.MinValue;
|
---|
| 75 | case TypeCode.Single: return float.MinValue;
|
---|
| 76 | case TypeCode.Double: return double.MinValue;
|
---|
| 77 | case TypeCode.Decimal: return decimal.MinValue;
|
---|
| 78 | case TypeCode.Byte: return byte.MinValue;
|
---|
[17353] | 79 | case TypeCode.Boolean: return false;
|
---|
| 80 | default: return GetDefaultValue(t);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
|
---|
[17266] | 85 | #endregion
|
---|
[17263] | 86 | }
|
---|
| 87 | }
|
---|