Naming policies for System.Text.Json
This GitHub repo contains classes to change the name properties of a JSON into different naming conventions.
Update April 5 2022
Unlikely it will be added in .NET 7
Update Jul 23 2021
Microsoft has moved the implementation of kebab-case and snake_case to .NET 7
Additional information here
Update Oct 13 2021 - NuGet Package NickJohn published 6 months ago, a NuGet Package with the code I share here.
You can incorporate the code easily inside of your developments.
You will find this NuGet Package here
Thanks a lot NickeJohn
This class creates a policy to change the name properties of a JSON into SnakeCase (snake_case) used in some programming languages and APIs.
This code passes the tests that you will find in the corefx SnakeCaseUnitTests of Microsoft
var options = new JsonSerializerOptions() { PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy() };
var person = new Person() { FirstName = "Jorge", Birthday = DateTime.UtcNow, MyJobCity = "Madrid" };
var json = JsonSerializer.Serialize(person, options);
{"first_name":"Jorge","birthday":"2020-01-03T20:00:59.6991482Z","my_job_city":"Madrid"}
This class creates a policy to change the name properties of a JSON into KebabCase (kebab-case).
var options = new JsonSerializerOptions() { PropertyNamingPolicy = new JsonKebabCaseNamingPolicy() };
var person = new Person() { FirstName = "Jorge", Birthday = DateTime.UtcNow, MyJobCity = "Madrid" };
var json = JsonSerializer.Serialize(person, options);
{"first-name":"Jorge","birthday":"2020-01-03T20:00:59.6991482Z","my-job-city":"Madrid"}