[17263] | 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;
|
---|
[17371] | 8 | using HeuristicLab.PluginInfrastructure;
|
---|
| 9 | using HEAL.Attic;
|
---|
| 10 | using System.Collections;
|
---|
[17263] | 11 |
|
---|
[17284] | 12 | namespace HeuristicLab.JsonInterface {
|
---|
[17353] | 13 | /// <summary>
|
---|
[17394] | 14 | /// Class for handling json converters.
|
---|
[17353] | 15 | /// </summary>
|
---|
[17394] | 16 | public class JsonItemConverter : IJsonItemConverter {
|
---|
[17263] | 17 |
|
---|
[17394] | 18 | #region Properties
|
---|
| 19 | private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
|
---|
| 20 | = new Dictionary<Type, IJsonItemConverter>();
|
---|
[17263] | 21 |
|
---|
[17406] | 22 | private IDictionary<int, IJsonItem> Cache { get; set; }
|
---|
| 23 | = new Dictionary<int, IJsonItem>();
|
---|
[17371] | 24 |
|
---|
[17394] | 25 | public int Priority => throw new NotImplementedException();
|
---|
[17371] | 26 |
|
---|
[17394] | 27 | public Type ConvertableType => throw new NotImplementedException();
|
---|
| 28 | #endregion
|
---|
[17263] | 29 |
|
---|
[17353] | 30 | /// <summary>
|
---|
[17394] | 31 | /// GetConverter a converter for a specific type.
|
---|
[17353] | 32 | /// </summary>
|
---|
| 33 | /// <param name="type">The type for which the converter will be selected.</param>
|
---|
| 34 | /// <returns>An IJsonItemConverter object.</returns>
|
---|
[17394] | 35 | public IJsonItemConverter GetConverter(Type type) {
|
---|
| 36 | IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
|
---|
[17353] | 37 |
|
---|
| 38 | foreach (var x in Converters)
|
---|
[17266] | 39 | if (type.IsEqualTo(x.Key))
|
---|
[17282] | 40 | possibleConverters.Add(x.Value);
|
---|
[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) {
|
---|
[17394] | 54 | if(!Cache.ContainsKey(item.GetHashCode())) {
|
---|
| 55 | IJsonItemConverter converter = GetConverter(item.GetType());
|
---|
| 56 | if(converter != null) converter.Inject(item, data, root);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[17263] | 59 |
|
---|
[17406] | 60 | public IJsonItem Extract(IItem item, IJsonItemConverter root) {
|
---|
[17394] | 61 | int hash = item.GetHashCode();
|
---|
[17406] | 62 | if (Cache.TryGetValue(hash, out IJsonItem val))
|
---|
[17394] | 63 | return val;
|
---|
| 64 | else {
|
---|
| 65 | IJsonItemConverter converter = GetConverter(item.GetType());
|
---|
| 66 | if (converter == null) return new UnsupportedJsonItem();
|
---|
[17406] | 67 | IJsonItem tmp = GetConverter(item.GetType()).Extract(item, root);
|
---|
[17394] | 68 | Cache.Add(hash, tmp);
|
---|
| 69 | return tmp;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[17406] | 73 | public static void Inject(IItem item, IJsonItem data) {
|
---|
[17394] | 74 | IJsonItemConverter c = JsonItemConverterFactory.Create();
|
---|
| 75 | c.Inject(item, data, c);
|
---|
| 76 | }
|
---|
[17263] | 77 |
|
---|
[17406] | 78 | public static IJsonItem Extract(IItem item) {
|
---|
[17394] | 79 | IJsonItemConverter c = JsonItemConverterFactory.Create();
|
---|
| 80 | return c.Extract(item, c);
|
---|
| 81 | }
|
---|
[17263] | 82 |
|
---|
[17353] | 83 | /// <summary>
|
---|
| 84 | /// Static constructor for default converter configuration.
|
---|
| 85 | /// </summary>
|
---|
[17394] | 86 | internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
|
---|
| 87 | Converters = converters;
|
---|
[17263] | 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|