-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
.NET 8.0 JsonArray.Add throws System.InvalidOperationException: "The element cannot be an object or array." #94842
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionAdding a It is interesting that Related: #70427 Reproduction Stepsusing System.Text.Json;
using System.Text.Json.Nodes;
var element = JsonDocument.Parse("""{"test":"hello world"}""");
var array = new JsonArray();
array.Add(element.RootElement); // .NET 8.0 throws here
Console.WriteLine(array.ToJsonString()); Expected behaviorWorks without exception. Actual behaviorThrows a System.InvalidOperationException: "The element cannot be an object or array."
Regression?Yes, it works with .NET 7.0 Known WorkaroundsFor this particular case the Configuration.NET 8 Other informationNo response
|
It seems that the particular example was working until .NET 7 because the method was creating a
Bypassing the usual validation in place when creating runtime/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.CreateOverloads.cs Line 319 in 90f1525
The validation is in place because Nevertheless, we should try to fix this because it's a regression. I'll follow up with a PR that both fixes the problem and ensures |
Description
Adding a
JsonElement
viaJsonArray.Add
throws an exception with .NET 8. In .NET 7 it worked without throwing.It is interesting that
JsonArray.Add
worked in .NET 7 butJsonValue.Create
fails in both .NET 7/8 when passing in aJsonElement
withValueKind.Object/Array
.Related: #70427
Regression in 1db4357 ?
Reproduction Steps
Expected behavior
Works without exception.
Actual behavior
Throws a System.InvalidOperationException: "The element cannot be an object or array."
Regression?
Yes, it works with .NET 7.0
Known Workarounds
For this particular case the
JsonDocument
could be added instead of theRootElement
.A general workaround would be #70427 (comment)
Configuration
.NET 8
Windows
x64
Other information
No response
The text was updated successfully, but these errors were encountered: