Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17828 was 17828, checked in by dpiringe, 3 years 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
RevLine 
[17263]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
[17284]5namespace HeuristicLab.JsonInterface {
[17353]6  /// <summary>
[17394]7  /// Class for handling json converters.
[17353]8  /// </summary>
[17394]9  public class JsonItemConverter : IJsonItemConverter {
[17263]10   
[17394]11    #region Properties
12    private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
13      = new Dictionary<Type, IJsonItemConverter>();
[17263]14
[17439]15    private IDictionary<int, IJsonItem> InjectCache { get; set; }
[17406]16      = new Dictionary<int, IJsonItem>();
[17371]17
[17439]18    private IDictionary<int, IJsonItem> ExtractCache { get; set; }
19      = new Dictionary<int, IJsonItem>();
20
[17394]21    public int Priority => throw new NotImplementedException();
[17371]22
[17394]23    public Type ConvertableType => throw new NotImplementedException();
[17828]24
25    public bool CanConvertType(Type t) => throw new NotImplementedException();
[17394]26    #endregion
[17263]27
[17353]28    /// <summary>
[17394]29    /// GetConverter a converter for a specific type.
[17353]30    /// </summary>
31    /// <param name="type">The type for which the converter will be selected.</param>
32    /// <returns>An IJsonItemConverter object.</returns>
[17394]33    public IJsonItemConverter GetConverter(Type type) {
34      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
[17353]35     
[17828]36      foreach (var x in Converters) {
37        if (x.Value.CanConvertType(type))
[17282]38          possibleConverters.Add(x.Value);
[17828]39      }
40       
[17266]41
[17282]42      if(possibleConverters.Count > 0) {
[17394]43        IJsonItemConverter best = possibleConverters.First();
[17282]44        foreach (var x in possibleConverters) {
45          if (x.Priority > best.Priority)
46            best = x;
[17266]47        }
[17394]48        return best;
[17266]49      }
[17394]50      return null;
[17263]51    }
[17394]52   
[17406]53    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
[17439]54      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
[17394]55        IJsonItemConverter converter = GetConverter(item.GetType());
56        if(converter != null) converter.Inject(item, data, root);
[17439]57        InjectCache.Add(item.GetHashCode(), data);
[17394]58      }
59    }
[17263]60
[17406]61    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
[17394]62      int hash = item.GetHashCode();
[17439]63      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
[17394]64        return val;
65      else {
66        IJsonItemConverter converter = GetConverter(item.GetType());
[17828]67        if (converter == null)
68          return new UnsupportedJsonItem() { Name = $"{item.ItemName} (unsupported)" };
[17540]69        IJsonItem tmp = converter.Extract(item, root);
[17439]70        ExtractCache.Add(hash, tmp);
[17394]71        return tmp;
72      }
73    }
74   
[17406]75    public static void Inject(IItem item, IJsonItem data) {
[17394]76      IJsonItemConverter c = JsonItemConverterFactory.Create();
77      c.Inject(item, data, c);
78    }
[17263]79
[17406]80    public static IJsonItem Extract(IItem item) {
[17394]81      IJsonItemConverter c = JsonItemConverterFactory.Create();
82      return c.Extract(item, c);
83    }
[17263]84
[17353]85    /// <summary>
86    /// Static constructor for default converter configuration.
87    /// </summary>
[17394]88    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
89      Converters = converters;
[17263]90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.