Serializing JSON with custom property names

When serializing with the System.text.json library, the names are inferred from the property names of the class.

To change this behavior we must add the JsonPropertyName attribute like this:


public class SomeResponse {
    [JsonPropertyName("access_token")]
    public string AccessToken { get; set; } = "";
}

The result of the serialization is 


{
    "acccess_token": ""
}

Post a Comment

Previous Post Next Post