source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs @ 17828

Last change on this file since 17828 was 17828, checked in by dpiringe, 2 months ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
5namespace HeuristicLab.JsonInterface {
6  /// <summary>
7  /// Class for handling json converters.
8  /// </summary>
9  public class JsonItemConverter : IJsonItemConverter {
10   
11    #region Properties
12    private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
13      = new Dictionary<Type, IJsonItemConverter>();
14
15    private IDictionary<int, IJsonItem> InjectCache { get; set; }
16      = new Dictionary<int, IJsonItem>();
17
18    private IDictionary<int, IJsonItem> ExtractCache { get; set; }
19      = new Dictionary<int, IJsonItem>();
20
21    public int Priority => throw new NotImplementedException();
22
23    public Type ConvertableType => throw new NotImplementedException();
24
25    public bool CanConvertType(Type t) => throw new NotImplementedException();
26    #endregion
27
28    /// <summary>
29    /// GetConverter a converter for a specific type.
30    /// </summary>
31    /// <param name="type">The type for which the converter will be selected.</param>
32    /// <returns>An IJsonItemConverter object.</returns>
33    public IJsonItemConverter GetConverter(Type type) {
34      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
35     
36      foreach (var x in Converters) {
37        if (x.Value.CanConvertType(type))
38          possibleConverters.Add(x.Value);
39      }
40       
41
42      if(possibleConverters.Count > 0) {
43        IJsonItemConverter best = possibleConverters.First();
44        foreach (var x in possibleConverters) {
45          if (x.Priority > best.Priority)
46            best = x;
47        }
48        return best;
49      }
50      return null;
51    }
52   
53    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
54      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
55        IJsonItemConverter converter = GetConverter(item.GetType());
56        if(converter != null) converter.Inject(item, data, root);
57        InjectCache.Add(item.GetHashCode(), data);
58      }
59    }
60
61    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
62      int hash = item.GetHashCode();
63      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
64        return val;
65      else {
66        IJsonItemConverter converter = GetConverter(item.GetType());
67        if (converter == null)
68          return new UnsupportedJsonItem() { Name = $"{item.ItemName} (unsupported)" };
69        IJsonItem tmp = converter.Extract(item, root);
70        ExtractCache.Add(hash, tmp);
71        return tmp;
72      }
73    }
74   
75    public static void Inject(IItem item, IJsonItem data) {
76      IJsonItemConverter c = JsonItemConverterFactory.Create();
77      c.Inject(item, data, c);
78    }
79
80    public static IJsonItem Extract(IItem item) {
81      IJsonItemConverter c = JsonItemConverterFactory.Create();
82      return c.Extract(item, c);
83    }
84
85    /// <summary>
86    /// Static constructor for default converter configuration.
87    /// </summary>
88    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
89      Converters = converters;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.