Skip to content

ToString for JsonValue is not special cased correctly #117992

@PranavSenthilnathan

Description

@PranavSenthilnathan

Calling ToString on a JsonValue of a string should return a value without quotes. The ToString implementation for JsonValues of other types can just JSON serialize their value, but serializing JsonValue of string will return a value with quotes around it. This needs to be special cased to not have the quotes.

Note there are many ways to create JsonValue and some will fail:

JsonNode node = JsonNode.Parse("\"Hello\"");
string json = node.ToString();
Assert.Equal("Hello", json); // Success

node = JsonValue.Create("Hello");
json = node.ToString();
Assert.Equal("Hello", json); // Success

node = JsonSerializer.Deserialize<JsonNode>("\"Hello\"");
json = node.ToString();
Assert.Equal("Hello", json); // Fail, json is "\"Hello\"", but it should just be "Hello"

This happens when the underlying type of the JsonValue is the internal JsonValueOfJsonString class. This class was added in #116798.

This is a regression from .NET 9.0.

/cc @BrennanConroy @wtgodbe who ran into this in dotnet/aspnetcore#62869
/cc @eiriktsarpalis @jeffhandley This is causing ASP.NET Core test failures when they are picking up the preview 7 System.Text.Json binaries, so we should consider backporting.

Metadata

Metadata

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions