JsonReferenceVisitorBase
Visitor to transform an object with JsonSchema4 objects.
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
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 IContractResolver _contractResolver;
private readonly string[] _jsonSchemaProperties = (from p in typeof(JsonSchema4).GetRuntimeProperties()
select p.Name).ToArray();
protected JsonReferenceVisitorBase()
: this(new DefaultContractResolver())
{
}
protected JsonReferenceVisitorBase(IContractResolver contractResolver)
{
_contractResolver = contractResolver;
}
public virtual async Task VisitAsync(object obj)
{
await VisitAsync(obj, "#", null, new HashSet<object>(), delegate {
throw new NotSupportedException("Cannot replace the root.");
}).ConfigureAwait(false);
}
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) {
IJsonReference jsonReference2 = await VisitJsonReferenceAsync(reference, path, typeNameHint).ConfigureAwait(false);
if (jsonReference2 != reference) {
replacer(jsonReference2);
return;
}
}
JsonSchema4 schema;
if ((schema = (obj as JsonSchema4)) != null) {
if (schema.Reference != null)
await VisitAsync(schema.Reference, path, null, checkedObjects, delegate(object o) {
schema.Reference = (JsonSchema4)o;
}).ConfigureAwait(false);
if (schema.AdditionalItemsSchema != null)
await VisitAsync(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, delegate(object o) {
schema.AdditionalItemsSchema = (JsonSchema4)o;
}).ConfigureAwait(false);
if (schema.AdditionalPropertiesSchema != null)
await VisitAsync(schema.AdditionalPropertiesSchema, path + "/additionalProperties", null, checkedObjects, delegate(object o) {
schema.AdditionalPropertiesSchema = (JsonSchema4)o;
}).ConfigureAwait(false);
if (schema.Item != null)
await VisitAsync(schema.Item, path + "/items", null, checkedObjects, delegate(object o) {
schema.Item = (JsonSchema4)o;
}).ConfigureAwait(false);
for (int l = 0; l < schema.Items.Count; l++) {
int index = l;
await VisitAsync(schema.Items.ElementAt(l), path + "/items[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.Items, index, (JsonSchema4)o);
}).ConfigureAwait(false);
}
for (int l = 0; l < schema.AllOf.Count; l++) {
int index2 = l;
await VisitAsync(schema.AllOf.ElementAt(l), path + "/allOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.AllOf, index2, (JsonSchema4)o);
}).ConfigureAwait(false);
}
for (int l = 0; l < schema.AnyOf.Count; l++) {
int index3 = l;
await VisitAsync(schema.AnyOf.ElementAt(l), path + "/anyOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.AnyOf, index3, (JsonSchema4)o);
}).ConfigureAwait(false);
}
for (int l = 0; l < schema.OneOf.Count; l++) {
int index4 = l;
await VisitAsync(schema.OneOf.ElementAt(l), path + "/oneOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.OneOf, index4, (JsonSchema4)o);
}).ConfigureAwait(false);
}
if (schema.Not != null)
await VisitAsync(schema.Not, path + "/not", null, checkedObjects, delegate(object o) {
schema.Not = (JsonSchema4)o;
}).ConfigureAwait(false);
KeyValuePair<string, JsonProperty>[] array = schema.Properties.ToArray();
for (int l = 0; l < array.Length; l++) {
KeyValuePair<string, JsonProperty> p = array[l];
await VisitAsync(p.Value, path + "/properties/" + p.Key, p.Key, checkedObjects, delegate(object o) {
schema.Properties[p.Key] = (JsonProperty)o;
}).ConfigureAwait(false);
}
KeyValuePair<string, JsonSchema4>[] array2 = schema.PatternProperties.ToArray();
for (int l = 0; l < array2.Length; l++) {
KeyValuePair<string, JsonSchema4> p2 = array2[l];
await VisitAsync(p2.Value, path + "/patternProperties/" + p2.Key, null, checkedObjects, delegate(object o) {
schema.PatternProperties[p2.Key] = (JsonProperty)o;
}).ConfigureAwait(false);
}
array2 = schema.Definitions.ToArray();
for (int l = 0; l < array2.Length; l++) {
KeyValuePair<string, JsonSchema4> p3 = array2[l];
await 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);
}).ConfigureAwait(false);
}
}
if (!(obj is string) && !(obj is JToken)) {
IDictionary dictionary;
IList list;
if ((dictionary = (obj as IDictionary)) != null) {
await VisitPropertiesAsync(obj, path, checkedObjects).ConfigureAwait(false);
object[] array3 = dictionary.Keys.OfType<object>().ToArray();
foreach (object key in array3) {
await VisitAsync(dictionary[key], path + "/" + key, key.ToString(), checkedObjects, delegate(object o) {
if (o != null)
dictionary[key] = (JsonSchema4)o;
else
dictionary.Remove(key);
}).ConfigureAwait(false);
}
} else if ((list = (obj as IList)) != null) {
object[] array3 = list.OfType<object>().ToArray();
for (int l = 0; l < array3.Length; l++) {
int index5 = l;
await VisitAsync(array3[l], path + "[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(list, index5, o);
}).ConfigureAwait(false);
}
} else {
IEnumerable obj2 = obj as IEnumerable;
if (obj2 != null) {
object[] array3 = obj2.OfType<object>().ToArray();
for (int l = 0; l < array3.Length; l++) {
await VisitAsync(array3[l], path + "[" + l + "]", null, checkedObjects, delegate {
throw new NotSupportedException("Cannot replace enumerable item.");
}).ConfigureAwait(false);
}
} else
await VisitPropertiesAsync(obj, path, checkedObjects).ConfigureAwait(false);
}
}
}
}
private async Task VisitPropertiesAsync(object obj, string path, ISet<object> checkedObjects)
{
JsonObjectContract jsonObjectContract;
if ((jsonObjectContract = (_contractResolver.ResolveContract(obj.GetType()) as JsonObjectContract)) != null) {
foreach (Newtonsoft.Json.Serialization.JsonProperty item in jsonObjectContract.Properties.Where(delegate(Newtonsoft.Json.Serialization.JsonProperty p) {
if (!p.Ignored)
return p.ShouldSerialize?.Invoke(obj) ?? true;
return false;
})) {
object value = item.ValueProvider.GetValue(obj);
if (value != null)
await VisitAsync(value, path + "/" + item.PropertyName, item.PropertyName, checkedObjects, delegate(object o) {
item.ValueProvider.SetValue(obj, o);
}).ConfigureAwait(false);
}
} else {
foreach (ReflectionCache.PropertyOrField item2 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) || (object)p.MemberInfo.DeclaringType == obj.GetType()) && p.CanRead && !p.IsIndexer)
return p.CustomAttributes.JsonIgnoreAttribute == null;
return false;
})) {
object value2 = item2.GetValue(obj);
if (value2 != null)
await VisitAsync(value2, path + "/" + item2.GetName(), item2.GetName(), checkedObjects, delegate(object o) {
item2.SetValue(obj, o);
}).ConfigureAwait(false);
}
}
}
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);
}
}
}