JsonSchemaReferenceUtilities
Provides utilities to resolve and set JSON schema references.
using NJsonSchema.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace NJsonSchema
{
public static class JsonSchemaReferenceUtilities
{
public static void UpdateSchemaReferences(object root)
{
UpdateSchemaReferences(root, root, new HashSet<object>());
}
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) {
foreach (object item2 in (IEnumerable)obj) {
UpdateSchemaReferencePaths(root, item2, checkedObjects, schemaDefinitionAppender);
}
} else {
foreach (ReflectionCache.Property item3 in ReflectionCache.GetProperties(obj.GetType()).Where(delegate(ReflectionCache.Property p) {
if (p.PropertyInfo.CanRead)
return p.CustomAttributes.JsonIgnoreAttribute == null;
return false;
})) {
object value = item3.PropertyInfo.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)
{
if (obj != null && !(obj is string)) {
JsonSchema4 jsonSchema = obj as JsonSchema4;
if (jsonSchema != null && jsonSchema.SchemaReferencePath != null)
jsonSchema.SchemaReference = JsonPathUtilities.GetObjectFromJsonPath(root, jsonSchema.SchemaReferencePath);
if (obj is IDictionary) {
foreach (object value2 in ((IDictionary)obj).Values) {
UpdateSchemaReferences(root, value2, checkedObjects);
}
} else if (obj is IEnumerable) {
foreach (object item in (IEnumerable)obj) {
UpdateSchemaReferences(root, item, checkedObjects);
}
} else {
foreach (ReflectionCache.Property item2 in ReflectionCache.GetProperties(obj.GetType()).Where(delegate(ReflectionCache.Property p) {
if (p.PropertyInfo.CanRead)
return p.CustomAttributes.JsonIgnoreAttribute == null;
return false;
})) {
object value = item2.PropertyInfo.GetValue(obj);
if (value != null && !checkedObjects.Contains(value)) {
checkedObjects.Add(value);
UpdateSchemaReferences(root, value, checkedObjects);
}
}
}
}
}
}
}