Skip to content

JsonSerializer.Serialize produces invalid JSON for [JsonExtensionData] property #97225

@KalininAndreyVictorovich

Description

Description

When serializing object containing property marked with [JsonExtensionData] attribute, serializer produces invalid JSON like this one: {"Id":1,{"nested":true}}

Reproduction Steps

Run the following code

var mix = new Mix
{
    Id = 1,
    Extra = new() { ["nested"] = true, }
};

var text = System.Text.Json.JsonSerializer.Serialize(mix);
Console.WriteLine(text);
// output {"Id":1,{"nested":true}}


public class Mix
{
    public int Id { get; set; }

    [System.Text.Json.Serialization.JsonExtensionData]
    public System.Text.Json.Nodes.JsonObject? Extra { get; set; }
}

Expected behavior

Correct JSON like {"Id":1,"nested":true} or at least valid JSON as if there was not [JsonExtensionData] attribute ({"Id":1,"Extra":{"nested":true}} ).

Actual behavior

Invalid JSON {"Id":1,{"nested":true}}

Regression?

Reproducible at least on .Net 6 to .Net 8

Known Workarounds

No response

Configuration

.Net 8
Windows 11
x64

Other information

No response

Metadata

Metadata

Labels

area-System.Text.Jsonbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions