From 0f3b5b30c82ea5b2ece43d7007be5c3d48fb18c9 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 11 Feb 2023 22:20:19 +1100 Subject: [PATCH] Null String being reported as String rather than JTokenType.Null https://github.com/JamesNK/Newtonsoft.Json/pull/2796 --- src/Argon/Linq/JTokenWriter.cs | 6 ++++++ src/Tests/Issues/Issue2775.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/Tests/Issues/Issue2775.cs diff --git a/src/Argon/Linq/JTokenWriter.cs b/src/Argon/Linq/JTokenWriter.cs index 9b6d1953c..a27e8c929 100644 --- a/src/Argon/Linq/JTokenWriter.cs +++ b/src/Argon/Linq/JTokenWriter.cs @@ -215,6 +215,12 @@ public override void WriteComment(string? text) /// public override void WriteValue(string? value) { + if (value == null) + { + WriteNull(); + return; + } + base.WriteValue(value); AddJValue(new(value)); } diff --git a/src/Tests/Issues/Issue2775.cs b/src/Tests/Issues/Issue2775.cs new file mode 100644 index 000000000..07f6c65d1 --- /dev/null +++ b/src/Tests/Issues/Issue2775.cs @@ -0,0 +1,28 @@ + public class Issue2775 + { + [Fact] + //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.Equal(JTokenType.Null, jToken.Children().Children().Single().Type); + + jObject = new() + { + { + "NullProperty", (string) null + } + }; + + jToken = JToken.FromObject(jObject); + Assert.Equal(JTokenType.Null, jToken.Children().Children().Single().Type); + } + } \ No newline at end of file