[17263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
[17284] | 5 | namespace 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 | }
|
---|