[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 |
|
---|
[17444] | 66 | private string fixedPath = "";
|
---|
[17394] | 67 | public virtual string Path {
|
---|
| 68 | get {
|
---|
[17444] | 69 | if (!string.IsNullOrWhiteSpace(fixedPath))
|
---|
| 70 | return fixedPath;
|
---|
| 71 |
|
---|
[17406] | 72 | IJsonItem tmp = Parent;
|
---|
[17394] | 73 | StringBuilder builder = new StringBuilder(this.Name);
|
---|
| 74 | while(tmp != null) {
|
---|
| 75 | builder.Insert(0, tmp.Name + ".");
|
---|
| 76 | tmp = tmp.Parent;
|
---|
| 77 | }
|
---|
| 78 | return builder.ToString();
|
---|
[17354] | 79 | }
|
---|
[17275] | 80 | }
|
---|
[17519] | 81 |
|
---|
[17394] | 82 | [JsonIgnore]
|
---|
[17519] | 83 | public virtual IEnumerable<IJsonItem> Children { get; protected set; }
|
---|
[17394] | 84 |
|
---|
| 85 | [JsonIgnore]
|
---|
[17406] | 86 | public virtual IJsonItem Parent { get; set; }
|
---|
[17394] | 87 |
|
---|
[17451] | 88 | [JsonIgnore]
|
---|
| 89 | public virtual bool Active { get; set; }
|
---|
| 90 |
|
---|
[17394] | 91 | #region Constructors
|
---|
| 92 | public JsonItem() { }
|
---|
| 93 |
|
---|
[17406] | 94 | public JsonItem(IEnumerable<IJsonItem> childs) {
|
---|
[17420] | 95 | AddChildren(childs);
|
---|
[17263] | 96 | }
|
---|
[17394] | 97 | #endregion
|
---|
[17451] | 98 |
|
---|
[17349] | 99 | #region Public Methods
|
---|
[17420] | 100 | public void AddChildren(params IJsonItem[] childs) =>
|
---|
| 101 | AddChildren(childs as IEnumerable<IJsonItem>);
|
---|
[17394] | 102 |
|
---|
[17420] | 103 | public void AddChildren(IEnumerable<IJsonItem> childs) {
|
---|
[17444] | 104 | if (childs == null) return;
|
---|
[17379] | 105 | if (Children == null)
|
---|
[17406] | 106 | Children = new List<IJsonItem>();
|
---|
[17519] | 107 | if(Children is IList<IJsonItem> list) {
|
---|
| 108 | foreach (var child in childs) {
|
---|
| 109 | list.Add(child);
|
---|
| 110 | child.Parent = this;
|
---|
| 111 | }
|
---|
[17394] | 112 | }
|
---|
[17374] | 113 | }
|
---|
| 114 |
|
---|
[17481] | 115 | public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
|
---|
[17444] | 116 |
|
---|
| 117 | public void FixatePath() => fixedPath = Path;
|
---|
| 118 | public void LoosenPath() => fixedPath = "";
|
---|
[17446] | 119 |
|
---|
[17477] | 120 | public virtual JObject GenerateJObject() =>
|
---|
| 121 | JObject.FromObject(this, new JsonSerializer() {
|
---|
| 122 | TypeNameHandling = TypeNameHandling.None,
|
---|
| 123 | NullValueHandling = NullValueHandling.Ignore,
|
---|
| 124 | ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
---|
| 125 | });
|
---|
| 126 |
|
---|
| 127 | public virtual void SetJObject(JObject jObject) { }
|
---|
[17349] | 128 | #endregion
|
---|
[17280] | 129 |
|
---|
[17477] | 130 | #region Abstract Methods
|
---|
[17481] | 131 | protected abstract ValidationResult Validate();
|
---|
[17477] | 132 | #endregion
|
---|
| 133 |
|
---|
| 134 | #region IEnumerable Support
|
---|
| 135 | public virtual IEnumerator<IJsonItem> GetEnumerator() {
|
---|
| 136 | yield return this;
|
---|
| 137 |
|
---|
| 138 | if (Children != null) {
|
---|
| 139 | foreach (var x in Children) {
|
---|
| 140 | foreach (var c in x) {
|
---|
| 141 | yield return c;
|
---|
| 142 | }
|
---|
[17374] | 143 | }
|
---|
[17477] | 144 | }
|
---|
[17374] | 145 | }
|
---|
| 146 |
|
---|
[17477] | 147 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
---|
[17269] | 148 | #endregion
|
---|
[17354] | 149 | }
|
---|
[17339] | 150 | } |
---|