Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueJsonItem.cs @ 17477

Last change on this file since 17477 was 17477, checked in by dpiringe, 4 years ago

#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
File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Newtonsoft.Json.Linq;
7
8namespace HeuristicLab.JsonInterface {
9  public abstract class ValueJsonItem : JsonItem, IValueJsonItem {
10    public object Value { get; set; }
11
12    public override void SetJObject(JObject jObject) {
13      Value = jObject[nameof(IValueJsonItem.Value)]?.ToObject<object>();
14    }
15
16  }
17
18  public abstract class ValueJsonItem<T> : ValueJsonItem, IValueJsonItem<T> {
19    public new T Value {
20      get => ConvertObject(base.Value);
21      set => base.Value = value;
22    }
23
24    private T ConvertObject(object obj) {
25      if (obj is IConvertible)
26        return (T)Convert.ChangeType(obj, typeof(T));
27
28      if (obj is JToken token)
29        return token.ToObject<T>();
30
31      return (T)obj;
32    }
33
34    public override void SetJObject(JObject jObject) {
35      if(jObject[nameof(IValueJsonItem<T>.Value)] != null)
36        Value = jObject[nameof(IValueJsonItem<T>.Value)].ToObject<T>();
37    }
38  }
39}
Note: See TracBrowser for help on using the repository browser.