Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18183 was 18077, checked in by dpiringe, 3 years ago

#3026

  • added the dockerhub readme file
  • fixed a bug which caused changed values (changed by events) to be overwritten with wrong values
File size: 1.6 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        if (data.Active) {
43          i.Active = true;
44          root.Inject(tmp, i, root);
45          collection.Add((dynamic)tmp);
46        }
47      }
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.