Skip to content

Remove JsonConverter.RuntimeType #65224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Collections.Generic;

namespace System.Text.Json.Serialization.Converters
{
Expand All @@ -14,15 +13,5 @@ protected override void Add(in TElement value, ref ReadStack state)
{
((TCollection)state.Current.ReturnValue!).Enqueue(value);
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
if (state.Current.JsonTypeInfo.CreateObject is null)
{
ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type);
}

state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Collections.Generic;

namespace System.Text.Json.Serialization.Converters
{
Expand All @@ -14,15 +13,5 @@ protected override void Add(in TElement value, ref ReadStack state)
{
((TCollection)state.Current.ReturnValue!).Push(value);
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
if (state.Current.JsonTypeInfo.CreateObject is null)
{
ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type);
}

state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ protected override void Add(TKey key, in TValue value, JsonSerializerOptions opt
((TCollection)state.Current.ReturnValue!)[key] = value;
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state)
{
if (state.Current.JsonTypeInfo.CreateObject == null)
{
ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type);
}

state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject();
}

protected internal override bool OnWriteResume(
Utf8JsonWriter writer,
TCollection value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -25,45 +26,22 @@ protected override void Add(in TElement value, ref ReadStack state)

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;

if (TypeToConvert.IsInterface || TypeToConvert.IsAbstract)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = new List<TElement>();
}
else
base.CreateCollection(ref reader, ref state, options);
TCollection returnValue = (TCollection)state.Current.ReturnValue!;
if (returnValue.IsReadOnly)
{
if (typeInfo.CreateObject == null)
{
ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
}

TCollection returnValue = (TCollection)typeInfo.CreateObject()!;

if (returnValue.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = returnValue;
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}
}

internal override Type RuntimeType
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
get
// Deserialize as List<T> for interface types that support it.
if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(List<TElement>)))
{
if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
{
return typeof(List<TElement>);
}

return TypeToConvert;
Debug.Assert(TypeToConvert.IsInterface);
jsonTypeInfo.CreateObject = () => new List<TElement>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -27,33 +28,12 @@ protected override void Add(string key, in object? value, JsonSerializerOptions

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;

if (TypeToConvert.IsInterface || TypeToConvert.IsAbstract)
base.CreateCollection(ref reader, ref state);
TDictionary returnValue = (TDictionary)state.Current.ReturnValue!;
if (returnValue.IsReadOnly)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

// Strings are intentionally used as keys when deserializing non-generic dictionaries.
state.Current.ReturnValue = new Dictionary<string, object>();
}
else
{
if (typeInfo.CreateObject is null)
{
ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
}

TDictionary returnValue = (TDictionary)typeInfo.CreateObject()!;

if (returnValue.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = returnValue;
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}
}

Expand Down Expand Up @@ -115,6 +95,14 @@ protected internal override bool OnWriteResume(Utf8JsonWriter writer, TDictionar
return true;
}

internal override Type RuntimeType => TypeToConvert.IsAbstract || TypeToConvert.IsInterface ? typeof(Dictionary<string, object>) : TypeToConvert;
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?>)))
{
Debug.Assert(TypeToConvert.IsInterface);
jsonTypeInfo.CreateObject = () => new Dictionary<string, object?>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -27,45 +28,22 @@ protected override void Add(TKey key, in TValue value, JsonSerializerOptions opt

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;

if (TypeToConvert.IsInterface || TypeToConvert.IsAbstract)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = new Dictionary<TKey, TValue>();
}
else
base.CreateCollection(ref reader, ref state);
TDictionary returnValue = (TDictionary)state.Current.ReturnValue!;
if (returnValue.IsReadOnly)
{
if (typeInfo.CreateObject == null)
{
ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
}

TDictionary returnValue = (TDictionary)typeInfo.CreateObject()!;

if (returnValue.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = returnValue;
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}
}

internal override Type RuntimeType
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
get
// Deserialize as Dictionary<TKey,TValue> for interface types that support it.
if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(Dictionary<TKey, TValue>)))
{
if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
{
return typeof(Dictionary<TKey, TValue>);
}

return TypeToConvert;
Debug.Assert(TypeToConvert.IsInterface);
jsonTypeInfo.CreateObject = () => new Dictionary<TKey, TValue>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
{
Expand All @@ -14,14 +15,16 @@ internal sealed class IEnumerableConverter<TCollection>
: JsonCollectionConverter<TCollection, object?>
where TCollection : IEnumerable
{
private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(List<object?>));

protected override void Add(in object? value, ref ReadStack state)
{
((List<object?>)state.Current.ReturnValue!).Add(value);
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
if (!_isDeserializable)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}
Expand Down Expand Up @@ -71,7 +74,5 @@ protected override bool OnWriteResume(

return true;
}

internal override Type RuntimeType => typeof(List<object?>);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ internal sealed class IEnumerableOfTConverter<TCollection, TElement>
: IEnumerableDefaultConverter<TCollection, TElement>
where TCollection : IEnumerable<TElement>
{
private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(List<TElement>));

protected override void Add(in TElement value, ref ReadStack state)
{
((List<TElement>)state.Current.ReturnValue!).Add(value);
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
if (!_isDeserializable)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = new List<TElement>();
}

internal override Type RuntimeType => typeof(List<TElement>);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -19,37 +20,17 @@ protected override void Add(in object? value, ref ReadStack state)
if (IsValueType)
{
state.Current.ReturnValue = collection;
};
}
}

protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;

if (TypeToConvert.IsInterface || TypeToConvert.IsAbstract)
base.CreateCollection(ref reader, ref state, options);
TCollection returnValue = (TCollection)state.Current.ReturnValue!;
if (returnValue.IsReadOnly)
{
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = new List<object?>();
}
else
{
if (typeInfo.CreateObject == null)
{
ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
}

TCollection returnValue = (TCollection)typeInfo.CreateObject()!;

if (returnValue.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}

state.Current.ReturnValue = returnValue;
state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
}
}

Expand Down Expand Up @@ -91,16 +72,13 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
return true;
}

internal override Type RuntimeType
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
get
// Deserialize as List<object?> for interface types that support it.
if (jsonTypeInfo.CreateObject is null && TypeToConvert.IsAssignableFrom(typeof(List<object?>)))
{
if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
{
return typeof(List<object?>);
}

return TypeToConvert;
Debug.Assert(TypeToConvert.IsInterface);
jsonTypeInfo.CreateObject = () => new List<object?>();
}
}
}
Expand Down
Loading