Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeValueConverter.cs @ 17394

Last change on this file since 17394 was 17394, checked in by dpiringe, 4 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.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8
9namespace HeuristicLab.JsonInterface {
10
11  public class IntValueConverter : ValueTypeValueConverter<IntValue, int> {
12    public override int Priority => 1;
13    public override Type ConvertableType => typeof(IntValue);
14  }
15
16  public class DoubleValueConverter : ValueTypeValueConverter<DoubleValue, double> {
17    public override int Priority => 1;
18    public override Type ConvertableType => typeof(DoubleValue);
19  }
20
21  public class PercentValueConverter : ValueTypeValueConverter<PercentValue, double> {
22    public override int Priority => 2;
23    public override Type ConvertableType => typeof(PercentValue);
24  }
25
26  public class BoolValueConverter : ValueTypeValueConverter<BoolValue, bool> {
27    public override int Priority => 1;
28    public override Type ConvertableType => typeof(BoolValue);
29  }
30
31  public class DateTimeValueConverter : ValueTypeValueConverter<DateTimeValue, DateTime> {
32    public override int Priority => 1;
33    public override Type ConvertableType => typeof(DateTimeValue);
34  }
35
36  public abstract class ValueTypeValueConverter<ValueType, T> : BaseConverter
37    where ValueType : ValueTypeValue<T>
38    where T : struct {
39
40    public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) =>
41      ((ValueType)item).Value = CastValue<T>(data.Value);
42
43    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
44      item.Name = "[OverridableParamName]";
45      item.Value = ((ValueType)value).Value;
46      item.Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T)) };
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.