-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
ToString method with Formatting.None returns "null" instead of empty string for null values #2944
Comments
No, none of the two I am not saying that the current Note that Side note: If your intention is to work with the actual values represented by a JValue instance, don't use the ToString() methods. Instead, use the var value = jsonObject["test"].Value<string?>();
Console.WriteLine(value ?? "<JSON_NULL_LITERAL>"); I am not associated with the Newtonsoft.Json project or its author/maintainer. I am just a user of the library like you (although these days i prefer STJ over Newtonsoft.Json). |
Thank you for your detailed explanation and insights into JValue.ToString() behavior. I completely agree with your point about the need for consistency in the ToString method. The inconsistency between ToString(Formatting.None) and ToString(Formatting.Indented) has indeed caused confusion and unexpected behavior in my subsequent logic, particularly in the empty value check. In my view, when using ToString(), the current behavior of returning an empty string whether the value is present or null is exactly what I expect and want. My intention in using Formatting.None was simply to compress the JSON output for readability, not to change the value of the JSON representation. I believe that the ToString method should consistently return the same result irrespective of the Formatting parameter, with Formatting only affecting the format of the output, not the value itself. I would also be okay with ToString() returning "null" to maintain consistency. Thank you for suggesting the Value extension method as an alternative approach. I will consider using it for working directly with the underlying values. |
Source/destination types
Source/destination JSON
Expected behavior
value2 should both return an empty string because a null value should be converted to an empty string in both ToString(Formatting.None) and ToString(Formatting.Indented).
Actual behavior
value2 returns the string "null"
Steps to reproduce
The text was updated successfully, but these errors were encountered: