Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
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, JsonItem> Cache { get; set; }
23      = new Dictionary<int, JsonItem>();
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, JsonItem 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 JsonItem Extract(IItem item, IJsonItemConverter root) {
61      int hash = item.GetHashCode();
62      if (Cache.TryGetValue(hash, out JsonItem val))
63        return val;
64      else {
65        IJsonItemConverter converter = GetConverter(item.GetType());
66        if (converter == null) return new UnsupportedJsonItem();
67        JsonItem 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, JsonItem data) {
74      IJsonItemConverter c = JsonItemConverterFactory.Create();
75      c.Inject(item, data, c);
76    }
77
78    public static JsonItem 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.