How to map fields name in jsonb field to another name in object?
Solution 1:
npgsql driver uses System.Text.Json
for serializing json
values. So all you need is to annotate your property with JsonPropertyName
attribute.
public class BarData
{
[JsonPropertyName("baz")]
public String Baz { get; set; }
}
If your only concern is casing you can use JsonSerializerSettings.PropertyNamingPolicy
This is how you change serialization options as of version 6.0 :
var options = new JsonSerializerOptions
{
// Customize based on needs...
};
NpgsqlConnection.GlobalTypeMapper.AddTypeResolverFactory(new JsonOverrideTypeHandlerResolverFactory(options));
Credits to this github answer
Also worth mentioning that the second workaround won't affect the query generation.