Skip to content

Commit 7224366

Browse files
authored
JsonSerializer.Serialize should check whether the passed-in (dotnet/corefx#42026)
Utf8JsonWriter is null. Commit migrated from dotnet/corefx@b8cb521
1 parent 474e1d6 commit 7224366

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Helpers.cs

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ private static void WriteValueCore(Utf8JsonWriter writer, object value, Type typ
9797
options = JsonSerializerOptions.s_defaultOptions;
9898
}
9999

100+
if (writer == null)
101+
{
102+
throw new ArgumentNullException(nameof(writer));
103+
}
104+
100105
WriteCore(writer, value, type, options);
101106
}
102107

@@ -111,6 +116,7 @@ private static void WriteCore(PooledByteBufferWriter output, object value, Type
111116
private static void WriteCore(Utf8JsonWriter writer, object value, Type type, JsonSerializerOptions options)
112117
{
113118
Debug.Assert(type != null || value == null);
119+
Debug.Assert(writer != null);
114120

115121
if (value == null)
116122
{

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Utf8JsonWriter.cs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public static partial class JsonSerializer
1212
/// <param name="writer">The writer to write.</param>
1313
/// <param name="value">The value to convert and write.</param>
1414
/// <param name="options">Options to control the behavior.</param>
15+
/// <exception cref="ArgumentNullException">
16+
/// <paramref name="writer"/> is null.
17+
/// </exception>
1518
public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions options = null)
1619
{
1720
WriteValueCore(writer, value, typeof(TValue), options);
@@ -24,6 +27,9 @@ public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSe
2427
/// <param name="value">The value to convert and write.</param>
2528
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
2629
/// <param name="options">Options to control the behavior.</param>
30+
/// <exception cref="ArgumentNullException">
31+
/// <paramref name="writer"/> is null.
32+
/// </exception>
2733
public static void Serialize(Utf8JsonWriter writer, object value, Type inputType, JsonSerializerOptions options = null)
2834
{
2935
VerifyValueAndType(value, inputType);

src/libraries/System.Text.Json/tests/Serialization/WriteValueTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ namespace System.Text.Json.Serialization.Tests
99
{
1010
public static partial class WriteValueTests
1111
{
12+
[Fact]
13+
public static void NullWriterThrows()
14+
{
15+
Assert.Throws<ArgumentNullException>(() => JsonSerializer.Serialize(null, 1));
16+
Assert.Throws<ArgumentNullException>(() => JsonSerializer.Serialize(null, 1, typeof(int)));
17+
}
18+
1219
[Fact]
1320
public static void CanWriteValueToJsonArray()
1421
{

0 commit comments

Comments
 (0)