Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/17/20 12:33:35 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • refactored JsonTemplateInstantiator -> now returns a InstantiatorResult which contains the optimizer and an IEnumerable of IResultJsonItem
  • code cleanup in JCGenerator
  • relocated the serialization of json items into IJsonItem with method GenerateJObject (virtual base implementation in JsonItem)
    • this allows custom serialization for json items (example: ValueLookupJsonItem)
    • items of interface IIntervalRestrictedJsonItem have a custom implementation of GenerateJObject -> hides Minimum and Maximum if the values are the physically min/max of their type
  • code cleanup in BaseConverter
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models
Files:
12 edited

Legend:

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

    r17473 r17477  
    99  public abstract class ArrayJsonItem<T> : ValueJsonItem<T[]>, IArrayJsonItem {
    1010    public virtual bool Resizable { get; set; }
    11     public override void SetFromJObject(JObject jObject) {
    12       base.SetFromJObject(jObject);
     11    public override void SetJObject(JObject jObject) {
     12      base.SetJObject(jObject);
    1313      Resizable = (jObject[nameof(IArrayJsonItem.Resizable)]?.ToObject<bool>()).GetValueOrDefault();
    1414    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DateTimeJsonItem.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    89  public class DateTimeJsonItem : IntervalRestrictedValueJsonItem<DateTime> {
    910    protected override bool Validate() => Minimum.CompareTo(Value) >= 0 && Maximum.CompareTo(Value) <= 0;
     11   
     12    public override JObject GenerateJObject() {
     13      var obj = base.GenerateJObject();
     14
     15      if (Minimum.CompareTo(DateTime.MinValue) == 0)
     16        obj.Property("Minimum").Remove();
     17
     18      if (Maximum.CompareTo(DateTime.MaxValue) == 0)
     19        obj.Property("Maximum").Remove();
     20
     21      return obj;
     22    }
     23
     24    public override void SetJObject(JObject jObject) {
     25      Minimum = DateTime.MinValue;
     26      Maximum = DateTime.MaxValue;
     27      base.SetJObject(jObject);
     28    }
    1029  }
    1130}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DoubleJsonItems.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    8   public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> { }
    9   public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> { }
    10   public class DoubleRangeJsonItem : RangedJsonItem<double> { }
     9  public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> {
     10    public override JObject GenerateJObject() {
     11      var obj = base.GenerateJObject();
    1112
     13      if (Minimum.CompareTo(double.MinValue) == 0)
     14        obj.Property("Minimum").Remove();
     15
     16      if (Maximum.CompareTo(double.MaxValue) == 0)
     17        obj.Property("Maximum").Remove();
     18
     19      return obj;
     20    }
     21
     22    public override void SetJObject(JObject jObject) {
     23      Minimum = double.MinValue;
     24      Maximum = double.MaxValue;
     25      base.SetJObject(jObject);
     26    }
     27  }
     28  public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> {
     29    public override JObject GenerateJObject() {
     30      var obj = base.GenerateJObject();
     31
     32      if (Minimum.CompareTo(double.MinValue) == 0)
     33        obj.Property("Minimum").Remove();
     34
     35      if (Maximum.CompareTo(double.MaxValue) == 0)
     36        obj.Property("Maximum").Remove();
     37
     38      return obj;
     39    }
     40
     41    public override void SetJObject(JObject jObject) {
     42      Minimum = double.MinValue;
     43      Maximum = double.MaxValue;
     44      base.SetJObject(jObject);
     45    }
     46  }
     47  public class DoubleRangeJsonItem : RangedJsonItem<double> {
     48    public override JObject GenerateJObject() {
     49      var obj = base.GenerateJObject();
     50
     51      if (Minimum.CompareTo(double.MinValue) == 0)
     52        obj.Property("Minimum").Remove();
     53
     54      if (Maximum.CompareTo(double.MaxValue) == 0)
     55        obj.Property("Maximum").Remove();
     56
     57      return obj;
     58    }
     59
     60    public override void SetJObject(JObject jObject) {
     61      Minimum = double.MinValue;
     62      Maximum = double.MaxValue;
     63      base.SetJObject(jObject);
     64    }
     65  }
    1266  public class DoubleMatrixJsonItem : IntervalRestrictedMatrixJsonItem<double> {
    13     /*
    14     protected override bool IsInRange() {
    15       for (int c = 0; c < Value.Length; ++c) {
    16         for (int r = 0; r < Value[c].Length; ++r) {
    17           if (Value[c][r] < Range.First() && Range.Last() < Value[c][r])
    18             return false;
    19         }
    20       }
    21       return true;
     67    public override JObject GenerateJObject() {
     68      var obj = base.GenerateJObject();
     69
     70      if (Minimum.CompareTo(double.MinValue) == 0)
     71        obj.Property("Minimum").Remove();
     72
     73      if (Maximum.CompareTo(double.MaxValue) == 0)
     74        obj.Property("Maximum").Remove();
     75
     76      return obj;
    2277    }
    23     */
     78
     79    public override void SetJObject(JObject jObject) {
     80      Minimum = double.MinValue;
     81      Maximum = double.MaxValue;
     82      base.SetJObject(jObject);
     83    }
    2484  }
    2585}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntJsonItems.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    8   public class IntJsonItem : IntervalRestrictedValueJsonItem<int> { }
    9   public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> { }
    10   public class IntRangeJsonItem : RangedJsonItem<int> { }
     9  public class IntJsonItem : IntervalRestrictedValueJsonItem<int> {
     10    public override JObject GenerateJObject() {
     11      var obj = base.GenerateJObject();
     12
     13      if (Minimum.CompareTo(int.MinValue) == 0)
     14        obj.Property("Minimum").Remove();
     15
     16      if (Maximum.CompareTo(int.MaxValue) == 0)
     17        obj.Property("Maximum").Remove();
     18
     19      return obj;
     20    }
     21
     22    public override void SetJObject(JObject jObject) {
     23      Minimum = int.MinValue;
     24      Maximum = int.MaxValue;
     25      base.SetJObject(jObject);
     26    }
     27  }
     28  public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> {
     29    public override JObject GenerateJObject() {
     30      var obj = base.GenerateJObject();
     31
     32      if (Minimum.CompareTo(int.MinValue) == 0)
     33        obj.Property("Minimum").Remove();
     34
     35      if (Maximum.CompareTo(int.MaxValue) == 0)
     36        obj.Property("Maximum").Remove();
     37
     38      return obj;
     39    }
     40
     41    public override void SetJObject(JObject jObject) {
     42      Minimum = int.MinValue;
     43      Maximum = int.MaxValue;
     44      base.SetJObject(jObject);
     45    }
     46  }
     47  public class IntRangeJsonItem : RangedJsonItem<int> {
     48    public override JObject GenerateJObject() {
     49      var obj = base.GenerateJObject();
     50
     51      if (Minimum.CompareTo(int.MinValue) == 0)
     52        obj.Property("Minimum").Remove();
     53
     54      if (Maximum.CompareTo(int.MaxValue) == 0)
     55        obj.Property("Maximum").Remove();
     56
     57      return obj;
     58    }
     59
     60    public override void SetJObject(JObject jObject) {
     61      Minimum = int.MinValue;
     62      Maximum = int.MaxValue;
     63      base.SetJObject(jObject);
     64    }
     65  }
    1166  public class IntMatrixJsonItem : IntervalRestrictedMatrixJsonItem<int> {
    12     /*
    13     protected override bool IsInRange() {
    14       for (int c = 0; c < Value.Length; ++c) {
    15         for (int r = 0; r < Value[c].Length; ++r) {
    16           if (Value[c][r] < Range.First() && Range.Last() < Value[c][r])
    17             return false;
    18         }
    19       }
    20       return true;
     67    public override JObject GenerateJObject() {
     68      var obj = base.GenerateJObject();
     69
     70      if (Minimum.CompareTo(int.MinValue) == 0)
     71        obj.Property("Minimum").Remove();
     72
     73      if (Maximum.CompareTo(int.MaxValue) == 0)
     74        obj.Property("Maximum").Remove();
     75
     76      return obj;
    2177    }
    22     */
     78
     79    public override void SetJObject(JObject jObject) {
     80      Minimum = int.MinValue;
     81      Maximum = int.MaxValue;
     82      base.SetJObject(jObject);
     83    }
    2384  }
    2485}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedArrayJsonItem.cs

    r17473 r17477  
    2020    }
    2121
    22     public override void SetFromJObject(JObject jObject) {
    23       base.SetFromJObject(jObject);
    24       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    25       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     22    public override void SetJObject(JObject jObject) {
     23      base.SetJObject(jObject);
     24
     25      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     26      if (minProp != null) Minimum = minProp.ToObject<T>();
     27
     28
     29      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     30      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2631    }
    2732  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedMatrixJsonItem.cs

    r17473 r17477  
    2020      return true;
    2121    }
    22     public override void SetFromJObject(JObject jObject) {
    23       base.SetFromJObject(jObject);
    24       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    25       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     22    public override void SetJObject(JObject jObject) {
     23      base.SetJObject(jObject);
     24
     25      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     26      if (minProp != null) Minimum = minProp.ToObject<T>();
     27
     28
     29      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     30      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2631    }
    2732  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedValueJsonItem.cs

    r17473 r17477  
    1414    protected override bool Validate() => Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0;
    1515
    16     public override void SetFromJObject(JObject jObject) {
    17       base.SetFromJObject(jObject);
    18       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    19       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     16    public override void SetJObject(JObject jObject) {
     17      base.SetJObject(jObject);
     18
     19      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     20      if (minProp != null) Minimum = minProp.ToObject<T>();
     21
     22
     23      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     24      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2025    }
    2126  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs

    r17473 r17477  
    6262      }
    6363    }
    64 
    65     //public virtual object Value { get; set; }
    66 
    67     //public virtual IEnumerable<object> Range { get; set; }
    68    
     64       
    6965    // TODO jsonIgnore dataType?
    7066
     
    105101    public void LoosenPath() => fixedPath = "";
    106102
    107     public virtual void SetFromJObject(JObject jObject) {
    108       //Value = jObject[nameof(IJsonItem.Value)]?.ToObject<object>();
    109       //Range = jObject[nameof(IJsonItem.Range)]?.ToObject<object[]>();
    110     }
     103    public virtual JObject GenerateJObject() =>
     104      JObject.FromObject(this, new JsonSerializer() {
     105        TypeNameHandling = TypeNameHandling.None,
     106        NullValueHandling = NullValueHandling.Ignore,
     107        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
     108      });
     109
     110    public virtual void SetJObject(JObject jObject) { }
    111111    #endregion
    112112
    113     #region Helper
    114     /*
    115      * TODO protected abstract bool Validate();
    116      */
     113    #region Abstract Methods
    117114    protected abstract bool Validate();
    118     /*
    119     protected virtual bool IsInRange() {
    120       bool b1 = true, b2 = true;
    121       if (Value is IEnumerable && !(Value is string)) {
    122         foreach (var x in (IEnumerable)Value) {
    123           b1 = b1 ? IsInRangeList(x) : b1;
    124           b2 = b2 ? IsInNumericRange(x) : b2;
     115    #endregion
     116
     117    #region IEnumerable Support
     118    public virtual IEnumerator<IJsonItem> GetEnumerator() {
     119      yield return this;
     120     
     121      if (Children != null) {
     122        foreach (var x in Children) {
     123          foreach (var c in x) {
     124            yield return c;
     125          }
    125126        }
    126       }
    127       else {
    128         b1 = IsInRangeList(Value);
    129         b2 = IsInNumericRange(Value);
    130       }
    131       return b1 || b2;
     127      }
    132128    }
    133129
    134     protected bool IsInRangeList(object value) {
    135       foreach (var x in Range)
    136         if (x.Equals(value)) return true;
    137       return false;
    138     }
    139 
    140     protected bool IsInNumericRange(object value) =>
    141       IsInNumericRange<ulong>(value)
    142       || IsInNumericRange<uint>(value)
    143       || IsInNumericRange<ushort>(value)
    144       || IsInNumericRange<long>(value)
    145       || IsInNumericRange<int>(value)
    146       || IsInNumericRange<short>(value)
    147       || IsInNumericRange<byte>(value)
    148       || IsInNumericRange<float>(value)
    149       || IsInNumericRange<double>(value)
    150       || (value is float && float.IsNaN((float)value))
    151       || (value is double && double.IsNaN((double)value));
    152 
    153     protected bool IsInNumericRange<T>(object value) where T : IComparable {
    154       object min = Range.First(), max = Range.Last();
    155       return
    156         value != null && min != null && max != null && value is T && min is T && max is T &&
    157         (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
    158         (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
    159     }
    160     */
     130    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    161131    #endregion
    162132  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/LookupJsonItem.cs

    r17473 r17477  
    1010    public virtual string ActualName { get; set; }
    1111
    12     public override void SetFromJObject(JObject jObject) {
    13       base.SetFromJObject(jObject);
     12    public override void SetJObject(JObject jObject) {
     13      base.SetJObject(jObject);
    1414      ActualName = jObject[nameof(ILookupJsonItem.ActualName)]?.ToString();
    1515    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/MatrixJsonItem.cs

    r17473 r17477  
    2323    }
    2424
    25     public override void SetFromJObject(JObject jObject) {
    26       base.SetFromJObject(jObject);
     25    public override void SetJObject(JObject jObject) {
     26      base.SetJObject(jObject);
    2727      RowsResizable = (jObject[nameof(IMatrixJsonItem.RowsResizable)]?.ToObject<bool>()).GetValueOrDefault();
    2828      ColumnsResizable = (jObject[nameof(IMatrixJsonItem.ColumnsResizable)]?.ToObject<bool>()).GetValueOrDefault();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueJsonItem.cs

    r17473 r17477  
    1010    public object Value { get; set; }
    1111
    12     //public IEnumerable<object> Range { get; set; }
    13 
    14     public override void SetFromJObject(JObject jObject) {
     12    public override void SetJObject(JObject jObject) {
    1513      Value = jObject[nameof(IValueJsonItem.Value)]?.ToObject<object>();
    16       //Range = jObject[nameof(IValueJsonItem.Range)]?.ToObject<object[]>();
    1714    }
    1815
     
    2421      set => base.Value = value;
    2522    }
    26 
    27     /*
    28     public new IEnumerable<T> Range {
    29       get => base.Range?.Cast<T>();
    30       set => base.Range = value.Cast<object>();
    31     }
    32     */
    3323
    3424    private T ConvertObject(object obj) {
     
    4232    }
    4333
    44     public override void SetFromJObject(JObject jObject) {
     34    public override void SetJObject(JObject jObject) {
    4535      if(jObject[nameof(IValueJsonItem<T>.Value)] != null)
    4636        Value = jObject[nameof(IValueJsonItem<T>.Value)].ToObject<T>();
    47       //Range = jObject[nameof(IValueJsonItem<T>.Range)]?.ToObject<T[]>();
    4837    }
    4938  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueLookupJsonItem.cs

    r17473 r17477  
    55using System.Threading.Tasks;
    66using Newtonsoft.Json;
     7using Newtonsoft.Json.Linq;
    78
    89namespace HeuristicLab.JsonInterface {
    910  public class ValueLookupJsonItem : LookupJsonItem, IValueLookupJsonItem {
    10     [JsonIgnore]
    11     public IJsonItem JsonItemReference { get; set; }
     11   
     12    public IJsonItem ActualValue { get; set; }
    1213
    1314    protected override bool Validate() {
    14       if (JsonItemReference == null) return true;
     15      if (ActualValue == null) return true;
    1516      IList<IJsonItem> faultyItems = new List<IJsonItem>();
    16       return JsonItemReference.GetValidator().Validate(ref faultyItems);
     17      return ActualValue.GetValidator().Validate(ref faultyItems);
     18    }
     19
     20    public override JObject GenerateJObject() {
     21      var obj = base.GenerateJObject();
     22      if(ActualValue != null) {
     23        var actualValue = ActualValue.GenerateJObject();
     24        obj.Add(nameof(IValueLookupJsonItem.ActualValue), actualValue);
     25      }
     26      return obj;
     27    }
     28
     29    public override IEnumerator<IJsonItem> GetEnumerator() {
     30      using (var it = base.GetEnumerator()) {
     31        while(it.MoveNext()) {
     32          yield return it.Current;
     33        }
     34      }
     35      if(ActualValue != null) {
     36        using (var it = ActualValue.GetEnumerator()) {
     37          while (it.MoveNext()) {
     38            yield return it.Current;
     39          }
     40        }
     41      }
    1742    }
    1843  }
Note: See TracChangeset for help on using the changeset viewer.