Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.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: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Newtonsoft.Json.Linq;
7using HeuristicLab.Data;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.JsonInterface {
11  public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
12    public override int Priority => 1;
13    public override Type ConvertableType => typeof(IntMatrix);
14  }
15
16  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
17    public override int Priority => 1;
18    public override Type ConvertableType => typeof(DoubleMatrix);
19  }
20
21  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
22    public override int Priority => 2;
23    public override Type ConvertableType => typeof(PercentMatrix);
24  }
25
26  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
27    public override int Priority => 1;
28    public override Type ConvertableType => typeof(BoolMatrix);
29  }
30
31  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
32    where MatrixType : ValueTypeMatrix<T>
33    where T : struct
34  {
35    public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) =>
36      CopyMatrixData(item as MatrixType, data.Value);
37
38    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
39      item.Name = "[OverridableParamName]";
40      item.Value = ((MatrixType)value).CloneAsMatrix();
41    }
42
43    #region Helper
44    private void CopyMatrixData(MatrixType matrix, object data) {
45      if (data is Array arr) {
46        var rows = arr.Length;
47        var cols = arr.Length > 0 && arr.GetValue(0) is JArray jarr ? jarr.Count : 0;
48
49        var rowInfo = matrix.GetType().GetProperty("Rows");
50        rowInfo.SetValue(matrix, rows);
51        var colInfo = matrix.GetType().GetProperty("Columns");
52        colInfo.SetValue(matrix, cols);
53
54        for (int x = 0; x < rows; ++x) {
55          jarr = (JArray)arr.GetValue(x);
56          for (int y = 0; y < cols; ++y) {
57            matrix[x, y] = jarr[y].ToObject<T>();
58          }
59        }
60      }
61    }
62    #endregion
63  }
64}
Note: See TracBrowser for help on using the repository browser.