Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added initial VM (ArrayValueVM) and control for array values (JsonItemArrayValueControl)
  • new types of JsonItems for better type safety:
    • for arrays: DoubleArrayJsonItem, IntArrayJsonItem, BoolArrayJsonItem
    • for matrix: DoubleMatrixJsonItem, IntMatrixJsonItem, BoolMatrixJsonItem
  • refactored ValueTypeArrayConverter and ValueTypeMatrixConverter -> better type safety with new JsonItems
  • enhanced StringValueVM and implemented JsonItemValidValuesControl with MVVM architecture
  • the VM of JsonItemBaseControl is now protected (was private)
  • improved JsonItem<V,R> -> now handles JTokens correctly
File size: 3.2 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        Value = ((IntArray)value).CloneAsArray()
31      };
32  }
33
34  public class DoubleArrayConverter : BaseConverter {
35    public override int Priority => 1;
36    public override Type ConvertableType => typeof(DoubleArray);
37
38    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
39      DoubleArray arr = item as DoubleArray;
40      double[] d = CastValue<double[]>(data);
41      bool resizeTmp = arr.Resizable;
42      arr.Resizable = true;
43      arr.Length = d.Length;
44      for (int i = 0; i < d.Length; ++i)
45        arr[i] = d[i];
46      arr.Resizable = resizeTmp;
47    }
48
49    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
50      new DoubleArrayJsonItem() {
51        Name = "[OverridableParamName]",
52        Value = ((DoubleArray)value).CloneAsArray()
53      };
54  }
55
56  public class PercentArrayConverter : BaseConverter {
57    public override int Priority => 2;
58    public override Type ConvertableType => typeof(PercentArray);
59
60    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
61      PercentArray arr = item as PercentArray;
62      double[] d = CastValue<double[]>(data);
63      bool resizeTmp = arr.Resizable;
64      arr.Resizable = true;
65      arr.Length = d.Length;
66      for (int i = 0; i < d.Length; ++i)
67        arr[i] = d[i];
68      arr.Resizable = resizeTmp;
69    }
70
71    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
72      new DoubleArrayJsonItem() {
73        Name = "[OverridableParamName]",
74        Value = ((PercentArray)value).CloneAsArray()
75      };
76  }
77
78  public class BoolArrayConverter : BaseConverter {
79    public override int Priority => 1;
80    public override Type ConvertableType => typeof(BoolArray);
81
82    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
83      BoolArray arr = item as BoolArray;
84      bool[] d = CastValue<bool[]>(data);
85      bool resizeTmp = arr.Resizable;
86      arr.Resizable = true;
87      arr.Length = d.Length;
88      for(int i = 0; i < d.Length; ++i)
89        arr[i] = d[i];
90      arr.Resizable = resizeTmp;
91    }
92
93    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
94      new BoolArrayJsonItem() {
95        Name = "[OverridableParamName]",
96        Value = ((BoolArray)value).CloneAsArray()
97      };
98  }
99}
Note: See TracBrowser for help on using the repository browser.