-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Description
Description
Using a JsonConverter to read a string node, .NET 9 and .NET 10 show different behavior.
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 #116798
Reactions are currently unavailable