<PackageReference Include="NJsonSchema" Version="11.0.1" />

JsonSchemaSerialization

The JSON Schema serialization context holding information about the current serialization.
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.IO; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace NJsonSchema.Infrastructure { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class JsonSchemaSerialization { [ThreadStatic] private static SchemaType _currentSchemaType; [ThreadStatic] private static bool _isWriting; [System.Runtime.CompilerServices.Nullable(2)] [ThreadStatic] private static JsonSerializerSettings _currentSerializerSettings; public static SchemaType CurrentSchemaType { get { return _currentSchemaType; } private set { _currentSchemaType = value; } } [System.Runtime.CompilerServices.Nullable(2)] public static JsonSerializerSettings CurrentSerializerSettings { [System.Runtime.CompilerServices.NullableContext(2)] get { return _currentSerializerSettings; } [System.Runtime.CompilerServices.NullableContext(2)] 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; } [Obsolete("Use FromJsonAsync with cancellation token instead.")] public static Task<T> FromJsonAsync<T>(string json, SchemaType schemaType, [System.Runtime.CompilerServices.Nullable(2)] string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver) { return FromJsonAsync(json, schemaType, documentPath, referenceResolverFactory, contractResolver, CancellationToken.None); } public static Task<T> FromJsonAsync<T>(string json, SchemaType schemaType, [System.Runtime.CompilerServices.Nullable(2)] string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver, CancellationToken cancellationToken = default(CancellationToken)) { Func<T> loader = () => JsonSchemaSerialization.FromJson<T>(json, contractResolver); return FromJsonWithLoaderAsync(loader, schemaType, documentPath, referenceResolverFactory, contractResolver, cancellationToken); } public static Task<T> FromJsonAsync<T>(Stream stream, SchemaType schemaType, [System.Runtime.CompilerServices.Nullable(2)] string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver, CancellationToken cancellationToken = default(CancellationToken)) { Func<T> loader = () => JsonSchemaSerialization.FromJson<T>(stream, contractResolver); return FromJsonWithLoaderAsync(loader, schemaType, documentPath, referenceResolverFactory, contractResolver, cancellationToken); } [AsyncStateMachine(typeof(<FromJsonWithLoaderAsync>d__16<>))] private static Task<T> FromJsonWithLoaderAsync<T>(Func<T> loader, SchemaType schemaType, [System.Runtime.CompilerServices.Nullable(2)] string documentPath, Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver, CancellationToken cancellationToken) { <FromJsonWithLoaderAsync>d__16<T> stateMachine = default(<FromJsonWithLoaderAsync>d__16<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.loader = loader; 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; } [return: System.Runtime.CompilerServices.Nullable(2)] public static T FromJson<[System.Runtime.CompilerServices.Nullable(2)] T>(string json, IContractResolver contractResolver) { IsWriting = true; UpdateCurrentSerializerSettings<T>(contractResolver); try { return JsonConvert.DeserializeObject<T>(json, CurrentSerializerSettings); } finally { CurrentSerializerSettings = null; } } [return: System.Runtime.CompilerServices.Nullable(2)] public static T FromJson<[System.Runtime.CompilerServices.Nullable(2)] T>(Stream stream, IContractResolver contractResolver) { IsWriting = true; UpdateCurrentSerializerSettings<T>(contractResolver); try { using (StreamReader reader = new StreamReader(stream)) using (JsonTextReader reader2 = new JsonTextReader(reader)) { JsonSerializer jsonSerializer = JsonSerializer.Create(CurrentSerializerSettings); return jsonSerializer.Deserialize<T>(reader2); } } finally { CurrentSerializerSettings = null; } } private static void UpdateCurrentSerializerSettings<[System.Runtime.CompilerServices.Nullable(2)] T>(IContractResolver contractResolver) { CurrentSerializerSettings = new JsonSerializerSettings { ContractResolver = contractResolver, MetadataPropertyHandling = MetadataPropertyHandling.Ignore, ConstructorHandling = ConstructorHandling.Default, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, PreserveReferencesHandling = PreserveReferencesHandling.None, MaxDepth = new int?(128) }; } } }