JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.74k stars 3.25k forks source link

Inconsistent Behavior from JToken.ToString() #2743

Open everix1992 opened 1 year ago

everix1992 commented 1 year ago

Source/destination JSON

{"BooleanFlag": true}

Expected behavior

JToken.ToString() returns the string "true"

Actual behavior

The string "True" is returned (note the capital T)

Steps to reproduce

var testObject = JObject.Parse("{\"BooleanFlag\": true}");
var booleanFlagToken = testObject.SelectToken("BooleanFlag");
var capitalizedString = booleanFlagToken.ToString(); // Returns "True"
var formattingNoneString = booleanFlagToken.ToString(Newtonsoft.Json.Formatting.None); // Returns "true"
var formattingIndentedString = booleanFlagToken.ToString(Newtonsoft.Json.Formatting.Indented); // Returns "true"

Other Notes

Tested this against the latest published version of the library (13.0.1) on a .NET 6 Console App. It seems to me like we'd expected the basic ToString() method to return exactly what is in the JSON, which is "true". Not sure why it's getting capitalized or why it doesn't match up with the other overloads.

everix1992 commented 1 year ago

After some further testing, I assume this is happening because the overloaded ToString() returns the JSON string (either indented or not), while the base ToString() with no arguments actually converts to a string with Microsoft's type converters (which outputs "True" for a value of true). I assume this can be closed out as a non-issue

wallymathieu commented 1 year ago

Then the documentation should be updated since it states that tostring returns JSON.

wallymathieu commented 1 year ago

There is a remark bout this violation of Liskov substitution principle is documented as a remark but the online docs don't reflect that.