Skip to content

Commit

Permalink
Fix: Null String being reported as String rather than JTokenType.Null (
Browse files Browse the repository at this point in the history
  • Loading branch information
dmmusil committed Jan 16, 2023
1 parent c908de3 commit 5702581
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2775.cs
@@ -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 changes: 6 additions & 0 deletions Src/Newtonsoft.Json/Linq/JTokenWriter.cs
Expand Up @@ -281,6 +281,12 @@ public override void WriteComment(string? text)
/// <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);
}
Expand Down

0 comments on commit 5702581

Please sign in to comment.