[17828] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
[17263] | 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using Newtonsoft.Json;
|
---|
[17446] | 7 | using Newtonsoft.Json.Linq;
|
---|
[17263] | 8 |
|
---|
[17284] | 9 | namespace HeuristicLab.JsonInterface {
|
---|
[17481] | 10 |
|
---|
| 11 | public readonly struct ValidationResult {
|
---|
| 12 |
|
---|
| 13 | public static ValidationResult Successful() => new ValidationResult(true, Enumerable.Empty<string>());
|
---|
| 14 | public static ValidationResult Faulty(IEnumerable<string> errors) => new ValidationResult(false, errors);
|
---|
| 15 | public static ValidationResult Faulty(string error) => new ValidationResult(false, error);
|
---|
| 16 |
|
---|
| 17 | public ValidationResult(bool success, IEnumerable<string> errors) {
|
---|
| 18 | Success = success;
|
---|
| 19 | Errors = errors;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public ValidationResult(bool success, string error) {
|
---|
| 23 | Success = success;
|
---|
| 24 | Errors = Enumerable.Repeat(error, 1);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public bool Success { get; }
|
---|
| 28 | public IEnumerable<string> Errors { get; }
|
---|
[17828] | 29 |
|
---|
| 30 | public Exception GenerateException() =>
|
---|
| 31 | new AggregateException(Errors.Select(x => new ArgumentException(x)));
|
---|
[17481] | 32 | }
|
---|
| 33 |
|
---|
[17353] | 34 | /// <summary>
|
---|
| 35 | /// Main data class for json interface.
|
---|
| 36 | /// </summary>
|
---|
[17473] | 37 | public abstract class JsonItem : IJsonItem {
|
---|
[17406] | 38 |
|
---|
| 39 | public class JsonItemValidator : IJsonItemValidator {
|
---|
[17394] | 40 | private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
|
---|
| 41 | private JsonItem Root { get; set; }
|
---|
| 42 | public JsonItemValidator(JsonItem root) {
|
---|
| 43 | Root = root;
|
---|
| 44 | }
|
---|
[17349] | 45 |
|
---|
[17481] | 46 | public ValidationResult Validate() {
|
---|
[17485] | 47 | List<string> errors = new List<string>();
|
---|
[17481] | 48 | bool success = true;
|
---|
| 49 | foreach(var x in Root) {
|
---|
[17485] | 50 | JsonItem item = x as JsonItem;
|
---|
| 51 | if(item.Active) {
|
---|
| 52 | var res = ((JsonItem)x).Validate();
|
---|
| 53 | //if one success is false -> whole validation is false
|
---|
| 54 | success = success && res.Success;
|
---|
| 55 | errors.AddRange(res.Errors);
|
---|
| 56 | }
|
---|
[17473] | 57 | }
|
---|
[17481] | 58 | return new ValidationResult(success, errors);
|
---|
[17354] | 59 | }
|
---|
[17339] | 60 | }
|
---|
[17394] | 61 |
|
---|
| 62 | public virtual string Name { get; set; }
|
---|
| 63 |
|
---|
[17433] | 64 | public virtual string Description { get; set; }
|
---|
| 65 |
|
---|
[17394] | 66 | public virtual string Path {
|
---|
| 67 | get {
|
---|
[17406] | 68 | IJsonItem tmp = Parent;
|
---|
[17394] | 69 | StringBuilder builder = new StringBuilder(this.Name);
|
---|
| 70 | while(tmp != null) {
|
---|
| 71 | builder.Insert(0, tmp.Name + ".");
|
---|
| 72 | tmp = tmp.Parent;
|
---|
| 73 | }
|
---|
| 74 | return builder.ToString();
|
---|
[17354] | 75 | }
|
---|
[17275] | 76 | }
|
---|
[17519] | 77 |
|
---|
[17394] | 78 | [JsonIgnore]
|
---|
[17519] | 79 | public virtual IEnumerable<IJsonItem> Children { get; protected set; }
|
---|
[17394] | 80 |
|
---|
| 81 | [JsonIgnore]
|
---|
[17406] | 82 | public virtual IJsonItem Parent { get; set; }
|
---|
[17394] | 83 |
|
---|
[17451] | 84 | [JsonIgnore]
|
---|
| 85 | public virtual bool Active { get; set; }
|
---|
| 86 |
|
---|
[17394] | 87 | #region Constructors
|
---|
| 88 | public JsonItem() { }
|
---|
| 89 |
|
---|
[17406] | 90 | public JsonItem(IEnumerable<IJsonItem> childs) {
|
---|
[17420] | 91 | AddChildren(childs);
|
---|
[17263] | 92 | }
|
---|
[17394] | 93 | #endregion
|
---|
[17451] | 94 |
|
---|
[17349] | 95 | #region Public Methods
|
---|
[17420] | 96 | public void AddChildren(params IJsonItem[] childs) =>
|
---|
| 97 | AddChildren(childs as IEnumerable<IJsonItem>);
|
---|
[17394] | 98 |
|
---|
[17420] | 99 | public void AddChildren(IEnumerable<IJsonItem> childs) {
|
---|
[17444] | 100 | if (childs == null) return;
|
---|
[17379] | 101 | if (Children == null)
|
---|
[17406] | 102 | Children = new List<IJsonItem>();
|
---|
[17519] | 103 | if(Children is IList<IJsonItem> list) {
|
---|
| 104 | foreach (var child in childs) {
|
---|
| 105 | list.Add(child);
|
---|
| 106 | child.Parent = this;
|
---|
| 107 | }
|
---|
[17394] | 108 | }
|
---|
[17374] | 109 | }
|
---|
| 110 |
|
---|
[17481] | 111 | public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
|
---|
[17444] | 112 |
|
---|
[17477] | 113 | public virtual JObject GenerateJObject() =>
|
---|
| 114 | JObject.FromObject(this, new JsonSerializer() {
|
---|
| 115 | TypeNameHandling = TypeNameHandling.None,
|
---|
| 116 | NullValueHandling = NullValueHandling.Ignore,
|
---|
| 117 | ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
---|
| 118 | });
|
---|
| 119 |
|
---|
[17834] | 120 | public virtual void SetJObject(JObject jObject) {
|
---|
| 121 | Name = (jObject[nameof(IJsonItem.Name)]?.ToObject<string>());
|
---|
| 122 | Description = (jObject[nameof(IJsonItem.Description)]?.ToObject<string>());
|
---|
| 123 | }
|
---|
[17349] | 124 | #endregion
|
---|
[17280] | 125 |
|
---|
[17477] | 126 | #region Abstract Methods
|
---|
[17481] | 127 | protected abstract ValidationResult Validate();
|
---|
[17477] | 128 | #endregion
|
---|
| 129 |
|
---|
| 130 | #region IEnumerable Support
|
---|
| 131 | public virtual IEnumerator<IJsonItem> GetEnumerator() {
|
---|
| 132 | yield return this;
|
---|
| 133 |
|
---|
| 134 | if (Children != null) {
|
---|
| 135 | foreach (var x in Children) {
|
---|
| 136 | foreach (var c in x) {
|
---|
| 137 | yield return c;
|
---|
| 138 | }
|
---|
[17374] | 139 | }
|
---|
[17477] | 140 | }
|
---|
[17374] | 141 | }
|
---|
| 142 |
|
---|
[17477] | 143 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
---|
[17269] | 144 | #endregion
|
---|
[17354] | 145 | }
|
---|
[17339] | 146 | } |
---|