Breaking change in JsonNodeConverter
Description
Using a JsonConverter
The new System.Text.Json.Nodes.JsonValueOfJsonString introduced in .NET 10 cannot be converted to an object using GetValue<object>(), resulting in an exception.
Reproduction Steps
See Repo at https://github.com/NiceWaffel/jsonnodeconverter-repro
var reader = new Utf8JsonReader("\"Hello World!\""u8);
reader.Read();
var node = (SerializationContext.Default.JsonNode.Converter as JsonConverter<JsonNode>)
?.Read(ref reader, typeof(JsonNode), SerializationContext.Default.Options);
Debug.Assert(node != null, "We should get a valid JsonNode.");
// Different type in .NET 8, .NET 9 and .NET 10
// .NET 8: System.Text.Json.Nodes.JsonValuePrimitive`1[System.Text.Json.JsonElement]
// .NET 9: System.Text.Json.Nodes.JsonValueOfElement
// .NET 10: System.Text.Json.Nodes.JsonValueOfJsonString
Console.WriteLine(node.GetType());
// Exception in .NET 10 (no exception in .NET 8)
// System.InvalidOperationException: An element of type 'String' cannot be converted to a 'System.Object'.
var o = node.GetValue<object>();
Console.WriteLine(o);
Expected behavior
The behavior in .NET 8, .NET 9 and .Net 10 should be the same. No exception should be thrown.
Actual behavior
In .NET 10, a string JsonNode (read as JsonValueOfJsonString) cannot be converted to an object, resulting in an exception.
Unhandled exception. System.InvalidOperationException: An element of type 'String' cannot be converted to a 'System.Object'.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NodeUnableToConvertElement(JsonValueKind valueKind, Type destinationType)
at System.Text.Json.Nodes.JsonValueOfJsonString.GetValue[T]()
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
JsonValueOfJsonString was introduced with https://github.com/dotnet/runtime/pull/116798
Tagging subscribers to this area: @dotnet/area-system-text-json See info in area-owners.md if you want to be subscribed.
@gregsdennis Just FYI, this issue surfaced for me when using version 7.4.0 of the JsonSchema.Net library. When evaluating a TypeKeyword, it tries determining the schema value type, which calls jsonValue.GetValue<object>() in Json.Schema.JsonNodeExtensions.GetSchemaValueType(JsonNode node)
FYI @PranavSenthilnathan @jeffhandley