Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs @ 17399

Last change on this file since 17399 was 17394, checked in by dpiringe, 5 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: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10
11namespace HeuristicLab.JsonInterface {
12
13  public class IntRangeConverter : ValueRangeConverter<IntRange, IntValue, int> {
14    public override int Priority => 1;
15    public override Type ConvertableType => typeof(IntRange);
16  }
17  public class DoubleRangeConverter : ValueRangeConverter<DoubleRange, DoubleValue, double> {
18    public override int Priority => 1;
19    public override Type ConvertableType => typeof(DoubleRange);
20  }
21
22  public abstract class ValueRangeConverter<RangeType, T, TType> : BaseConverter
23    where RangeType : StringConvertibleValueTuple<T, T>
24    where T : ValueTypeValue<TType>, IDeepCloneable, IStringConvertibleValue
25    where TType : struct {
26
27    private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
28
29    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
30      var field = value.GetType().GetField("values", Flags);
31      Tuple<T,T> tuple = (Tuple<T,T>)field.GetValue(value);
32      item.Name = "[OverridableParamName]";
33      item.Value = new object[] { tuple.Item1.Value, tuple.Item2.Value };
34      item.Range = new object[] { GetMinValue(tuple.Item1.Value.GetType()), GetMaxValue(tuple.Item2.Value.GetType()) };
35    }
36
37    public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) {
38      object[] arr = (object[])data.Value;
39      Tuple<T,T> tuple = new Tuple<T,T>(Instantiate<T>(arr[0]), Instantiate<T>(arr[1]));
40      var field = item.GetType().GetField("values", Flags);
41      field.SetValue(tuple, item);
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.