Skip to content

Text.Json fails to serialize schema with properties of derived overloaded classes #100541

Answered by elgonzo
ptr727 asked this question in Q&A
Discussion options

You must be logged in to vote

@ptr727

since the get accessor is not public, you need to annotate it with the [JsonInclude] attribute to include it in (de)serialization. E.g.:

public record Child1
{
    public Child1() { }

    [Obsolete]
    [JsonInclude]
    public List<Sub> List { internal get; set; } = [];
}

The documentation does not seem to be clear about this, but i suspect that the deserializer notices the lack of a public getter and thus excludes the property from deserialization. And because the deserialization logic doesn't acknowledge the public setter without considering the getter (similarily to it also not acknowledging setter-only properties), it therefore neither attempts creating a new List<Sub> insta…

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@ptr727
Comment options

@eiriktsarpalis
Comment options

@ptr727
Comment options

@elgonzo
Comment options

Answer selected by ptr727
@ptr727
Comment options

@eiriktsarpalis
Comment options

@ptr727
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
3 participants
Converted from issue

This discussion was converted from issue #100523 on April 02, 2024 18:11.