I'm interfacing with an API which uses snake_case as convention names. I would like to map them to fields which are named with CamelCase. Previously, I've used Newtonsoft.Json's NamingStrategy
. A System.Text.Json equivalent seems to be PropertyNamingPolicy
.
I've created a global JsonSerializerOptions object and set the PropertyNamingPolicy
as follows:
public static readonly `JsonSerializerOptions` JsonSettings = new JsonSerializerOptions()
{
Converters =
{
new GatewayMessageJsonConverter(),
new HeartbeatMessageJsonConverter(),
new OpcodeJsonConverter(),
new InstantJsonConverter()
},
IgnoreNullValues = true,
PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy(),
DictionaryKeyPolicy = new JsonSnakeCaseNamingPolicy()
};
Which I use while deserializing by passing it as an argument:
JsonSerializer.Deserialize<GatewayRestResponse>(responseString.Result, JsonSettings)
This however does not work. Debugging seems to indicate that the ConvertName
function in the JsonSnakeCaseNamingPolicy
never even gets called.
Is my configuration wrong, or does System.Text.Json not support this?