Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added property Description in IJsonItem and updated all construction calls
  • updated UnsupportedJsonItem with unsupported property Description
  • updated JsonItemBaseControl and JsonItemVMBase for new property Description
File size: 3.6 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 : BaseConverter {
13    public override int Priority => 1;
14    public override Type ConvertableType => typeof(IntArray);
15
16    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
17      IntArray arr = item as IntArray;
18      int[] d = CastValue<int[]>(data);
19      bool resizeTmp = arr.Resizable;
20      arr.Resizable = true;
21      arr.Length = d.Length;
22      for (int i = 0; i < d.Length; ++i)
23        arr[i] = d[i];
24      arr.Resizable = resizeTmp;
25    }
26
27    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
28      new IntArrayJsonItem() {
29        Name = "[OverridableParamName]",
30        Description = value.ItemDescription,
31        Value = ((IntArray)value).CloneAsArray(),
32        Range = new int[] { int.MinValue, int.MaxValue }
33      };
34  }
35
36  public class DoubleArrayConverter : BaseConverter {
37    public override int Priority => 1;
38    public override Type ConvertableType => typeof(DoubleArray);
39
40    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
41      DoubleArray arr = item as DoubleArray;
42      double[] d = CastValue<double[]>(data);
43      bool resizeTmp = arr.Resizable;
44      arr.Resizable = true;
45      arr.Length = d.Length;
46      for (int i = 0; i < d.Length; ++i)
47        arr[i] = d[i];
48      arr.Resizable = resizeTmp;
49    }
50
51    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
52      new DoubleArrayJsonItem() {
53        Name = "[OverridableParamName]",
54        Description = value.ItemDescription,
55        Value = ((DoubleArray)value).CloneAsArray(),
56        Range = new double[] { double.MinValue, double.MaxValue }
57      };
58  }
59
60  public class PercentArrayConverter : BaseConverter {
61    public override int Priority => 2;
62    public override Type ConvertableType => typeof(PercentArray);
63
64    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
65      PercentArray arr = item as PercentArray;
66      double[] d = CastValue<double[]>(data);
67      bool resizeTmp = arr.Resizable;
68      arr.Resizable = true;
69      arr.Length = d.Length;
70      for (int i = 0; i < d.Length; ++i)
71        arr[i] = d[i];
72      arr.Resizable = resizeTmp;
73    }
74
75    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
76      new DoubleArrayJsonItem() {
77        Name = "[OverridableParamName]",
78        Description = value.ItemDescription,
79        Value = ((PercentArray)value).CloneAsArray(),
80        Range = new double[] { 0.0d, 1.0d }
81      };
82  }
83
84  public class BoolArrayConverter : BaseConverter {
85    public override int Priority => 1;
86    public override Type ConvertableType => typeof(BoolArray);
87
88    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
89      BoolArray arr = item as BoolArray;
90      bool[] d = CastValue<bool[]>(data);
91      bool resizeTmp = arr.Resizable;
92      arr.Resizable = true;
93      arr.Length = d.Length;
94      for(int i = 0; i < d.Length; ++i)
95        arr[i] = d[i];
96      arr.Resizable = resizeTmp;
97    }
98
99    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
100      new BoolArrayJsonItem() {
101        Name = "[OverridableParamName]",
102        Description = value.ItemDescription,
103        Value = ((BoolArray)value).CloneAsArray(),
104        Range = new bool[] { false, true }
105      };
106  }
107}
Note: See TracBrowser for help on using the repository browser.