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

JsonSchemaReferenceUtilities

public static class JsonSchemaReferenceUtilities
Provides utilities to resolve and set JSON schema references.
using Newtonsoft.Json.Linq; using NJsonSchema.Infrastructure; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace NJsonSchema { public static class JsonSchemaReferenceUtilities { public static void UpdateSchemaReferences(object root) { UpdateSchemaReferences(root, root, new HashSet<object>(), new JsonReferenceResolver()); } public static void UpdateSchemaReferences(object root, JsonReferenceResolver jsonReferenceResolver) { UpdateSchemaReferences(root, root, new HashSet<object>(), jsonReferenceResolver); } public static void UpdateSchemaReferencePaths(object root, ISchemaDefinitionAppender schemaDefinitionAppender) { UpdateSchemaReferencePaths(root, root, new HashSet<object>(), schemaDefinitionAppender); } public static string ConvertJsonReferences(string data) { return data.Replace("$ref", "schemaReferencePath"); } public static string ConvertPropertyReferences(string data) { return data.Replace("schemaReferencePath", "$ref"); } private static void UpdateSchemaReferencePaths(object root, object obj, HashSet<object> checkedObjects, ISchemaDefinitionAppender schemaDefinitionAppender) { if (obj != null && !(obj is string)) { JsonSchema4 jsonSchema = obj as JsonSchema4; if (jsonSchema != null && jsonSchema.SchemaReference != null) jsonSchema.SchemaReferencePath = JsonPathUtilities.GetJsonPath(root, jsonSchema.SchemaReference.ActualSchema, schemaDefinitionAppender); if (obj is IDictionary) { foreach (object item in ((IDictionary)obj).Values.OfType<object>().ToList()) { UpdateSchemaReferencePaths(root, item, checkedObjects, schemaDefinitionAppender); } } else if (obj is IEnumerable) { object[] array = ((IEnumerable)obj).OfType<object>().ToArray(); foreach (object obj2 in array) { UpdateSchemaReferencePaths(root, obj2, checkedObjects, schemaDefinitionAppender); } } if (!(obj is JToken)) { foreach (ReflectionCache.PropertyOrField item2 in ReflectionCache.GetProperties(obj.GetType()).Where(delegate(ReflectionCache.PropertyOrField p) { if (p.CanRead && !p.IsIndexer && p.MemberInfo is PropertyInfo) return p.CustomAttributes.JsonIgnoreAttribute == null; return false; })) { object value = item2.GetValue(obj); if (value != null && !checkedObjects.Contains(value)) { checkedObjects.Add(value); UpdateSchemaReferencePaths(root, value, checkedObjects, schemaDefinitionAppender); } } } } } private static void UpdateSchemaReferences(object root, object obj, HashSet<object> checkedObjects, JsonReferenceResolver jsonReferenceResolver) { if (obj != null && !(obj is string)) { JsonSchema4 jsonSchema = obj as JsonSchema4; if (jsonSchema != null && jsonSchema.SchemaReferencePath != null) jsonSchema.SchemaReference = jsonReferenceResolver.ResolveReference(root, jsonSchema.SchemaReferencePath); if (obj is IDictionary) { foreach (object value2 in ((IDictionary)obj).Values) { UpdateSchemaReferences(root, value2, checkedObjects, jsonReferenceResolver); } } else if (obj is IEnumerable) { object[] array = ((IEnumerable)obj).OfType<object>().ToArray(); foreach (object obj2 in array) { UpdateSchemaReferences(root, obj2, checkedObjects, jsonReferenceResolver); } } if (!(obj is JToken)) { foreach (ReflectionCache.PropertyOrField item in ReflectionCache.GetProperties(obj.GetType()).Where(delegate(ReflectionCache.PropertyOrField p) { if (p.CanRead && !p.IsIndexer && p.MemberInfo is PropertyInfo) return p.CustomAttributes.JsonIgnoreAttribute == null; return false; })) { object value = item.GetValue(obj); if (value != null && !checkedObjects.Contains(value)) { checkedObjects.Add(value); UpdateSchemaReferences(root, value, checkedObjects, jsonReferenceResolver); } } } } } } }