1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.PluginInfrastructure;
|
---|
9 | using HEAL.Attic;
|
---|
10 | using System.Collections;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|