Link Search Menu Expand Document

Record: Serialize/deserialize attributes using primary constructor

The JsonPropertyName will override any naming policy specified by JsonNamingPolicy.

    public record Person(
        [property: JsonPropertyName("name_test")] string Name,
        [property: JsonPropertyName("age_test")] int Age
        );

    var person = new Person("userA", 42);
    var json = JsonSerializer.Serialize(person);
{
    "name_test": "userA",
    "age_test": 42
}

Sample


Created: 02.08.2021