Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17519 was 17519, checked in by dpiringe, 4 years ago

#3026:

  • added error output for failed runner initialization
  • reorganised some final view models
  • TargetedJsonItemType (in JsonItemVMBase) now automatically returns the type of the defined JsonItem
  • code cleanup
  • refactored RegressionProblemDataConverter
  • added lots of comments
  • added new view for StringArrayJsonItem
  • added new UI component for concrete restricted items and used it in JsonItemConcreteItemArrayControl and JsonItemValidValuesControl
File size: 3.4 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();
24    #endregion
[17263]25
[17353]26    /// <summary>
[17394]27    /// GetConverter a converter for a specific type.
[17353]28    /// </summary>
29    /// <param name="type">The type for which the converter will be selected.</param>
30    /// <returns>An IJsonItemConverter object.</returns>
[17394]31    public IJsonItemConverter GetConverter(Type type) {
32      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
[17353]33     
34      foreach (var x in Converters)
[17478]35        if (CompareTypes(type, x.Key))
[17282]36          possibleConverters.Add(x.Value);
[17266]37
[17282]38      if(possibleConverters.Count > 0) {
[17394]39        IJsonItemConverter best = possibleConverters.First();
[17282]40        foreach (var x in possibleConverters) {
41          if (x.Priority > best.Priority)
42            best = x;
[17266]43        }
[17394]44        return best;
[17266]45      }
[17394]46      return null;
[17263]47    }
[17394]48   
[17406]49    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
[17439]50      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
[17394]51        IJsonItemConverter converter = GetConverter(item.GetType());
52        if(converter != null) converter.Inject(item, data, root);
[17439]53        InjectCache.Add(item.GetHashCode(), data);
[17394]54      }
55    }
[17263]56
[17406]57    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
[17394]58      int hash = item.GetHashCode();
[17439]59      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
[17394]60        return val;
61      else {
62        IJsonItemConverter converter = GetConverter(item.GetType());
63        if (converter == null) return new UnsupportedJsonItem();
[17406]64        IJsonItem tmp = GetConverter(item.GetType()).Extract(item, root);
[17439]65        ExtractCache.Add(hash, tmp);
[17394]66        return tmp;
67      }
68    }
69   
[17406]70    public static void Inject(IItem item, IJsonItem data) {
[17394]71      IJsonItemConverter c = JsonItemConverterFactory.Create();
72      c.Inject(item, data, c);
73    }
[17263]74
[17406]75    public static IJsonItem Extract(IItem item) {
[17394]76      IJsonItemConverter c = JsonItemConverterFactory.Create();
77      return c.Extract(item, c);
78    }
[17263]79
[17353]80    /// <summary>
81    /// Static constructor for default converter configuration.
82    /// </summary>
[17394]83    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
84      Converters = converters;
[17263]85    }
[17478]86
87    private bool CompareGenericTypes(Type t1, Type t2) =>
[17483]88      (t1.IsGenericType && t1.GetGenericTypeDefinition() == t2) /*||
89      (t2.IsGenericType && t2.GetGenericTypeDefinition() == t1)*/;
[17478]90
91    private bool CompareTypes(Type t1, Type t2) =>
[17483]92      t1 == t2 || /*t1.IsAssignableFrom(t2) ||*/
[17478]93      t1.GetInterfaces().Any(
94        i => i == t2 || CompareGenericTypes(i, t2)
95      ) ||
96      CompareGenericTypes(t1, t2);
[17263]97  }
98}
Note: See TracBrowser for help on using the repository browser.