From 427c7bd763aeea6c2391eec6fa7ae9ea1a2847c0 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Wed, 23 Oct 2019 16:23:20 -0700 Subject: [PATCH] JsonSerializer.Serialize should check whether the passed-in (#42026) (#42027) Utf8JsonWriter is null. --- .../Json/Serialization/JsonSerializer.Write.Helpers.cs | 6 ++++++ .../Serialization/JsonSerializer.Write.Utf8JsonWriter.cs | 6 ++++++ .../tests/Serialization/WriteValueTests.cs | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs index b990c376a867..0413a3f206f6 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs +++ b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs @@ -97,6 +97,11 @@ private static void WriteValueCore(Utf8JsonWriter writer, object value, Type typ options = JsonSerializerOptions.s_defaultOptions; } + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + WriteCore(writer, value, type, options); } @@ -111,6 +116,7 @@ private static void WriteCore(PooledByteBufferWriter output, object value, Type private static void WriteCore(Utf8JsonWriter writer, object value, Type type, JsonSerializerOptions options) { Debug.Assert(type != null || value == null); + Debug.Assert(writer != null); if (value == null) { diff --git a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Utf8JsonWriter.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Utf8JsonWriter.cs index 3b304cf1cd62..8e285557f37e 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Utf8JsonWriter.cs +++ b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Utf8JsonWriter.cs @@ -12,6 +12,9 @@ public static partial class JsonSerializer /// The writer to write. /// The value to convert and write. /// Options to control the behavior. + /// + /// is null. + /// public static void Serialize(Utf8JsonWriter writer, TValue value, JsonSerializerOptions options = null) { WriteValueCore(writer, value, typeof(TValue), options); @@ -24,6 +27,9 @@ public static void Serialize(Utf8JsonWriter writer, TValue value, JsonSe /// The value to convert and write. /// The type of the to convert. /// Options to control the behavior. + /// + /// is null. + /// public static void Serialize(Utf8JsonWriter writer, object value, Type inputType, JsonSerializerOptions options = null) { VerifyValueAndType(value, inputType); diff --git a/src/System.Text.Json/tests/Serialization/WriteValueTests.cs b/src/System.Text.Json/tests/Serialization/WriteValueTests.cs index d52d673f5475..299f758b769c 100644 --- a/src/System.Text.Json/tests/Serialization/WriteValueTests.cs +++ b/src/System.Text.Json/tests/Serialization/WriteValueTests.cs @@ -9,6 +9,13 @@ namespace System.Text.Json.Serialization.Tests { public static partial class WriteValueTests { + [Fact] + public static void NullWriterThrows() + { + Assert.Throws(() => JsonSerializer.Serialize(null, 1)); + Assert.Throws(() => JsonSerializer.Serialize(null, 1, typeof(int))); + } + [Fact] public static void CanWriteValueToJsonArray() {