Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ItemCollectionConverter.cs @ 18057

Last change on this file since 18057 was 18055, checked in by dpiringe, 3 years ago

#3026

  • added StorableTypeAttribute and StorableConstructorAttribute to all JsonItems
  • added a new JsonItem ListJsonItem + Interfaces IListJsonItem
  • renamed SymRegPythonProcessor to RunCollectionSRSolutionPythonFormatter
  • removed Interface IResultCollectionProcessor -> using the interface IRunCollectionModifier now (has aleady implementations)
  • renamed all related variables/fields/properties with a connection to ResultCollectionProcessor
  • added new implementations for IRunCollectionModifier
File size: 1.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HEAL.Attic;
7using HeuristicLab.Collections;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.JsonInterface {
11  public class ItemCollectionConverter : BaseConverter {
12    public override int Priority => 5;
13
14    public override bool CanConvertType(Type t) =>
15      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IItemCollection<>));
16
17    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
18      var collection = (dynamic)value;
19      var item = new ListJsonItem();
20      IList<IJsonItem> valueItems = new List<IJsonItem>();
21
22      foreach(IItem i in collection) {
23        var tmp = root.Extract(i, root);
24        valueItems.Add(tmp);
25      }
26      if (valueItems.Count > 0) {
27        var targetType = valueItems.First().GetType();
28        item.TargetTypeGUID = StorableTypeAttribute.GetStorableTypeAttribute(targetType).Guid.ToString();
29      }
30
31      item.Value = valueItems.ToArray();
32      return item;
33    }
34
35    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
36      var collection = (dynamic)item;
37      var listItem = data as ListJsonItem;
38      var genericType = item.GetType().GetGenericArguments().First();
39
40      foreach (var i in listItem.Value) {
41        var tmp = Instantiate(genericType);
42        root.Inject(tmp, i, root);
43        collection.Add((dynamic)tmp);
44      }
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.