Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
JsonSerializer.Serialize should check whether the passed-in (#42026) (#…
Browse files Browse the repository at this point in the history
…42027)

Utf8JsonWriter is null.
  • Loading branch information
ahsonkhan authored and danmoseley committed Oct 23, 2019
1 parent 92013f7 commit 427c7bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public static partial class JsonSerializer
/// <param name="writer">The writer to write.</param>
/// <param name="value">The value to convert and write.</param>
/// <param name="options">Options to control the behavior.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="writer"/> is null.
/// </exception>
public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions options = null)
{
WriteValueCore(writer, value, typeof(TValue), options);
Expand All @@ -24,6 +27,9 @@ public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSe
/// <param name="value">The value to convert and write.</param>
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param>
/// <param name="options">Options to control the behavior.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="writer"/> is null.
/// </exception>
public static void Serialize(Utf8JsonWriter writer, object value, Type inputType, JsonSerializerOptions options = null)
{
VerifyValueAndType(value, inputType);
Expand Down
7 changes: 7 additions & 0 deletions src/System.Text.Json/tests/Serialization/WriteValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace System.Text.Json.Serialization.Tests
{
public static partial class WriteValueTests
{
[Fact]
public static void NullWriterThrows()
{
Assert.Throws<ArgumentNullException>(() => JsonSerializer.Serialize(null, 1));
Assert.Throws<ArgumentNullException>(() => JsonSerializer.Serialize(null, 1, typeof(int)));
}

[Fact]
public static void CanWriteValueToJsonArray()
{
Expand Down

0 comments on commit 427c7bd

Please sign in to comment.