Skip to content

Do not pass null value for parameter Type inputType in JsonSerializer.Serialize #20823

@buyaa-n

Description

@buyaa-n

Do not pass a null for parameter Type inputType in JsonSerializer.Serialize(...) overloads

Passing in null for the Type parameter of the JsonSerialaizer.Serialize is unacceptable and should throw ArgumentNullException in that case. It was throwing the exception in most cases except one edge case: for certain JsonSerializer.Serialize, JsonSerializer.SerializeAsync and JsonSerializer.SerializeToUtf8Bytes() overloads which accepting object value, Type inputType as parameters, and when those value and inputType parameters are both null it was producing "null" JSON string. Now it throws ArgumentNullException correctly whenever Type inputType parameter is passed as null. Related to dotnet/runtime#528 (comment)

Version introduced

5.0

Old behavior

public static void TestSerializeNullType()
{
    // Before (3.1): Returned a string with value "null"
    // Same with other Serialize{Async} overloads
    JsonSerializer.Serialize(null, null);

    // Before (3.1): Returned a byte[] with value "null"
    JsonSerializer.SerializeToUtf8Bytes(null, null);
}

New behavior

Passing in null for the Type parameter while serializing now correctly throws ArgumentNullException rather than returning "null" JSON:

public static void TestSerializeNullType()
{
    // After (5.0): ArgumentNullException: Value cannot be null. (Parameter 'inputType')
    // Same with other Serialize{Async} overloads
    JsonSerializer.Serialize(null, null);

    // After (5.0): ArgumentNullException: Value cannot be null. (Parameter 'inputType')
    JsonSerializer.SerializeToUtf8Bytes(null, null);
}

Reason for change

Passing in null for the Type inputType parameter of the JsonSerialaizer is unacceptable and should always throw ArgumentNullException.

Recommended action

Avoid passing null value for Type inputType parameter

Category

  • ASP.NET Core
  • C#
  • Code analysis
  • Core .NET libraries
  • Cryptography
  • Data
  • Debugger
  • Deployment
  • Globalization
  • Interop
  • JIT
  • LINQ
  • Managed Extensibility Framework (MEF)
  • MSBuild
  • Networking
  • Printing
  • Security
  • Serialization
  • Visual Basic
  • Windows Forms
  • Windows Presentation Foundation (WPF)
  • XML, XSLT

Affected APIs

System.Text.Json.JsonSerializer.Serialize(Object, Type, JsonSerializerOptions)
System.Text.Json.JsonSerializer.Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)
System.Text.Json.JsonSerializer.SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)
System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions)


Issue metadata

  • Issue type: breaking-change

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 5Work items for the .NET 5 releasebreaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions