Do not pass null value for parameter Type inputType
in JsonSerializer.Serialize
#20823
Closed
2 of 22 tasks
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 5
Work items for the .NET 5 release
Do not pass a null for parameter
Type inputType
in JsonSerializer.Serialize(...) overloadsPassing in null for the
Type
parameter of theJsonSerialaizer.Serialize
is unacceptable and should throw ArgumentNullException in that case. It was throwing the exception in most cases except one edge case: for certainJsonSerializer.Serialize
,JsonSerializer.SerializeAsync
andJsonSerializer.SerializeToUtf8Bytes()
overloads which acceptingobject value, Type inputType
as parameters, and when thosevalue
andinputType
parameters are bothnull
it was producing "null" JSON string. Now it throwsArgumentNullException
correctly wheneverType inputType
parameter is passed as null. Related to dotnet/runtime#528 (comment)Version introduced
5.0
Old behavior
New behavior
Passing in null for the Type parameter while serializing now correctly throws ArgumentNullException rather than returning "null" JSON:
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
parameterCategory
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
The text was updated successfully, but these errors were encountered: