JsonReferenceVisitorBase
Visitor to transform an object with JsonSchema4 objects.
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 typeof(JsonSchema4).GetRuntimeProperties()
select p.Name).ToArray();
public virtual async Task VisitAsync(object obj)
{
await VisitAsync(obj, "#", null, new HashSet<object>(), delegate {
throw new NotSupportedException("Cannot replace the root.");
});
}
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);
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;
});
if (schema.AdditionalItemsSchema != null)
await VisitAsync(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, delegate(object o) {
schema.AdditionalItemsSchema = (JsonSchema4)o;
});
if (schema.AdditionalPropertiesSchema != null)
await VisitAsync(schema.AdditionalPropertiesSchema, path + "/additionalProperties", null, checkedObjects, delegate(object o) {
schema.AdditionalPropertiesSchema = (JsonSchema4)o;
});
if (schema.Item != null)
await VisitAsync(schema.Item, path + "/items", null, checkedObjects, delegate(object o) {
schema.Item = (JsonSchema4)o;
});
for (int k = 0; k < schema.Items.Count; k++) {
int index = k;
await VisitAsync(schema.Items.ElementAt(k), path + "/items[" + k + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.Items, index, (JsonSchema4)o);
});
}
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);
});
}
for (int m = 0; m < schema.AnyOf.Count; m++) {
int index3 = m;
await VisitAsync(schema.AnyOf.ElementAt(m), path + "/anyOf[" + m + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.AnyOf, index3, (JsonSchema4)o);
});
}
for (int n = 0; n < schema.OneOf.Count; n++) {
int index4 = n;
await VisitAsync(schema.OneOf.ElementAt(n), path + "/oneOf[" + n + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.OneOf, index4, (JsonSchema4)o);
});
}
if (schema.Not != null)
await VisitAsync(schema.Not, path + "/not", null, checkedObjects, delegate(object o) {
schema.Not = (JsonSchema4)o;
});
KeyValuePair<string, JsonProperty>[] array = schema.Properties.ToArray();
for (int num = 0; num < array.Length; num++) {
KeyValuePair<string, JsonProperty> p = array[num];
await VisitAsync(p.Value, path + "/properties/" + p.Key, p.Key, checkedObjects, delegate(object o) {
schema.Properties[p.Key] = (JsonProperty)o;
});
}
KeyValuePair<string, JsonSchema4>[] array2 = schema.PatternProperties.ToArray();
for (int num = 0; num < array2.Length; num++) {
KeyValuePair<string, JsonSchema4> p2 = array2[num];
await VisitAsync(p2.Value, path + "/patternProperties/" + p2.Key, null, checkedObjects, delegate(object o) {
schema.PatternProperties[p2.Key] = (JsonProperty)o;
});
}
array2 = schema.Definitions.ToArray();
for (int num = 0; num < array2.Length; num++) {
KeyValuePair<string, JsonSchema4> p3 = array2[num];
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);
});
}
}
if (!(obj is string) && !(obj is JToken)) {
IDictionary dictionary;
IList list;
if ((dictionary = (obj as IDictionary)) != null) {
await VisitPropertiesAsync(obj, path, checkedObjects);
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);
});
}
} else if ((list = (obj as IList)) != null) {
object[] items2 = list.OfType<object>().ToArray();
for (int j = 0; j < items2.Length; j++) {
int index5 = j;
await VisitAsync(items2[j], path + "[" + j + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(list, index5, o);
});
}
} else {
IEnumerable obj2 = obj as IEnumerable;
if (obj2 != null) {
object[] items = obj2.OfType<object>().ToArray();
for (int i = 0; i < items.Length; i++) {
await VisitAsync(items[i], path + "[" + i + "]", null, checkedObjects, delegate {
throw new NotSupportedException("Cannot replace enumerable item.");
});
}
} else
await VisitPropertiesAsync(obj, path, checkedObjects);
}
}
}
}
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) || (object)p.MemberInfo.DeclaringType == obj.GetType()) && p.CanRead && !p.IsIndexer)
return p.CustomAttributes.JsonIgnoreAttribute == null;
return false;
})) {
object value = item.GetValue(obj);
if (value != null)
await VisitAsync(value, path + "/" + item.GetName(), item.GetName(), checkedObjects, delegate(object o) {
item.SetValue(obj, o);
});
}
}
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);
}
}
}