<PackageReference Include="NJsonSchema" Version="9.9.9" />

JsonReferenceVisitorBase

public abstract class JsonReferenceVisitorBase
Visitor to transform an object with JsonSchema4 objects.
using Microsoft.Runtime.CompilerServices; using Newtonsoft.Json.Linq; using NJsonSchema.Infrastructure; using NJsonSchema.References; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace NJsonSchema.Visitors { public abstract class JsonReferenceVisitorBase { private readonly string[] _jsonSchemaProperties = (from p in ReflectionExtensions.GetRuntimeProperties(typeof(JsonSchema4)) select p.Name).ToArray(); public virtual async Task VisitAsync(object obj) { TaskAwaiter val = AwaitExtensions.GetAwaiter(VisitAsync(obj, "#", null, new HashSet<object>(), delegate { throw new NotSupportedException("Cannot replace the root."); })); if (!val.get_IsCompleted()) { await val; TaskAwaiter val2 = default(TaskAwaiter); val = val2; } val.GetResult(); } protected abstract Task<IJsonReference> VisitJsonReferenceAsync(IJsonReference reference, string path, string typeNameHint); protected virtual async Task VisitAsync(object obj, string path, string typeNameHint, ISet<object> checkedObjects, Action<object> replacer) { if (obj != null && !checkedObjects.Contains(obj)) { checkedObjects.Add(obj); IJsonReference jsonReference; IJsonReference reference = jsonReference = (obj as IJsonReference); if (jsonReference != null) { TaskAwaiter<IJsonReference> val = AwaitExtensions.GetAwaiter<IJsonReference>(VisitJsonReferenceAsync(reference, path, typeNameHint)); if (!val.get_IsCompleted()) { await val; TaskAwaiter<IJsonReference> val2 = default(TaskAwaiter<IJsonReference>); val = val2; } IJsonReference result = val.GetResult(); if (result != reference) { replacer(result); return; } } JsonSchema4 schema; TaskAwaiter val3; TaskAwaiter val4 = default(TaskAwaiter); if ((schema = (obj as JsonSchema4)) != null) { if (schema.Reference != null) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.Reference, path, null, checkedObjects, delegate(object o) { schema.Reference = (JsonSchema4)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } if (schema.AdditionalItemsSchema != null) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, delegate(object o) { schema.AdditionalItemsSchema = (JsonSchema4)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } if (schema.AdditionalPropertiesSchema != null) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.AdditionalPropertiesSchema, path + "/additionalProperties", null, checkedObjects, delegate(object o) { schema.AdditionalPropertiesSchema = (JsonSchema4)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } if (schema.Item != null) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.Item, path + "/items", null, checkedObjects, delegate(object o) { schema.Item = (JsonSchema4)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } for (int k = 0; k < schema.Items.Count; k++) { int index = k; val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.Items.ElementAt(k), path + "/items[" + k + "]", null, checkedObjects, delegate(object o) { ReplaceOrDelete(schema.Items, index, (JsonSchema4)o); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } for (int l = 0; l < schema.AllOf.Count; l++) { int index2 = l; val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.AllOf.ElementAt(l), path + "/allOf[" + l + "]", null, checkedObjects, delegate(object o) { ReplaceOrDelete(schema.AllOf, index2, (JsonSchema4)o); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } for (int m = 0; m < schema.AnyOf.Count; m++) { int index3 = m; val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.AnyOf.ElementAt(m), path + "/anyOf[" + m + "]", null, checkedObjects, delegate(object o) { ReplaceOrDelete(schema.AnyOf, index3, (JsonSchema4)o); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } for (int n = 0; n < schema.OneOf.Count; n++) { int index4 = n; val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.OneOf.ElementAt(n), path + "/oneOf[" + n + "]", null, checkedObjects, delegate(object o) { ReplaceOrDelete(schema.OneOf, index4, (JsonSchema4)o); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } if (schema.Not != null) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(schema.Not, path + "/not", null, checkedObjects, delegate(object o) { schema.Not = (JsonSchema4)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } KeyValuePair<string, JsonProperty>[] array = schema.Properties.ToArray(); for (int num = 0; num < array.Length; num++) { KeyValuePair<string, JsonProperty> p = array[num]; val3 = AwaitExtensions.GetAwaiter(VisitAsync(p.Value, path + "/properties/" + p.Key, p.Key, checkedObjects, delegate(object o) { schema.Properties[p.Key] = (JsonProperty)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } KeyValuePair<string, JsonSchema4>[] array2 = schema.PatternProperties.ToArray(); for (int num = 0; num < array2.Length; num++) { KeyValuePair<string, JsonSchema4> p2 = array2[num]; val3 = AwaitExtensions.GetAwaiter(VisitAsync(p2.Value, path + "/patternProperties/" + p2.Key, null, checkedObjects, delegate(object o) { schema.PatternProperties[p2.Key] = (JsonProperty)o; })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } array2 = schema.Definitions.ToArray(); for (int num = 0; num < array2.Length; num++) { KeyValuePair<string, JsonSchema4> p3 = array2[num]; val3 = AwaitExtensions.GetAwaiter(VisitAsync(p3.Value, path + "/definitions/" + p3.Key, p3.Key, checkedObjects, delegate(object o) { if (o != null) schema.Definitions[p3.Key] = (JsonSchema4)o; else schema.Definitions.Remove(p3.Key); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } } if (!(obj is string) && !(obj is JToken)) { IDictionary dictionary; IList list; if ((dictionary = (obj as IDictionary)) != null) { val3 = AwaitExtensions.GetAwaiter(VisitPropertiesAsync(obj, path, checkedObjects)); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); object[] array3 = dictionary.Keys.OfType<object>().ToArray(); foreach (object key in array3) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(dictionary[key], path + "/" + key, key.ToString(), checkedObjects, delegate(object o) { if (o != null) dictionary[key] = (JsonSchema4)o; else dictionary.Remove(key); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } } else if ((list = (obj as IList)) != null) { object[] items2 = list.OfType<object>().ToArray(); for (int j = 0; j < items2.Length; j++) { int index5 = j; val3 = AwaitExtensions.GetAwaiter(VisitAsync(items2[j], path + "[" + j + "]", null, checkedObjects, delegate(object o) { ReplaceOrDelete(list, index5, o); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } } else { IEnumerable obj2 = obj as IEnumerable; if (obj2 != null) { object[] items = obj2.OfType<object>().ToArray(); for (int i = 0; i < items.Length; i++) { val3 = AwaitExtensions.GetAwaiter(VisitAsync(items[i], path + "[" + i + "]", null, checkedObjects, delegate { throw new NotSupportedException("Cannot replace enumerable item."); })); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(TaskAwaiter); } val3.GetResult(); } } else { val3 = AwaitExtensions.GetAwaiter(VisitPropertiesAsync(obj, path, checkedObjects)); if (!val3.get_IsCompleted()) { await val3; val3 = val4; } val3.GetResult(); } } } } } private async Task VisitPropertiesAsync(object obj, string path, ISet<object> checkedObjects) { foreach (ReflectionCache.PropertyOrField item in ReflectionCache.GetPropertiesAndFields(obj.GetType()).Where(delegate(ReflectionCache.PropertyOrField p) { if (p.MemberInfo is PropertyInfo && (!(obj is JsonSchema4) || !_jsonSchemaProperties.Contains(p.MemberInfo.Name)) && (!(obj is IDictionary) || p.MemberInfo.DeclaringType == obj.GetType()) && p.CanRead && !p.IsIndexer) return p.CustomAttributes.JsonIgnoreAttribute == null; return false; })) { object value = item.GetValue(obj); if (value != null) { TaskAwaiter val = AwaitExtensions.GetAwaiter(VisitAsync(value, path + "/" + item.GetName(), item.GetName(), checkedObjects, delegate(object o) { item.SetValue(obj, o); })); if (!val.get_IsCompleted()) { await val; TaskAwaiter val2 = default(TaskAwaiter); val = val2; val2 = default(TaskAwaiter); } val.GetResult(); } } } private void ReplaceOrDelete<T>(ICollection<T> collection, int index, T obj) { ((Collection<T>)collection).RemoveAt(index); if (obj != null) ((Collection<T>)collection).Insert(index, obj); } private void ReplaceOrDelete(IList collection, int index, object obj) { collection.RemoveAt(index); if (obj != null) collection.Insert(index, obj); } } }