<PackageReference Include="NJsonSchema" Version="11.0.0-preview007" />

JsonReferenceVisitorBase

public abstract class JsonReferenceVisitorBase
Visitor to transform an object with JsonSchema objects.
using Namotion.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using NJsonSchema.References; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.CompilerServices; namespace NJsonSchema.Visitors { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public abstract class JsonReferenceVisitorBase { private readonly IContractResolver _contractResolver; protected JsonReferenceVisitorBase() : this(new DefaultContractResolver()) { } protected JsonReferenceVisitorBase(IContractResolver contractResolver) { _contractResolver = contractResolver; } public virtual void Visit(object obj) { Visit(obj, "#", null, new HashSet<object>(), delegate { throw new NotSupportedException("Cannot replace the root."); }); } protected abstract IJsonReference VisitJsonReference(IJsonReference reference, string path, [System.Runtime.CompilerServices.Nullable(2)] string typeNameHint); protected virtual void Visit(object obj, string path, [System.Runtime.CompilerServices.Nullable(2)] string typeNameHint, ISet<object> checkedObjects, Action<object> replacer) { if (obj != null && checkedObjects.Add(obj)) { IJsonReference jsonReference = obj as IJsonReference; if (jsonReference != null) { IJsonReference jsonReference2 = VisitJsonReference(jsonReference, path, typeNameHint); if (jsonReference2 != jsonReference) { replacer(jsonReference2); return; } } JsonSchema schema = obj as JsonSchema; if (schema != null) { if (schema.Reference != null) Visit(schema.Reference, path, null, checkedObjects, delegate(object o) { schema.Reference = (JsonSchema)o; }); if (schema.AdditionalItemsSchema != null) Visit(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, delegate(object o) { schema.AdditionalItemsSchema = (JsonSchema)o; }); if (schema.AdditionalPropertiesSchema != null) Visit(schema.AdditionalPropertiesSchema, path + "/additionalProperties", null, checkedObjects, delegate(object o) { schema.AdditionalPropertiesSchema = (JsonSchema)o; }); if (schema.Item != null) Visit(schema.Item, path + "/items", null, checkedObjects, delegate(object o) { schema.Item = (JsonSchema)o; }); ObservableCollection<JsonSchema> items = schema._items; for (int i = 0; i < items.Count; i++) { int index = i; Visit(items[i], $"{path}""{i}""", null, checkedObjects, delegate(object o) { ReplaceOrDelete(items, index, (JsonSchema)o); }); } ObservableCollection<JsonSchema> allOf = schema._allOf; for (int j = 0; j < allOf.Count; j++) { int index2 = j; Visit(allOf[j], $"{path}""{j}""", null, checkedObjects, delegate(object o) { ReplaceOrDelete(allOf, index2, (JsonSchema)o); }); } ObservableCollection<JsonSchema> anyOf = schema._anyOf; for (int k = 0; k < anyOf.Count; k++) { int index3 = k; Visit(anyOf[k], $"{path}""{k}""", null, checkedObjects, delegate(object o) { ReplaceOrDelete(anyOf, index3, (JsonSchema)o); }); } ObservableCollection<JsonSchema> oneOf = schema._oneOf; for (int l = 0; l < oneOf.Count; l++) { int index4 = l; Visit(oneOf[l], $"{path}""{l}""", null, checkedObjects, delegate(object o) { ReplaceOrDelete(oneOf, index4, (JsonSchema)o); }); } if (schema.Not != null) Visit(schema.Not, path + "/not", null, checkedObjects, delegate(object o) { schema.Not = (JsonSchema)o; }); if (schema.DictionaryKey != null) Visit(schema.DictionaryKey, path + "/x-dictionaryKey", null, checkedObjects, delegate(object o) { schema.DictionaryKey = (JsonSchema)o; }); if (schema.DiscriminatorRaw != null) Visit(schema.DiscriminatorRaw, path + "/discriminator", null, checkedObjects, delegate(object o) { schema.DiscriminatorRaw = o; }); KeyValuePair<string, JsonSchemaProperty>[] array = schema.Properties.ToArray(); for (int m = 0; m < array.Length; m++) { KeyValuePair<string, JsonSchemaProperty> p = array[m]; Visit(p.Value, path + "/properties/" + p.Key, p.Key, checkedObjects, delegate(object o) { schema.Properties[p.Key] = (JsonSchemaProperty)o; }); } KeyValuePair<string, JsonSchemaProperty>[] array2 = schema.PatternProperties.ToArray(); for (int n = 0; n < array2.Length; n++) { KeyValuePair<string, JsonSchemaProperty> p2 = array2[n]; Visit(p2.Value, path + "/patternProperties/" + p2.Key, null, checkedObjects, delegate(object o) { schema.PatternProperties[p2.Key] = (JsonSchemaProperty)o; }); } KeyValuePair<string, JsonSchema>[] array3 = schema.Definitions.ToArray(); for (int num = 0; num < array3.Length; num++) { KeyValuePair<string, JsonSchema> p3 = array3[num]; Visit(p3.Value, path + "/definitions/" + p3.Key, p3.Key, checkedObjects, delegate(object o) { if (o != null) schema.Definitions[p3.Key] = (JsonSchema)o; else schema.Definitions.Remove(p3.Key); }); } } if (!(obj is string) && !(obj is JToken) && obj.GetType() != typeof(JsonSchema)) { JsonObjectContract jsonObjectContract = _contractResolver.ResolveContract(obj.GetType()) as JsonObjectContract; if (jsonObjectContract != null) { foreach (JsonProperty property3 in jsonObjectContract.Properties) { if ((!(obj is JsonSchema) || !JsonSchema.JsonSchemaPropertiesCache.Contains(property3.UnderlyingName)) && !property3.Ignored && (property3.ShouldSerialize?.Invoke(obj) ?? true)) { object obj2 = property3.ValueProvider?.GetValue(obj); if (obj2 != null) Visit(obj2, path + "/" + property3.PropertyName, property3.PropertyName, checkedObjects, delegate(object o) { property3.ValueProvider?.SetValue(obj, o); }); } } } else { IDictionary dictionary = obj as IDictionary; if (dictionary != null) { object[] array4 = dictionary.Keys.OfType<object>().ToArray(); foreach (object key in array4) { object obj3 = dictionary[key]; if (obj3 != null) Visit(obj3, $"{path}""{key}", key.ToString(), checkedObjects, delegate(object o) { if (o != null) dictionary[key] = (JsonSchema)o; else dictionary.Remove(key); }); } ContextualType val = ContextualTypeExtensions.ToContextualType(obj.GetType()); if (AttributeExtensions.IsAttributeDefined<JsonConverterAttribute>(val, true)) { ContextualPropertyInfo[] contextualProperties = ContextualTypeExtensions.GetContextualProperties(val.get_Type()); foreach (ContextualPropertyInfo property2 in contextualProperties) { if (property2.get_MemberInfo().DeclaringType == val.get_Type() && !AttributeExtensions.IsAttributeDefined<JsonIgnoreAttribute>(property2, true)) { object value = property2.GetValue(obj); if (value != null) Visit(value, path + "/" + property2.get_Name(), property2.get_Name(), checkedObjects, delegate(object o) { property2.SetValue(obj, o); }); } } } } else { IList list = obj as IList; if (list != null) { object[] array5 = list.OfType<object>().ToArray(); for (int num4 = 0; num4 < array5.Length; num4++) { int index5 = num4; Visit(array5[num4], $"{path}""{num4}""", null, checkedObjects, delegate(object o) { ReplaceOrDelete(list, index5, o); }); } } else { IEnumerable enumerable = obj as IEnumerable; if (enumerable != null) { object[] array6 = enumerable.OfType<object>().ToArray(); for (int num5 = 0; num5 < array6.Length; num5++) { Visit(array6[num5], $"{path}""{num5}""", null, checkedObjects, delegate { throw new NotSupportedException("Cannot replace enumerable item."); }); } } } } } } } } private static void ReplaceOrDelete<[System.Runtime.CompilerServices.Nullable(2)] T>(ObservableCollection<T> collection, int index, T obj) { ((Collection<T>)collection).RemoveAt(index); if (obj != null) ((Collection<T>)collection).Insert(index, obj); } private static void ReplaceOrDelete(IList collection, int index, object obj) { collection.RemoveAt(index); if (obj != null) collection.Insert(index, obj); } } }