From 15525f1c44e0d99ef8fdee73430853e22239181d Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 21 Feb 2021 13:09:54 +1300 Subject: [PATCH] Fix JsonWriter.WriteToken to allow null with string token (#2472) --- .../JsonTextWriterTest.cs | 6 +++-- .../Linq/JTokenWriterTest.cs | 23 +++++++++++++++++++ Src/Newtonsoft.Json/JsonWriter.cs | 5 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index b3bdd4d93..f0c462337 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -945,13 +945,15 @@ public void WriteTokenDirect() jsonWriter.WriteToken(JsonToken.StartArray); jsonWriter.WriteToken(JsonToken.Integer, 1); jsonWriter.WriteToken(JsonToken.StartObject); - jsonWriter.WriteToken(JsonToken.PropertyName, "string"); + jsonWriter.WriteToken(JsonToken.PropertyName, "integer"); jsonWriter.WriteToken(JsonToken.Integer, int.MaxValue); + jsonWriter.WriteToken(JsonToken.PropertyName, "null-string"); + jsonWriter.WriteToken(JsonToken.String, null); jsonWriter.WriteToken(JsonToken.EndObject); jsonWriter.WriteToken(JsonToken.EndArray); } - Assert.AreEqual(@"[1,{""string"":2147483647}]", sb.ToString()); + Assert.AreEqual(@"[1,{""integer"":2147483647,""null-string"":null}]", sb.ToString()); } [Test] diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs index d8d7dbe5f..18d195eb5 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs @@ -392,5 +392,28 @@ public void DateTimeZoneHandling() Assert.AreEqual(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc), dt); } + + [Test] + public void WriteTokenDirect() + { + JToken token; + + using (JTokenWriter jsonWriter = new JTokenWriter()) + { + jsonWriter.WriteToken(JsonToken.StartArray); + jsonWriter.WriteToken(JsonToken.Integer, 1); + jsonWriter.WriteToken(JsonToken.StartObject); + jsonWriter.WriteToken(JsonToken.PropertyName, "integer"); + jsonWriter.WriteToken(JsonToken.Integer, int.MaxValue); + jsonWriter.WriteToken(JsonToken.PropertyName, "null-string"); + jsonWriter.WriteToken(JsonToken.String, null); + jsonWriter.WriteToken(JsonToken.EndObject); + jsonWriter.WriteToken(JsonToken.EndArray); + + token = jsonWriter.Token; + } + + Assert.AreEqual(@"[1,{""integer"":2147483647,""null-string"":null}]", token.ToString(Formatting.None)); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index d9121f610..a68fc8c43 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -579,8 +579,9 @@ public void WriteToken(JsonToken token, object? value) } break; case JsonToken.String: - ValidationUtils.ArgumentNotNull(value, nameof(value)); - WriteValue(value.ToString()); + // Allow for a null string. This matches JTokenReader behavior which can read + // a JsonToken.String with a null value. + WriteValue(value?.ToString()); break; case JsonToken.Boolean: ValidationUtils.ArgumentNotNull(value, nameof(value));