JsonSchemaSerialization
The JSON Schema serialization context holding information about the current serialization.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace NJsonSchema.Infrastructure
{
public class JsonSchemaSerialization
{
[ThreadStatic]
private static SchemaType _currentSchemaType;
[ThreadStatic]
private static bool _isWriting;
[ThreadStatic]
private static JsonSerializerSettings _currentSerializerSettings;
public static SchemaType CurrentSchemaType {
get {
return _currentSchemaType;
}
private set {
_currentSchemaType = value;
}
}
public static JsonSerializerSettings CurrentSerializerSettings {
get {
return _currentSerializerSettings;
}
private set {
_currentSerializerSettings = value;
}
}
public static bool IsWriting {
get {
return _isWriting;
}
private set {
_isWriting = value;
}
}
public static string ToJson(object obj, SchemaType schemaType, IContractResolver contractResolver, Formatting formatting)
{
IsWriting = false;
CurrentSchemaType = schemaType;
JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(obj, false, contractResolver);
IsWriting = false;
CurrentSerializerSettings = new JsonSerializerSettings {
ContractResolver = contractResolver
};
string result = JsonConvert.SerializeObject(obj, formatting, CurrentSerializerSettings);
CurrentSerializerSettings = null;
CurrentSchemaType = SchemaType.JsonSchema;
return result;
}
[AsyncStateMachine(typeof(<FromJsonAsync>d__13<>))]
[Obsolete("Use FromJsonAsync with cancellation token instead.")]
public static Task<T> FromJsonAsync<T>(string json, SchemaType schemaType, string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver)
{
<FromJsonAsync>d__13<T> stateMachine = default(<FromJsonAsync>d__13<T>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create();
stateMachine.json = json;
stateMachine.schemaType = schemaType;
stateMachine.documentPath = documentPath;
stateMachine.referenceResolverFactory = referenceResolverFactory;
stateMachine.contractResolver = contractResolver;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<FromJsonAsync>d__14<>))]
public static Task<T> FromJsonAsync<T>(string json, SchemaType schemaType, string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver, CancellationToken cancellationToken = default(CancellationToken))
{
<FromJsonAsync>d__14<T> stateMachine = default(<FromJsonAsync>d__14<T>);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create();
stateMachine.json = json;
stateMachine.schemaType = schemaType;
stateMachine.documentPath = documentPath;
stateMachine.referenceResolverFactory = referenceResolverFactory;
stateMachine.contractResolver = contractResolver;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public static T FromJson<T>(string json, IContractResolver contractResolver)
{
IsWriting = true;
CurrentSerializerSettings = new JsonSerializerSettings {
ContractResolver = contractResolver,
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.None
};
T result = JsonConvert.DeserializeObject<T>(json, CurrentSerializerSettings);
CurrentSerializerSettings = null;
return result;
}
}
}