1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HEAL.Attic;
|
---|
7 | using HeuristicLab.Collections;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|