Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs @ 17407

Last change on this file since 17407 was 17407, checked in by dpiringe, 4 years ago

#3026:

  • simplified converter inheritance:
    • BaseConverter now only has Inject and Extract from IJsonItemConverter as abstract methods
    • removed ParameterBaseConverter
    • concrete converters have to initialize their JsonItem now -> enables better handling with different types of JsonItem
File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface {
11
12  public class IntArrayConverter : ValueTypeArrayConverter<IntArray, int> {
13    public override int Priority => 1;
14    public override Type ConvertableType => typeof(IntArray);
15  }
16
17  public class DoubleArrayConverter : ValueTypeArrayConverter<DoubleArray, double> {
18    public override int Priority => 1;
19    public override Type ConvertableType => typeof(DoubleArray);
20  }
21
22  public class PercentArrayConverter : ValueTypeArrayConverter<PercentArray, double> {
23    public override int Priority => 2;
24    public override Type ConvertableType => typeof(PercentArray);
25  }
26
27  public class BoolArrayConverter : ValueTypeArrayConverter<BoolArray, bool> {
28    public override int Priority => 1;
29    public override Type ConvertableType => typeof(BoolArray);
30  }
31
32  public abstract class ValueTypeArrayConverter<ArrayType, T> : BaseConverter
33    where ArrayType : ValueTypeArray<T>
34    where T : struct
35  {
36    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
37      CopyArrayData(((ArrayType)item), CastValue<T[]>(data.Value));
38
39    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
40      new JsonItem() {
41        Name = "[OverridableParamName]",
42        Value = ((ArrayType)value).CloneAsArray()
43      };
44
45    #region Helper
46    private void CopyArrayData(ArrayType array, T[] data) {
47      var colInfo = array.GetType().GetProperty("Length");
48      colInfo.SetValue(array, data.Length);
49      for (int i = 0; i < data.Length; ++i) {
50        array[i] = data[i];
51      }
52    }
53    #endregion
54  }
55}
Note: See TracBrowser for help on using the repository browser.