Ignore:
Timestamp:
11/19/19 16:33:01 (17 months ago)
Author:
dpiringe
Message:

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs

    r17287 r17353  
    88
    99namespace HeuristicLab.JsonInterface {
     10  /// <summary>
     11  /// Static class for handling json converters.
     12  /// </summary>
    1013  public static class JsonItemConverter {
    1114
     
    1518    }
    1619
    17     private static IDictionary<Type, ConverterPriorityContainer> transformers = new Dictionary<Type, ConverterPriorityContainer>();
     20    private static IDictionary<Type, ConverterPriorityContainer> Converters { get; set; }
     21      = new Dictionary<Type, ConverterPriorityContainer>();
    1822   
     23    /// <summary>
     24    /// Register a converter for a given type and priority.
     25    /// </summary>
     26    /// <param name="type">The type for which the converter will be selected.</param>
     27    /// <param name="converter">The implemented converter.</param>
     28    /// <param name="priority">The priority for the converter selection (when multiple converter match for a given type). Higher is better.</param>
    1929    public static void Register(Type type, IJsonItemConverter converter, int priority) {
    20       if (!transformers.ContainsKey(type))
    21         transformers.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
     30      if (!Converters.ContainsKey(type))
     31        Converters.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
    2232    }
    2333
    24     public static void Register<T>(IJsonItemConverter converter, int priority) => Register(typeof(T), converter, priority);
     34    public static void Register<T>(IJsonItemConverter converter, int priority) =>
     35      Register(typeof(T), converter, priority);
    2536
     37    /// <summary>
     38    /// Deregister a converter (same object has to be already registered).
     39    /// </summary>
     40    /// <param name="converter">Converter to deregister.</param>
     41    public static void Deregister(IJsonItemConverter converter) {
     42      var types =
     43        Converters
     44        .Where(x => x.Value.Converter.GetHashCode() == converter.GetHashCode())
     45        .Select(x => x.Key);
     46      foreach (var x in types)
     47        Converters.Remove(x);
     48    }
     49
     50    /// <summary>
     51    /// Get a converter for a specific type.
     52    /// </summary>
     53    /// <param name="type">The type for which the converter will be selected.</param>
     54    /// <returns>An IJsonItemConverter object.</returns>
    2655    public static IJsonItemConverter Get(Type type) {
    2756      IList<ConverterPriorityContainer> possibleConverters = new List<ConverterPriorityContainer>();
    28 
    29       foreach (var x in transformers)
     57     
     58      foreach (var x in Converters)
    3059        if (type.IsEqualTo(x.Key))
    3160          possibleConverters.Add(x.Value);
     
    4978
    5079
     80    /// <summary>
     81    /// Static constructor for default converter configuration.
     82    /// </summary>
    5183    static JsonItemConverter() {
    5284      Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
     
    6799      Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
    68100
     101      Register<DoubleRange>(new DoubleRangeConverter(), 1);
     102      Register<IntRange>(new IntRangeConverter(), 1);
     103
    69104      Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
    70105
    71       Register<IValueParameter>(new ValueParameterConverter(), 1);
    72       Register<IParameterizedItem>(new ParameterizedItemConverter(), 1);
    73       Register<ILookupParameter>(new LookupParameterConverter(), 2);
    74       Register<IValueLookupParameter>(new ValueLookupParameterConverter(), 3);
     106      Register<IParameter>(new ParameterConverter(), 1);
    75107
    76       Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 2);
    77       Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 2);
     108      Register<IValueParameter>(new ValueParameterConverter(), 2);
     109      Register<IParameterizedItem>(new ParameterizedItemConverter(), 2);
     110      Register<ILookupParameter>(new LookupParameterConverter(), 3);
     111      Register<IValueLookupParameter>(new ValueLookupParameterConverter(), 4);
     112
     113      Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 3);
     114      Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 3);
    78115    }
    79116  }
Note: See TracChangeset for help on using the changeset viewer.