}
public static partial class JsonSerializer
{
+ public static bool IsReflectionEnabledByDefault { get { throw null; } }
public static object? Deserialize(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static TValue? Deserialize<TValue>(ref System.Text.Json.Utf8JsonReader reader, System.Text.Json.JsonSerializerOptions? options = null) { throw null; }
public static TValue? Deserialize<TValue>(ref System.Text.Json.Utf8JsonReader reader, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo) { throw null; }
- public static bool IsReflectionEnabledByDefault { get { throw null; } }
public static void Serialize(System.IO.Stream utf8Json, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public System.Text.Json.Serialization.JsonUnmappedMemberHandling UnmappedMemberHandling { get { throw null; } set { } }
public bool WriteIndented { get { throw null; } set { } }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
- [System.ObsoleteAttribute("JsonSerializerOptions.AddContext is obsolete. To register a JsonSerializerContext, use either the TypeInfoResolver or TypeInfoResolverChain properties.", DiagnosticId = "SYSLIB0049", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.ObsoleteAttribute("JsonSerializerOptions.AddContext is obsolete. To register a JsonSerializerContext, use either the TypeInfoResolver or TypeInfoResolverChain properties.", DiagnosticId="SYSLIB0049", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public void AddContext<TContext>() where TContext : System.Text.Json.Serialization.JsonSerializerContext, new() { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Getting a converter for a type may require reflection which depends on runtime code generation.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Getting a converter for a type may require reflection which depends on unreferenced code.")]
public System.Text.Json.Serialization.JsonConverter GetConverter(System.Type typeToConvert) { throw null; }
public System.Text.Json.Serialization.Metadata.JsonTypeInfo GetTypeInfo(System.Type type) { throw null; }
- public bool TryGetTypeInfo(System.Type type, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Text.Json.Serialization.Metadata.JsonTypeInfo typeInfo) { throw null; }
public void MakeReadOnly() { }
+ public bool TryGetTypeInfo(System.Type type, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Text.Json.Serialization.Metadata.JsonTypeInfo? typeInfo) { throw null; }
}
public enum JsonTokenType : byte
{
public void WritePropertyName(System.ReadOnlySpan<char> propertyName) { }
public void WritePropertyName(string propertyName) { }
public void WritePropertyName(System.Text.Json.JsonEncodedText propertyName) { }
- public void WriteRawValue(System.ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false) { }
public void WriteRawValue(System.Buffers.ReadOnlySequence<byte> utf8Json, bool skipInputValidation = false) { }
+ public void WriteRawValue(System.ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false) { }
public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan<char> json, bool skipInputValidation = false) { }
public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, bool skipInputValidation = false) { }
public void WriteStartArray() { }
public abstract partial class JsonConverter
{
internal JsonConverter() { }
+ public abstract System.Type? Type { get; }
public abstract bool CanConvert(System.Type typeToConvert);
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)]
public abstract partial class JsonConverterFactory : System.Text.Json.Serialization.JsonConverter
{
protected JsonConverterFactory() { }
+ public sealed override System.Type? Type { get { throw null; } }
public abstract System.Text.Json.Serialization.JsonConverter? CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);
}
public abstract partial class JsonConverter<T> : System.Text.Json.Serialization.JsonConverter
{
protected internal JsonConverter() { }
public virtual bool HandleNull { get { throw null; } }
+ public sealed override System.Type Type { get { throw null; } }
public override bool CanConvert(System.Type typeToConvert) { throw null; }
public abstract T? Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);
public virtual T ReadAsPropertyName(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
Replace = 0,
Populate = 1,
}
- [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)]
+ [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)]
public sealed partial class JsonObjectCreationHandlingAttribute : System.Text.Json.Serialization.JsonAttribute
{
public JsonObjectCreationHandlingAttribute(System.Text.Json.Serialization.JsonObjectCreationHandling handling) { }
{
internal JsonPropertyInfo() { }
public System.Reflection.ICustomAttributeProvider? AttributeProvider { get { throw null; } set { } }
- public System.Text.Json.Serialization.JsonObjectCreationHandling? ObjectCreationHandling { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonConverter? CustomConverter { get { throw null; } set { } }
public System.Func<object, object?>? Get { get { throw null; } set { } }
public bool IsExtensionData { get { throw null; } set { } }
public bool IsRequired { get { throw null; } set { } }
public string Name { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonNumberHandling? NumberHandling { get { throw null; } set { } }
+ public System.Text.Json.Serialization.JsonObjectCreationHandling? ObjectCreationHandling { get { throw null; } set { } }
public System.Text.Json.JsonSerializerOptions Options { get { throw null; } }
public int Order { get { throw null; } set { } }
public System.Type PropertyType { get { throw null; } }
public bool IsReadOnly { get { throw null; } }
public System.Text.Json.Serialization.Metadata.JsonTypeInfoKind Kind { get { throw null; } }
public System.Text.Json.Serialization.JsonNumberHandling? NumberHandling { get { throw null; } set { } }
- public System.Text.Json.Serialization.JsonObjectCreationHandling? PreferredPropertyObjectCreationHandling { get { throw null; } set { } }
public System.Action<object>? OnDeserialized { get { throw null; } set { } }
public System.Action<object>? OnDeserializing { get { throw null; } set { } }
public System.Action<object>? OnSerialized { get { throw null; } set { } }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver? OriginatingResolver { get { throw null; } set { } }
public System.Text.Json.Serialization.Metadata.JsonPolymorphismOptions? PolymorphismOptions { get { throw null; } set { } }
+ public System.Text.Json.Serialization.JsonObjectCreationHandling? PreferredPropertyObjectCreationHandling { get { throw null; } set { } }
public System.Collections.Generic.IList<System.Text.Json.Serialization.Metadata.JsonPropertyInfo> Properties { get { throw null; } }
public System.Type Type { get { throw null; } }
public System.Text.Json.Serialization.JsonUnmappedMemberHandling? UnmappedMemberHandling { get { throw null; } set { } }
internal CastingConverter(JsonConverter sourceConverter)
{
- Debug.Assert(typeof(T).IsInSubtypeRelationshipWith(sourceConverter.TypeToConvert));
+ Debug.Assert(typeof(T).IsInSubtypeRelationshipWith(sourceConverter.Type!));
Debug.Assert(sourceConverter.SourceConverterForCastingConverter is null, "casting converters should not be layered.");
_sourceConverter = sourceConverter;
{
if (!typeToConvert.IsAssignableFrom(typeof(IAsyncEnumerable<TElement>)))
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
return base.OnTryRead(ref reader, typeToConvert, options, ref state, out value!);
{
if (!state.SupportAsync)
{
- ThrowHelper.ThrowNotSupportedException_TypeRequiresAsyncSerialization(TypeToConvert);
+ ThrowHelper.ThrowNotSupportedException_TypeRequiresAsyncSerialization(Type);
}
return base.OnTryWrite(writer, value, options, ref state);
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as List<T> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(List<TElement>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<TElement>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new List<TElement>();
}
}
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as Dictionary<TKey,TValue> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(Dictionary<string, object?>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(Dictionary<string, object?>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new Dictionary<string, object?>();
}
}
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as Dictionary<TKey,TValue> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(Dictionary<TKey, TValue>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(Dictionary<TKey, TValue>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new Dictionary<TKey, TValue>();
}
}
{
if (!_isDeserializable)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
state.Current.ReturnValue = new List<object?>();
{
if (!_isDeserializable)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
state.Current.ReturnValue = new List<TElement>();
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as List<object?> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(List<object?>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<object?>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new List<object?>();
}
}
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as List<T> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(List<TElement>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<TElement>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new List<TElement>();
}
}
{
if (!_isDeserializable)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
state.Current.ReturnValue = new Dictionary<TKey, TValue>();
if (returnValue.IsReadOnly)
{
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
// Deserialize as HashSet<TElement> for interface types that support it.
- if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(HashSet<TElement>)))
+ if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(HashSet<TElement>)))
{
- Debug.Assert(TypeToConvert.IsInterface);
+ Debug.Assert(Type.IsInterface);
jsonTypeInfo.CreateObject = () => new HashSet<TElement>();
}
}
// The contract model was not able to produce a default constructor for two possible reasons:
// 1. Either the declared collection type is abstract and cannot be instantiated.
// 2. The collection type does not specify a default constructor.
- if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
+ if (Type.IsAbstract || Type.IsInterface)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
else
{
- ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(Type, ref reader, ref state);
}
}
if (reader.TokenType != JsonTokenType.StartArray)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
CreateCollection(ref reader, ref state, options);
}
// Obtain the CLR value from the JSON and apply to the object.
- TElement? element = elementConverter.Read(ref reader, elementConverter.TypeToConvert, options);
+ TElement? element = elementConverter.Read(ref reader, elementConverter.Type, options);
Add(element!, ref state);
}
}
{
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
state.Current.ObjectState = StackFrameObjectState.StartToken;
}
else
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo;
ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter)
{
Debug.Assert(!IsValueType);
- bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.TypeToConvert, options, ref state, out object? objectResult);
+ bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, options, ref state, out object? objectResult);
value = (TCollection)objectResult!;
state.ExitPolymorphicConverter(success);
return success;
// The contract model was not able to produce a default constructor for two possible reasons:
// 1. Either the declared collection type is abstract and cannot be instantiated.
// 2. The collection type does not specify a default constructor.
- if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
+ if (Type.IsAbstract || Type.IsInterface)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
else
{
- ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(Type, ref reader, ref state);
}
}
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
CreateCollection(ref reader, ref state);
{
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
state.Current.ObjectState = StackFrameObjectState.StartToken;
ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter)
{
Debug.Assert(!IsValueType);
- bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.TypeToConvert, options, ref state, out object? objectResult);
+ bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, options, ref state, out object? objectResult);
value = (TDictionary)objectResult!;
state.ExitPolymorphicConverter(success);
return success;
state.Current.JsonPropertyNameAsString = unescapedPropertyNameAsString; // Copy key name for JSON Path support in case of error.
// Special case string to avoid calling GetString twice and save one allocation.
- if (keyConverter.IsInternalConverter && keyConverter.TypeToConvert == typeof(string))
+ if (keyConverter.IsInternalConverter && keyConverter.Type == typeof(string))
{
key = (TKey)(object)unescapedPropertyNameAsString;
}
else
{
- key = keyConverter.ReadAsPropertyNameCore(ref reader, keyConverter.TypeToConvert, options);
+ key = keyConverter.ReadAsPropertyNameCore(ref reader, keyConverter.Type, options);
}
return key;
if (constructorDelegate == null)
{
- ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
+ ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
state.Current.ReturnValue = constructorDelegate();
public sealed override object ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
+ ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type, this);
return null!;
}
internal sealed override object ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
+ ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type, this);
return null!;
}
}
Type runtimeType = value.GetType();
- if (runtimeType == TypeToConvert)
+ if (runtimeType == Type)
{
ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(runtimeType, this);
}
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
state.Current.ObjectState = StackFrameObjectState.StartToken;
ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter)
{
Debug.Assert(!IsValueType);
- bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.TypeToConvert, options, ref state, out object? objectResult);
+ bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, options, ref state, out object? objectResult);
value = (T)objectResult!;
state.ExitPolymorphicConverter(success);
return success;
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
state.Current.ObjectState = StackFrameObjectState.StartToken;
ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter)
{
Debug.Assert(!IsValueType);
- bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.TypeToConvert, options, ref state, out object? objectResult);
+ bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, options, ref state, out object? objectResult);
value = (T)objectResult!;
state.ExitPolymorphicConverter(success);
return success;
if (jsonTypeInfo.ParameterCount != jsonTypeInfo.ParameterCache!.Count)
{
- ThrowHelper.ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(TypeToConvert);
+ ThrowHelper.ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(Type);
}
state.Current.InitializeRequiredPropertiesValidationState(jsonTypeInfo);
string[] names = Enum.GetNames<T>();
T[] values = Enum.GetValues<T>();
#else
- string[] names = Enum.GetNames(TypeToConvert);
- Array values = Enum.GetValues(TypeToConvert);
+ string[] names = Enum.GetNames(Type);
+ Array values = Enum.GetValues(Type);
#endif
Debug.Assert(names.Length == values.Length);
JsonConverter valueConverter = options.GetConverterInternal(valueTypeToConvert);
// If the value type has an interface or object converter, just return that converter directly.
- if (!valueConverter.TypeToConvert.IsValueType && valueTypeToConvert.IsValueType)
+ if (!valueConverter.Type!.IsValueType && valueTypeToConvert.IsValueType)
{
return valueConverter;
}
PolymorphicTypeResolver resolver = jsonTypeInfo.PolymorphicTypeResolver;
if (resolver.TryGetDerivedJsonTypeInfo(state.PolymorphicTypeDiscriminator, out JsonTypeInfo? resolvedType))
{
- Debug.Assert(TypeToConvert.IsAssignableFrom(resolvedType.Type));
+ Debug.Assert(Type!.IsAssignableFrom(resolvedType.Type));
polymorphicConverter = state.InitializePolymorphicReEntry(resolvedType);
if (!polymorphicConverter.CanHaveMetadata)
case PolymorphicSerializationState.PolymorphicReEntrySuspended:
polymorphicConverter = state.ResumePolymorphicReEntry();
- Debug.Assert(TypeToConvert.IsAssignableFrom(polymorphicConverter.TypeToConvert));
+ Debug.Assert(Type!.IsAssignableFrom(polymorphicConverter.Type));
break;
case PolymorphicSerializationState.PolymorphicReEntryNotFound:
internal JsonConverter? ResolvePolymorphicConverter(object value, JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options, ref WriteStack state)
{
Debug.Assert(!IsValueType);
- Debug.Assert(value != null && TypeToConvert.IsAssignableFrom(value.GetType()));
+ Debug.Assert(value != null && Type!.IsAssignableFrom(value.GetType()));
Debug.Assert(CanBePolymorphic || jsonTypeInfo.PolymorphicTypeResolver != null);
Debug.Assert(state.PolymorphicTypeDiscriminator is null);
Type runtimeType = value.GetType();
- if (CanBePolymorphic && runtimeType != TypeToConvert)
+ if (CanBePolymorphic && runtimeType != Type)
{
- Debug.Assert(TypeToConvert == typeof(object));
+ Debug.Assert(Type == typeof(object));
jsonTypeInfo = state.Current.InitializePolymorphicReEntry(runtimeType, options);
polymorphicConverter = jsonTypeInfo.Converter;
}
case PolymorphicSerializationState.PolymorphicReEntrySuspended:
Debug.Assert(state.IsContinuation);
polymorphicConverter = state.Current.ResumePolymorphicReEntry();
- Debug.Assert(TypeToConvert.IsAssignableFrom(polymorphicConverter.TypeToConvert));
+ Debug.Assert(Type.IsAssignableFrom(polymorphicConverter.Type));
break;
case PolymorphicSerializationState.PolymorphicReEntryNotFound:
}
/// <summary>
+ /// Gets the type being converted by the current converter instance.
+ /// </summary>
+ /// <remarks>
+ /// For instances of type <see cref="JsonConverter{T}"/> returns typeof(T),
+ /// and for instances of type <see cref="JsonConverterFactory"/> returns <see langword="null" />.
+ /// </remarks>
+ public abstract Type? Type { get; }
+
+ /// <summary>
/// Determines whether the type can be converted.
/// </summary>
/// <param name="typeToConvert">The type is checked as to whether it can be converted.</param>
return (state.FlushThreshold > 0 && writer.BytesPending > state.FlushThreshold);
}
- // This is used internally to quickly determine the type being converted for JsonConverter<T>.
- internal abstract Type TypeToConvert { get; }
-
internal abstract object? ReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options);
internal abstract bool OnTryReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out object? value);
internal abstract bool TryReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out object? value);
private protected override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.None;
/// <summary>
- /// Create a converter for the provided <see cref="Type"/>.
+ /// Create a converter for the provided <see cref="System.Type"/>.
/// </summary>
- /// <param name="typeToConvert">The <see cref="Type"/> being converted.</param>
+ /// <param name="typeToConvert">The <see cref="System.Type"/> being converted.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
/// <returns>
/// An instance of a <see cref="JsonConverter{T}"/> where T is compatible with <paramref name="typeToConvert"/>.
throw new InvalidOperationException();
}
- internal sealed override Type TypeToConvert => null!;
+ public sealed override Type? Type => null;
internal sealed override void WriteAsPropertyNameCoreAsObject(
Utf8JsonWriter writer,
/// <summary>
/// Converts an object or value to or from JSON.
/// </summary>
- /// <typeparam name="T">The <see cref="Type"/> to convert.</typeparam>
+ /// <typeparam name="T">The <see cref="System.Type"/> to convert.</typeparam>
public abstract partial class JsonConverter<T> : JsonConverter
{
/// <summary>
/// A converter may throw any Exception, but should throw <cref>JsonException</cref> when the JSON is invalid.
/// </remarks>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
- /// <param name="typeToConvert">The <see cref="Type"/> being converted.</param>
+ /// <param name="typeToConvert">The <see cref="System.Type"/> being converted.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
/// <returns>The value that was converted.</returns>
/// <remarks>Note that the value of <seealso cref="HandleNull"/> determines if the converter handles null JSON tokens.</remarks>
{
if (default(T) is not null)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type);
}
value = default;
JsonTypeInfo originalJsonTypeInfo = state.Current.JsonTypeInfo;
#endif
state.Push();
- Debug.Assert(TypeToConvert == state.Current.JsonTypeInfo.Type);
+ Debug.Assert(Type == state.Current.JsonTypeInfo.Type);
#if DEBUG
// For performance, only perform validation on internal converters on debug builds.
state.Current.PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStarted)
{
JsonTypeInfo jsonTypeInfo = state.PeekNestedJsonTypeInfo();
- Debug.Assert(jsonTypeInfo.Converter.TypeToConvert == TypeToConvert);
+ Debug.Assert(jsonTypeInfo.Converter.Type == Type);
bool canBePolymorphic = CanBePolymorphic || jsonTypeInfo.PolymorphicTypeResolver is not null;
JsonConverter? polymorphicConverter = canBePolymorphic ?
JsonTypeInfo originalJsonTypeInfo = state.Current.JsonTypeInfo;
#endif
state.Push();
- Debug.Assert(TypeToConvert == state.Current.JsonTypeInfo.Type);
+ Debug.Assert(Type == state.Current.JsonTypeInfo.Type);
#if DEBUG
// For performance, only perform validation on internal converters on debug builds.
{
// If not JsonDictionaryConverter<T> then we are JsonObject.
// Avoid a type reference to JsonObject and its converter to support trimming.
- Debug.Assert(TypeToConvert == typeof(Nodes.JsonObject));
+ Debug.Assert(Type == typeof(Nodes.JsonObject));
return TryWrite(writer, value, options, ref state);
}
return success;
}
- internal sealed override Type TypeToConvert { get; } = typeof(T);
+ public sealed override Type Type { get; } = typeof(T);
internal void VerifyRead(JsonTokenType tokenType, int depth, long bytesConsumed, bool isValueConverter, ref Utf8JsonReader reader)
{
/// Reads a dictionary key from a JSON property name.
/// </summary>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
- /// <param name="typeToConvert">The <see cref="Type"/> being converted.</param>
+ /// <param name="typeToConvert">The <see cref="System.Type"/> being converted.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
/// <returns>The value that was converted.</returns>
/// <remarks>Method should be overridden in custom converters of types used in deserialized dictionary keys.</remarks>
JsonConverter<T>? fallbackConverter = GetFallbackConverterForPropertyNameSerialization(options);
if (fallbackConverter is null)
{
- ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
+ ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type, this);
}
return fallbackConverter.ReadAsPropertyNameCore(ref reader, typeToConvert, options);
JsonConverter<T>? fallbackConverter = GetFallbackConverterForPropertyNameSerialization(options);
if (fallbackConverter is null)
{
- ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
+ ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type, this);
}
fallbackConverter.WriteAsPropertyNameCore(writer, value, options, isWritingExtensionDataProperty: false);
{
// Extension data is meant as mechanism to gather unused JSON properties;
// do not apply any custom key conversions and hardcode the default behavior.
- Debug.Assert(!IsInternalConverter && TypeToConvert == typeof(string));
+ Debug.Assert(!IsInternalConverter && Type == typeof(string));
writer.WritePropertyName((string)(object)value!);
return;
}
{
result = _fallbackConverterForPropertyNameSerialization;
- if (result is null && DefaultJsonTypeInfoResolver.TryGetDefaultSimpleConverter(TypeToConvert, out JsonConverter? defaultConverter))
+ if (result is null && DefaultJsonTypeInfoResolver.TryGetDefaultSimpleConverter(Type, out JsonConverter? defaultConverter))
{
Debug.Assert(defaultConverter != this);
_fallbackConverterForPropertyNameSerialization = result = (JsonConverter<T>)defaultConverter;
if (!converter.CanHaveMetadata)
{
// Should not be permitted unless the converter is capable of handling metadata.
- ThrowHelper.ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(converter.TypeToConvert);
+ ThrowHelper.ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(converter.Type!);
}
break;
if (converter.IsValueType)
{
// Should not be permitted if the converter is a struct.
- ThrowHelper.ThrowJsonException_MetadataInvalidReferenceToValueType(converter.TypeToConvert);
+ ThrowHelper.ThrowJsonException_MetadataInvalidReferenceToValueType(converter.Type!);
}
if (state.Current.MetadataPropertyNames != 0)
{
if (state.Current.MetadataPropertyNames != MetadataPropertyName.Ref)
{
// Read the entire JSON object while parsing for metadata: for collection converters this is only legal for $ref nodes.
- ThrowHelper.ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref state, converter.TypeToConvert);
+ ThrowHelper.ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref state, converter.Type!);
}
break;
default:
Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
// Do not tolerate non-metadata properties in collection converters.
- ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, converter.TypeToConvert, reader);
+ ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, converter.Type!, reader);
break;
}
}
// We also throw to avoid passing an invalid argument to setters for nullable struct properties,
// which would cause an InvalidProgramException when the generated IL is invoked.
if (propertyType.IsValueType && converter.IsValueType &&
- (propertyType.IsNullableOfT() ^ converter.TypeToConvert.IsNullableOfT()))
+ (propertyType.IsNullableOfT() ^ converter.Type!.IsNullableOfT()))
{
ThrowHelper.ThrowInvalidOperationException_ConverterCanConvertMultipleTypes(propertyType, converter);
}
}
/// <inheritdoc />
- public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum);
+ public sealed override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum);
/// <inheritdoc />
- public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
+ public sealed override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
if (typeToConvert != typeof(TEnum))
{
return converters;
void Add(JsonConverter converter) =>
- converters.Add(converter.TypeToConvert, converter);
+ converters.Add(converter.Type!, converter);
}
private static JsonConverter GetBuiltInConverter(Type typeToConvert)
// Expand if factory converter & validate.
converter = options.ExpandConverterFactory(converter, typeToConvert);
- if (!converter.TypeToConvert.IsInSubtypeRelationshipWith(typeToConvert))
+ if (!converter.Type!.IsInSubtypeRelationshipWith(typeToConvert))
{
ThrowHelper.ThrowInvalidOperationException_SerializationConverterNotCompatible(converter.GetType(), typeToConvert);
}
if (default(T) is null)
{
Debug.Assert(CanDeserialize || EffectiveObjectCreationHandling == JsonObjectCreationHandling.Populate);
- ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(EffectiveConverter.TypeToConvert);
+ ThrowHelper.ThrowInvalidOperationException_DeserializeUnableToAssignNull(EffectiveConverter.Type);
}
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.Type);
}
if (!IgnoreNullTokensOnRead)
{
if (default(T) is not null)
{
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.TypeToConvert);
+ ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(EffectiveConverter.Type);
}
value = default(T);
{
JsonTypeInfo jsonTypeInfo;
- if (converter.TypeToConvert == type)
+ if (converter.Type == type)
{
// For performance, avoid doing a reflection-based instantiation
// if the converter type matches that of the declared type.
[DoesNotReturn]
public static void ThrowInvalidOperationException_ConverterCanConvertMultipleTypes(Type runtimePropertyType, JsonConverter jsonConverter)
{
- throw new InvalidOperationException(SR.Format(SR.ConverterCanConvertMultipleTypes, jsonConverter.GetType(), jsonConverter.TypeToConvert, runtimePropertyType));
+ throw new InvalidOperationException(SR.Format(SR.ConverterCanConvertMultipleTypes, jsonConverter.GetType(), jsonConverter.Type, runtimePropertyType));
}
[DoesNotReturn]
{
JsonConverter obj = JsonMetadataServices.BooleanConverter;
- string json = JsonSerializer.Serialize(obj, PublicContext.Default.Options);
- Assert.Equal("{}", json);
-
- Assert.Throws<NotSupportedException>(() => JsonSerializer.Deserialize<JsonConverter>(json, PublicContext.Default.Options));
+ Assert.Throws<NotSupportedException>(() => JsonSerializer.Serialize(obj, PublicContext.Default.Options));
+ Assert.Throws<NotSupportedException>(() => JsonSerializer.Deserialize<JsonConverter>("{}", PublicContext.Default.Options));
}
[Fact]
[InlineData(InvalidCustomKeyConverter.InvalidOperationType.HandleEntireProperty)]
public static void InvalidCustomKeyConverter_Serialization(InvalidCustomKeyConverter.InvalidOperationType invalidOperationType)
{
- var options = new JsonSerializerOptions { Converters = { new InvalidCustomKeyConverter { Type = invalidOperationType } } };
+ var options = new JsonSerializerOptions { Converters = { new InvalidCustomKeyConverter { OperationType = invalidOperationType } } };
var value = new Dictionary<string, int> { ["key"] = 42 };
Assert.Throws<JsonException>(() => JsonSerializer.Serialize(value, options));
[InlineData(InvalidCustomKeyConverter.InvalidOperationType.ReturnNull, typeof(ArgumentNullException))]
public static void InvalidCustomKeyConverter_Deserialization(InvalidCustomKeyConverter.InvalidOperationType invalidOperationType, Type exceptionType)
{
- var options = new JsonSerializerOptions { Converters = { new InvalidCustomKeyConverter { Type = invalidOperationType } } };
+ var options = new JsonSerializerOptions { Converters = { new InvalidCustomKeyConverter { OperationType = invalidOperationType } } };
string json = @"{""key1"" : 1, ""key2"" : 2 }";
Assert.Throws(exceptionType, () => JsonSerializer.Deserialize<Dictionary<string, int>>(json, options));
DoNothing, HandleEntireProperty, HandleEntireParentObject, ReturnNull
}
- public InvalidOperationType Type { get; init; }
+ public InvalidOperationType OperationType { get; init; }
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> throw new NotSupportedException();
public override void WriteAsPropertyName(Utf8JsonWriter writer, string value, JsonSerializerOptions _)
{
- switch (Type)
+ switch (OperationType)
{
case InvalidOperationType.HandleEntireProperty:
writer.WriteString("key", value);
public override string ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions _)
{
- switch (Type)
+ switch (OperationType)
{
case InvalidOperationType.HandleEntireProperty:
reader.Read();
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Diagnostics;
+using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
=> new MyBoolEnumConverter();
}
+
+ [Theory]
+ [InlineData(typeof(ConverterReturningNull), typeof(Customer))]
+ [InlineData(typeof(TestFactory.SimpleConverter), typeof(string))]
+ [InlineData(typeof(ObjectBoolConverter), typeof(object))]
+ [InlineData(typeof(Int32NullConverter), typeof(int))]
+ [InlineData(typeof(JsonStringEnumConverter), null)]
+ [InlineData(typeof(JsonStringEnumConverter<MyBoolEnum>), null)]
+ [InlineData(typeof(InvalidJsonConverterFactory), null)]
+ [InlineData(typeof(TestFactory), null)]
+ public static void JsonConverter_TypeProperty_ReturnsExpectedResult(Type converterType, Type expectedType)
+ {
+ var converter = (JsonConverter)Activator.CreateInstance(converterType)!;
+ Assert.Equal(expectedType, converter.Type);
+ }
+
+ [Theory]
+ [InlineData(typeof(bool))]
+ [InlineData(typeof(int))]
+ [InlineData(typeof(string))]
+ [InlineData(typeof(object))]
+ [InlineData(typeof(MyBoolEnum))]
+ [InlineData(typeof(Customer))]
+ [InlineData(typeof(int[]))]
+ [InlineData(typeof(Dictionary<string, int>))]
+ public static void JsonSerializerOptions_GetConverter_TypeProperty_ReturnsExpectedResult(Type type)
+ {
+ JsonConverter converter = JsonSerializerOptions.Default.GetConverter(type);
+ Assert.Equal(type, converter.Type);
+ }
}
}