Fix: Null String being reported as String rather than JTokenType.Null (#2796)

Fixes https://github.com/JamesNK/Newtonsoft.Json/issues/2775
This commit is contained in:
Dylan Musil
2023-01-16 17:08:14 +08:00
committed by GitHub
parent c908de3017
commit 57025815e5
2 changed files with 38 additions and 0 deletions
@@ -0,0 +1,32 @@
using System.Linq;
using Newtonsoft.Json.Linq;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
using TestCase = Xunit.InlineDataAttribute;
#else
using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Issues
{
public class Issue2775
{
[Test]
//https://github.com/JamesNK/Newtonsoft.Json/issues/2775
public void TokenType()
{
var jObject = new JObject { { "NullProperty", false ? "0" : null } };
var jToken = JToken.FromObject(jObject);
Assert.AreEqual(JTokenType.Null, jToken.Children().Children().Single().Type);
jObject = new JObject { { "NullProperty", (string)null } };
jToken = JToken.FromObject(jObject);
Assert.AreEqual(JTokenType.Null, jToken.Children().Children().Single().Type);
}
}
}
+6
View File
@@ -281,6 +281,12 @@ namespace Newtonsoft.Json.Linq
/// <param name="value">The <see cref="String"/> value to write.</param>
public override void WriteValue(string? value)
{
if (value == null)
{
WriteNull();
return;
}
base.WriteValue(value);
AddJValue(new JValue(value), JsonToken.String);
}