Skip to content
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

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

Merged
merged 2 commits into from Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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