AsyncJsonReferenceVisitorBase
Visitor to transform an object with JsonSchema objects.
using Microsoft.Runtime.CompilerServices;
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.Threading.Tasks;
namespace NJsonSchema.Visitors
{
public abstract class AsyncJsonReferenceVisitorBase
{
private readonly IContractResolver _contractResolver;
protected AsyncJsonReferenceVisitorBase()
: this(new DefaultContractResolver())
{
}
protected AsyncJsonReferenceVisitorBase(IContractResolver contractResolver)
{
_contractResolver = contractResolver;
}
public virtual async Task VisitAsync(object obj)
{
ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(VisitAsync(obj, "#", null, new HashSet<object>(), delegate {
throw new NotSupportedException("Cannot replace the root.");
}), false);
ConfiguredTaskAwaiter val2 = val.GetAwaiter();
if (!val2.get_IsCompleted()) {
await val2;
ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter);
val2 = val3;
}
val2.GetResult();
}
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 reference = obj as IJsonReference;
if (reference != null) {
ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<IJsonReference>(VisitJsonReferenceAsync(reference, path, typeNameHint), false).GetAwaiter();
if (!val.get_IsCompleted()) {
await val;
ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter);
val = val2;
}
IJsonReference result = (IJsonReference)val.GetResult();
if (result != reference) {
replacer(result);
return;
}
}
JsonSchema schema = obj as JsonSchema;
ConfiguredTaskAwaitable val3;
ConfiguredTaskAwaiter val4;
ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter);
if (schema != null) {
if (schema.Reference != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.Reference, path, null, checkedObjects, delegate(object o) {
schema.Reference = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.AdditionalItemsSchema != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, delegate(object o) {
schema.AdditionalItemsSchema = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.AdditionalPropertiesSchema != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.AdditionalPropertiesSchema, path + "/additionalProperties", null, checkedObjects, delegate(object o) {
schema.AdditionalPropertiesSchema = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.Item != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.Item, path + "/items", null, checkedObjects, delegate(object o) {
schema.Item = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
for (int l = 0; l < schema.Items.Count; l++) {
int index = l;
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.Items.ElementAt(l), path + "/items[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.Items, index, (JsonSchema)o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
for (int l = 0; l < schema.AllOf.Count; l++) {
int index2 = l;
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.AllOf.ElementAt(l), path + "/allOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.AllOf, index2, (JsonSchema)o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
for (int l = 0; l < schema.AnyOf.Count; l++) {
int index3 = l;
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.AnyOf.ElementAt(l), path + "/anyOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.AnyOf, index3, (JsonSchema)o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
for (int l = 0; l < schema.OneOf.Count; l++) {
int index4 = l;
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.OneOf.ElementAt(l), path + "/oneOf[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(schema.OneOf, index4, (JsonSchema)o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.Not != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.Not, path + "/not", null, checkedObjects, delegate(object o) {
schema.Not = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.DictionaryKey != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.DictionaryKey, path + "/x-dictionaryKey", null, checkedObjects, delegate(object o) {
schema.DictionaryKey = (JsonSchema)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
if (schema.DiscriminatorRaw != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(schema.DiscriminatorRaw, path + "/discriminator", null, checkedObjects, delegate(object o) {
schema.DiscriminatorRaw = o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
KeyValuePair<string, JsonSchemaProperty>[] array = schema.Properties.ToArray();
for (int l = 0; l < array.Length; l++) {
KeyValuePair<string, JsonSchemaProperty> p2 = array[l];
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(p2.Value, path + "/properties/" + p2.Key, p2.Key, checkedObjects, delegate(object o) {
schema.Properties[p2.Key] = (JsonSchemaProperty)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
array = schema.PatternProperties.ToArray();
for (int l = 0; l < array.Length; l++) {
KeyValuePair<string, JsonSchemaProperty> p3 = array[l];
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(p3.Value, path + "/patternProperties/" + p3.Key, null, checkedObjects, delegate(object o) {
schema.PatternProperties[p3.Key] = (JsonSchemaProperty)o;
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
KeyValuePair<string, JsonSchema>[] array2 = schema.Definitions.ToArray();
for (int l = 0; l < array2.Length; l++) {
KeyValuePair<string, JsonSchema> p4 = array2[l];
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(p4.Value, path + "/definitions/" + p4.Key, p4.Key, checkedObjects, delegate(object o) {
if (o != null)
schema.Definitions[p4.Key] = (JsonSchema)o;
else
schema.Definitions.Remove(p4.Key);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
}
if (!(obj is string) && !(obj is JToken) && obj.GetType() != typeof(JsonSchema)) {
JsonObjectContract jsonObjectContract = _contractResolver.ResolveContract(obj.GetType()) as JsonObjectContract;
if (jsonObjectContract != null) {
foreach (JsonProperty item in jsonObjectContract.Properties.Where(delegate(JsonProperty p) {
if ((!(obj is JsonSchema) || !JsonSchema.JsonSchemaPropertiesCache.Contains(p.UnderlyingName)) && !p.Ignored)
return p.ShouldSerialize?.Invoke(obj) ?? true;
return false;
})) {
object value = item.ValueProvider.GetValue(obj);
if (value != null) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(value, path + "/" + item.PropertyName, item.PropertyName, checkedObjects, delegate(object o) {
item.ValueProvider.SetValue(obj, o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
}
} else {
IDictionary dictionary = obj as IDictionary;
if (dictionary != null) {
object[] array3 = dictionary.Keys.OfType<object>().ToArray();
foreach (object key in array3) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(dictionary[key], path + "/" + key, key.ToString(), checkedObjects, delegate(object o) {
if (o != null)
dictionary[key] = (JsonSchema)o;
else
dictionary.Remove(key);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
} else {
IList list = obj as IList;
if (list != null) {
object[] array3 = list.OfType<object>().ToArray();
for (int l = 0; l < array3.Length; l++) {
int index5 = l;
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(array3[l], path + "[" + l + "]", null, checkedObjects, delegate(object o) {
ReplaceOrDelete(list, index5, o);
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
} else {
IEnumerable enumerable = obj as IEnumerable;
if (enumerable != null) {
object[] array3 = enumerable.OfType<object>().ToArray();
for (int l = 0; l < array3.Length; l++) {
val3 = AwaitExtensions.ConfigureAwait(VisitAsync(array3[l], path + "[" + l + "]", null, checkedObjects, delegate {
throw new NotSupportedException("Cannot replace enumerable item.");
}), false);
val4 = val3.GetAwaiter();
if (!val4.get_IsCompleted()) {
await val4;
val4 = val5;
val5 = default(ConfiguredTaskAwaiter);
}
val4.GetResult();
}
}
}
}
}
}
}
}
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);
}
}
}