JsonSerializerContext
Provides metadata about a set of types that is relevant to JSON serialization.
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class JsonSerializerContext : IJsonTypeInfoResolver
{
private JsonSerializerOptions _options;
public JsonSerializerOptions Options {
get {
JsonSerializerOptions jsonSerializerOptions = _options;
if (jsonSerializerOptions == null) {
jsonSerializerOptions = new JsonSerializerOptions {
TypeInfoResolver = this
};
jsonSerializerOptions.MakeReadOnly();
_options = jsonSerializerOptions;
}
return jsonSerializerOptions;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
protected abstract JsonSerializerOptions GeneratedSerializerOptions {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
internal bool CanUseFastPathSerializationLogic(JsonSerializerOptions options)
{
if (GeneratedSerializerOptions != null && options.Converters.Count == 0 && options.Encoder == null && (options.NumberHandling & (JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowNamedFloatingPointLiterals)) == JsonNumberHandling.Strict && options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.None && !options.IgnoreNullValues && options.DefaultIgnoreCondition == GeneratedSerializerOptions.DefaultIgnoreCondition && options.IgnoreReadOnlyFields == GeneratedSerializerOptions.IgnoreReadOnlyFields && options.IgnoreReadOnlyProperties == GeneratedSerializerOptions.IgnoreReadOnlyProperties && options.IncludeFields == GeneratedSerializerOptions.IncludeFields && options.PropertyNamingPolicy == GeneratedSerializerOptions.PropertyNamingPolicy && options.DictionaryKeyPolicy == GeneratedSerializerOptions.DictionaryKeyPolicy)
return options.WriteIndented == GeneratedSerializerOptions.WriteIndented;
return false;
}
[System.Runtime.CompilerServices.NullableContext(2)]
protected JsonSerializerContext(JsonSerializerOptions options)
{
if (options != null) {
options.VerifyMutable();
options.TypeInfoResolver = this;
options.MakeReadOnly();
_options = options;
}
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public abstract JsonTypeInfo GetTypeInfo(Type type);
JsonTypeInfo IJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
{
if (options != null && options != _options)
ThrowHelper.ThrowInvalidOperationException_ResolverTypeInfoOptionsNotCompatible();
return GetTypeInfo(type);
}
}
}