-
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
Null String being reported as String rather than JTokenType.Null #2775
Comments
I had some unit tests fail with NullReferenceException during the 13.0.1 -> 13.0.2 upgrade and it ended up being this. Trying out your code I got the differences to appear clearly in Linqpad.
|
dmmusil
added a commit
to dmmusil/Newtonsoft.Json
that referenced
this issue
Jan 15, 2023
JamesNK
pushed a commit
that referenced
this issue
Jan 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source/destination JSON
{
"NullProperty": null
}
Expected behavior
In the sample code below, we are creating a JObject with a property set by creating a null string (string nullString = null) or via a ternary operator (j_Object.Add("NullProperty", false ? "0" : null);_), in the ternary operator sometimes it is set a string value and sometimes not, we then use JToken.FromObject(jObject) to get a JToken.
We were then look at the type of the NullProperty which was a JTokenType.Null in 13.0.1.
Actual behavior
With version 13.0.1, with this code we were getting back jToken.Children().Children().Single().Type as a JTokenType.Null, but in 13.0.2 this Type returns a String type.
Looking at the of the jObject property, whose values appears to be a String in both 13.0.1 and 13.0.2, it looks like this might be a bug fix.
However, it seems if we create any other objects types other than Strings in the same way, JTokenType.Null is always returned as the property type, so wondering why a string is treated differently and hence we need to add additional null checking to our JTokens.
This is a breaking change in our code base.
Steps to reproduce
Code to test different jObject configurations
var jObject = new JObject();
jObject.Add("NullProperty", false ? "0" : null);
var jToken = JToken.FromObject(jObject);
Console.WriteLine(jToken.Children().Children().Single()); // returns JTokenType.Null in 13.0.1 and String in 13.0.2
Console.WriteLine(jObject.Children().Children().Single().Type); // returns String in 13.0.1 and String in 13.0.2
jObject = new JObject();
string nullValue = null;
jObject.Add("NullProperty", nullValue);
jToken = JToken.FromObject(jObject);
Console.WriteLine(jToken.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and String in 13.0.2
Console.WriteLine(jObject.Children().Children().Single().Type); // returns String in 13.0.1 and String in 13.0.2
jObject = new JObject();
jObject.Add("NullProperty", false ? 1 : null);
jToken = JToken.FromObject(jObject);
Console.WriteLine(jToken.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
Console.WriteLine(jObject.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
jObject = new JObject();
jObject.Add("NullProperty", false ? true : null);
jToken = JToken.FromObject(jObject);
Console.WriteLine(jToken.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
Console.WriteLine(jObject.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
jObject = new JObject();
jObject.Add("NullProperty", false ? JToken.FromObject(new SomeClass()) : null);
jToken = JToken.FromObject(jObject);
Console.WriteLine(jToken.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
Console.WriteLine(jObject.Children().Children().Single().Type); // returns JTokenType.Null in 13.0.1 and JTokenType.Null in 13.0.2
The text was updated successfully, but these errors were encountered: