Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.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: 3.7 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using Newtonsoft.Json;
7using Newtonsoft.Json.Linq;
8
9namespace HeuristicLab.JsonInterface {
10  /// <summary>
11  /// Main data class for json interface.
12  /// </summary>
13  public abstract class JsonItem : IJsonItem {
14
15    public class JsonItemValidator : IJsonItemValidator {
16      private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
17      private JsonItem Root { get; set; }
18      public JsonItemValidator(JsonItem root) {
19        Root = root;
20      }
21
22      public bool Validate(ref IList<IJsonItem> faultyItems) {
23        faultyItems = new List<IJsonItem>();
24        return ValidateHelper(Root, ref faultyItems);
25      }
26
27      // TODO: return ValidationResult ?
28      private bool ValidateHelper(JsonItem item, ref IList<IJsonItem> faultyItems) {
29        int hash = item.GetHashCode();
30        if (Cache.TryGetValue(hash, out bool r))
31          return r;
32
33        bool res = item.Validate();
34        if (!res) faultyItems.Add(item);
35        Cache.Add(hash, res);
36        if(item.Children != null) {
37          foreach (var child in item.Children)
38            if (!ValidateHelper(child as JsonItem, ref faultyItems))
39              res = false && res;
40        }
41        return res;
42      }
43    }
44
45    public virtual string Name { get; set; }
46
47    public virtual string Description { get; set; }
48
49    private string fixedPath = "";
50    public virtual string Path {
51      get {
52        if (!string.IsNullOrWhiteSpace(fixedPath))
53          return fixedPath;
54
55        IJsonItem tmp = Parent;
56        StringBuilder builder = new StringBuilder(this.Name);
57        while(tmp != null) {
58          builder.Insert(0, tmp.Name + ".");
59          tmp = tmp.Parent;
60        }
61        return builder.ToString();
62      }
63    }
64       
65    // TODO jsonIgnore dataType?
66
67    [JsonIgnore]
68    public virtual IList<IJsonItem> Children { get; protected set; }
69
70    [JsonIgnore]
71    public virtual IJsonItem Parent { get; set; }
72
73    [JsonIgnore]
74    public virtual bool Active { get; set; }
75
76    #region Constructors
77    public JsonItem() { }
78
79    public JsonItem(IEnumerable<IJsonItem> childs) {
80      AddChildren(childs);
81    }
82    #endregion
83   
84    #region Public Methods
85    public void AddChildren(params IJsonItem[] childs) =>
86      AddChildren(childs as IEnumerable<IJsonItem>);
87
88    public void AddChildren(IEnumerable<IJsonItem> childs) {
89      if (childs == null) return;
90      if (Children == null)
91        Children = new List<IJsonItem>();
92      foreach (var child in childs) {
93        Children.Add(child);
94        child.Parent = this;
95      }
96    }
97
98    public virtual IJsonItemValidator GetValidator() => new JsonItemValidator(this);
99
100    public void FixatePath() => fixedPath = Path;
101    public void LoosenPath() => fixedPath = "";
102
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) { }
111    #endregion
112
113    #region Abstract Methods
114    protected abstract bool Validate();
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          }
126        }
127      }
128    }
129
130    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.