Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
File size: 1.4 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 IEnumerable<object> Range { get; set; }
13
14    public override void SetFromJObject(JObject jObject) {
15      Value = jObject[nameof(IValueJsonItem.Value)]?.ToObject<object>();
16      //Range = jObject[nameof(IValueJsonItem.Range)]?.ToObject<object[]>();
17    }
18
19  }
20
21  public abstract class ValueJsonItem<T> : ValueJsonItem, IValueJsonItem<T> {
22    public new T Value {
23      get => ConvertObject(base.Value);
24      set => base.Value = value;
25    }
26
27    /*
28    public new IEnumerable<T> Range {
29      get => base.Range?.Cast<T>();
30      set => base.Range = value.Cast<object>();
31    }
32    */
33
34    private T ConvertObject(object obj) {
35      if (obj is IConvertible)
36        return (T)Convert.ChangeType(obj, typeof(T));
37
38      if (obj is JToken token)
39        return token.ToObject<T>();
40
41      return (T)obj;
42    }
43
44    public override void SetFromJObject(JObject jObject) {
45      if(jObject[nameof(IValueJsonItem<T>.Value)] != null)
46        Value = jObject[nameof(IValueJsonItem<T>.Value)].ToObject<T>();
47      //Range = jObject[nameof(IValueJsonItem<T>.Range)]?.ToObject<T[]>();
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.