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

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

#3026

  • code cleanup
File size: 1.0 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5
6namespace HeuristicLab.JsonInterface {
7  public class EnumTypeConverter : BaseConverter {
8    public override int Priority => 1;
9
10    public override bool CanConvertType(Type t) =>
11      typeof(EnumValue<>).IsAssignableFrom(t) ||
12      (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EnumValue<>));
13
14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
15      ((dynamic)item).Value = Enum.Parse(
16        item.GetType().GenericTypeArguments.First(),
17        ((StringJsonItem)data).Value);
18   
19    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
20      object val = ((dynamic)value).Value;
21      Type enumType = val.GetType();
22      return new StringJsonItem() {
23        Name = value.ItemName,
24        Description = value.ItemDescription,
25        Value = Enum.GetName(enumType, val),
26        ConcreteRestrictedItems = Enum.GetNames(enumType)
27      };
28    }
29  }
30}
Note: See TracBrowser for help on using the repository browser.