diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj
index 00d8d24d2e3e8..13ef04061cdf2 100644
--- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj
+++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj
@@ -130,6 +130,8 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
+
+
@@ -226,14 +228,12 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
-
-
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
index dfd247a58f180..0fc70fd5b9818 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
@@ -129,12 +129,6 @@ internal virtual JsonTypeInfo CreateCustomJsonTypeInfo(JsonSerializerOptions opt
///
internal bool IsInternalConverterForNumberType { get; init; }
- ///
- /// Loosely-typed ReadCore() that forwards to strongly-typed ReadCore().
- ///
- internal abstract object? ReadCoreAsObject(ref Utf8JsonReader reader, JsonSerializerOptions options, scoped ref ReadStack state);
-
-
internal static bool ShouldFlush(Utf8JsonWriter writer, ref WriteStack state)
{
// If surpassed flush threshold then return false which will flush stream.
@@ -149,11 +143,6 @@ internal static bool ShouldFlush(Utf8JsonWriter writer, ref WriteStack state)
internal abstract bool TryWriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options, ref WriteStack state);
- ///
- /// Loosely-typed WriteCore() that forwards to strongly-typed WriteCore().
- ///
- internal abstract bool WriteCoreAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options, ref WriteStack state);
-
///
/// Loosely-typed WriteToPropertyName() that forwards to strongly-typed WriteToPropertyName().
///
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs
index fc008aed9bfb3..b5c70e64bc243 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs
@@ -61,16 +61,6 @@ internal JsonConverter GetConverterInternal(Type typeToConvert, JsonSerializerOp
return converter;
}
- internal sealed override object ReadCoreAsObject(
- ref Utf8JsonReader reader,
- JsonSerializerOptions options,
- scoped ref ReadStack state)
- {
- Debug.Fail("We should never get here.");
-
- throw new InvalidOperationException();
- }
-
internal sealed override bool OnTryReadAsObject(
ref Utf8JsonReader reader,
JsonSerializerOptions options,
@@ -106,17 +96,6 @@ internal sealed override bool TryWriteAsObject(
internal sealed override Type TypeToConvert => null!;
- internal sealed override bool WriteCoreAsObject(
- Utf8JsonWriter writer,
- object? value,
- JsonSerializerOptions options,
- ref WriteStack state)
- {
- Debug.Fail("We should never get here.");
-
- throw new InvalidOperationException();
- }
-
internal sealed override void WriteAsPropertyNameCoreAsObject(
Utf8JsonWriter writer, object value,
JsonSerializerOptions options,
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs
index 975df8ae20227..40b6bd169fe67 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs
@@ -5,18 +5,10 @@ namespace System.Text.Json.Serialization
{
public partial class JsonConverter
{
- internal sealed override object? ReadCoreAsObject(
- ref Utf8JsonReader reader,
- JsonSerializerOptions options,
- scoped ref ReadStack state)
- {
- return ReadCore(ref reader, options, ref state);
- }
-
internal T? ReadCore(
ref Utf8JsonReader reader,
JsonSerializerOptions options,
- scoped ref ReadStack state)
+ ref ReadStack state)
{
try
{
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs
index 7b003325d4dae..3abbd6564067c 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.WriteCore.cs
@@ -5,37 +5,6 @@ namespace System.Text.Json.Serialization
{
public partial class JsonConverter
{
- internal sealed override bool WriteCoreAsObject(
- Utf8JsonWriter writer,
- object? value,
- JsonSerializerOptions options,
- ref WriteStack state)
- {
- if (
-#if NETCOREAPP
- // Treated as a constant by recent versions of the JIT.
- typeof(T).IsValueType)
-#else
- IsValueType)
-#endif
- {
- // Value types can never have a null except for Nullable.
- if (default(T) is not null && value is null)
- {
- ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
- }
-
- // Root object is a boxed value type, we need to push it to the reference stack before it gets unboxed here.
- if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles && value != null)
- {
- state.ReferenceResolver.PushReferenceForCycleDetection(value);
- }
- }
-
- T actualValue = (T)value!;
- return WriteCore(writer, actualValue, options, ref state);
- }
-
internal bool WriteCore(
Utf8JsonWriter writer,
in T value,
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs
index 0e5c16b05bb65..5713cf079581d 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs
@@ -54,6 +54,23 @@ private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type inpu
return info;
}
+ private static void ValidateInputType(object? value, Type inputType)
+ {
+ if (inputType is null)
+ {
+ ThrowHelper.ThrowArgumentNullException(nameof(inputType));
+ }
+
+ if (value is not null)
+ {
+ Type runtimeType = value.GetType();
+ if (!inputType.IsAssignableFrom(runtimeType))
+ {
+ ThrowHelper.ThrowArgumentException_DeserializeWrongType(inputType, value);
+ }
+ }
+ }
+
internal static bool IsValidNumberHandlingValue(JsonNumberHandling handling) =>
JsonHelpers.IsInRangeInclusive((int)handling, 0,
(int)(
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Document.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Document.cs
index 58dd06cf6b0de..74361b7a17294 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Document.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Document.cs
@@ -35,8 +35,9 @@ public static partial class JsonSerializer
ThrowHelper.ThrowArgumentNullException(nameof(document));
}
- JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, typeof(TValue));
- return ReadDocument(document, jsonTypeInfo);
+ JsonTypeInfo jsonTypeInfo = GetTypeInfo(options);
+ ReadOnlySpan utf8Json = document.GetRootRawValue().Span;
+ return ReadFromSpan(utf8Json, jsonTypeInfo);
}
///
@@ -70,7 +71,8 @@ public static partial class JsonSerializer
}
JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
- return ReadDocument