Skip to content

[System.Text.Json] Type deriving from List<T> without parameterless constructor results in null ref #30918

Description

@pranavkm
class Program
{
    static void Main(string[] args)
    {
        var json = "[1, 2]";
        var x = JsonSerializer.Deserialize<CustomList<int>>(json);
    }
}

public class CustomList<T> : List<T>
{
    public CustomList(int x) { }
}

Stack trace:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Text.Json.ReadStackFrame.CreateEnumerableValue(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.HandleStartArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)

Reproduced using Microsoft.NETCore.App 3.0.0-rc1-19456-20

Adding a parameterless ctor works, but the exception message likely needs to say that a parameterless ctor needs to be declared.

Metadata

Metadata

Assignees

Labels

area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions