Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026

  • added interfaces IJsonItem and IJsonItemValidator
  • replaced every reference JsonItem with IJsonItem
File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.PluginInfrastructure;
9using HEAL.Attic;
10using System.Collections;
11
12namespace HeuristicLab.JsonInterface {
13  /// <summary>
14  /// Class for handling json converters.
15  /// </summary>
16  public class JsonItemConverter : IJsonItemConverter {
17   
18    #region Properties
19    private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
20      = new Dictionary<Type, IJsonItemConverter>();
21
22    private IDictionary<int, IJsonItem> Cache { get; set; }
23      = new Dictionary<int, IJsonItem>();
24
25    public int Priority => throw new NotImplementedException();
26
27    public Type ConvertableType => throw new NotImplementedException();
28    #endregion
29
30    /// <summary>
31    /// GetConverter a converter for a specific type.
32    /// </summary>
33    /// <param name="type">The type for which the converter will be selected.</param>
34    /// <returns>An IJsonItemConverter object.</returns>
35    public IJsonItemConverter GetConverter(Type type) {
36      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
37     
38      foreach (var x in Converters)
39        if (type.IsEqualTo(x.Key))
40          possibleConverters.Add(x.Value);
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(!Cache.ContainsKey(item.GetHashCode())) {
55        IJsonItemConverter converter = GetConverter(item.GetType());
56        if(converter != null) converter.Inject(item, data, root);
57      }
58    }
59
60    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
61      int hash = item.GetHashCode();
62      if (Cache.TryGetValue(hash, out IJsonItem val))
63        return val;
64      else {
65        IJsonItemConverter converter = GetConverter(item.GetType());
66        if (converter == null) return new UnsupportedJsonItem();
67        IJsonItem tmp = GetConverter(item.GetType()).Extract(item, root);
68        Cache.Add(hash, tmp);
69        return tmp;
70      }
71    }
72   
73    public static void Inject(IItem item, IJsonItem data) {
74      IJsonItemConverter c = JsonItemConverterFactory.Create();
75      c.Inject(item, data, c);
76    }
77
78    public static IJsonItem Extract(IItem item) {
79      IJsonItemConverter c = JsonItemConverterFactory.Create();
80      return c.Extract(item, c);
81    }
82
83    /// <summary>
84    /// Static constructor for default converter configuration.
85    /// </summary>
86    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
87      Converters = converters;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.