Skip to content

System.Text.Json absent empty constructor exception on class, none for struct #35161

Description

@NinoFloris

System.Text.Json (netcore3.1) missing empty constructor exception on class, none for struct during deserialization.

public class Item 
{
    public Item(int value) => Value = value;
    public int Value { get; }
}

public struct ValueItem
{
    public ValueItem(int value) => Value = value;
    public int Value { get; }
}

JsonSerializer.Deserialize<Item>("{\"Value\": 1}"); // exception
JsonSerializer.Deserialize<ValueItem>("{\"Value\": 1}"); // no exception

It took me a while before I understood why none of my structs were getting filled with data.

From a type system perspective It makes sense as structs always have a default constructor, though it seems when there is actually data the serializer should throw an exception just like the class case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions