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

Last change on this file since 18043 was 18043, checked in by dpiringe, 11 months ago

#3026

  • code cleanup
File size: 3.9 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4
5namespace HeuristicLab.JsonInterface {
6
7  public class IntArrayConverter : BaseConverter {
8    public override int Priority => 1;
9
10    public override bool CanConvertType(Type t) =>
11      typeof(IntArray).IsAssignableFrom(t);
12
13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
14      IntArray arr = item as IntArray;
15      IntArrayJsonItem intArrayItem = data as IntArrayJsonItem;
16      bool resizeTmp = arr.Resizable;
17      arr.Resizable = true;
18      arr.Length = intArrayItem.Value.Length;
19      for (int i = 0; i < intArrayItem.Value.Length; ++i)
20        arr[i] = intArrayItem.Value[i];
21      arr.Resizable = resizeTmp;
22    }
23
24    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
25      new IntArrayJsonItem() {
26        Name = value.ItemName,
27        Description = value.ItemDescription,
28        Value = ((IntArray)value).CloneAsArray(),
29        Minimum = int.MinValue,
30        Maximum = int.MaxValue
31      };
32  }
33
34  public class DoubleArrayConverter : BaseConverter {
35    public override int Priority => 1;
36
37    public override bool CanConvertType(Type t) =>
38      typeof(DoubleArray).IsAssignableFrom(t);
39
40    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
41      DoubleArray arr = item as DoubleArray;
42      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
43      bool resizeTmp = arr.Resizable;
44      arr.Resizable = true;
45      arr.Length = doubleArrayItem.Value.Length;
46      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
47        arr[i] = doubleArrayItem.Value[i];
48      arr.Resizable = resizeTmp;
49    }
50
51    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
52      new DoubleArrayJsonItem() {
53        Name = value.ItemName,
54        Description = value.ItemDescription,
55        Value = ((DoubleArray)value).CloneAsArray(),
56        Minimum = double.MinValue,
57        Maximum = double.MaxValue
58      };
59  }
60
61  public class PercentArrayConverter : BaseConverter {
62    public override int Priority => 2;
63
64    public override bool CanConvertType(Type t) =>
65      typeof(PercentArray).IsAssignableFrom(t);
66
67    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
68      PercentArray arr = item as PercentArray;
69      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
70      bool resizeTmp = arr.Resizable;
71      arr.Resizable = true;
72      arr.Length = doubleArrayItem.Value.Length;
73      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
74        arr[i] = doubleArrayItem.Value[i];
75      arr.Resizable = resizeTmp;
76    }
77
78    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
79      new DoubleArrayJsonItem() {
80        Name = value.ItemName,
81        Description = value.ItemDescription,
82        Value = ((PercentArray)value).CloneAsArray(),
83        Minimum = 0.0d,
84        Maximum = 1.0d
85      };
86  }
87
88  public class BoolArrayConverter : BaseConverter {
89    public override int Priority => 1;
90
91    public override bool CanConvertType(Type t) =>
92      typeof(BoolArray).IsAssignableFrom(t);
93
94    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
95      BoolArray arr = item as BoolArray;
96      BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem;
97      bool resizeTmp = arr.Resizable;
98      arr.Resizable = true;
99      arr.Length = boolArrayItem.Value.Length;
100      for(int i = 0; i < boolArrayItem.Value.Length; ++i)
101        arr[i] = boolArrayItem.Value[i];
102      arr.Resizable = resizeTmp;
103    }
104
105    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
106      new BoolArrayJsonItem() {
107        Name = value.ItemName,
108        Description = value.ItemDescription,
109        Value = ((BoolArray)value).CloneAsArray()
110      };
111  }
112}
Note: See TracBrowser for help on using the repository browser.