Skip to content

J0rgeSerran0/JsonNamingPolicy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Namespace JSON Naming Policies Compatibility

JsonNamingPolicy

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

afbeelding

Update Jul 23 2021

Microsoft has moved the implementation of kebab-case and snake_case to .NET 7

IMAGE

Additional information here

NuGet Package

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

JsonSnakeCaseNamingPolicy

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

Use

var options = new JsonSerializerOptions() { PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy() };
var person = new Person() { FirstName = "Jorge", Birthday = DateTime.UtcNow, MyJobCity = "Madrid" };
var json = JsonSerializer.Serialize(person, options);

Result

{"first_name":"Jorge","birthday":"2020-01-03T20:00:59.6991482Z","my_job_city":"Madrid"}

JsonKebabCaseNamingPolicy

This class creates a policy to change the name properties of a JSON into KebabCase (kebab-case).

Use

var options = new JsonSerializerOptions() { PropertyNamingPolicy = new JsonKebabCaseNamingPolicy() };
var person = new Person() { FirstName = "Jorge", Birthday = DateTime.UtcNow, MyJobCity = "Madrid" };
var json = JsonSerializer.Serialize(person, options);

Result

{"first-name":"Jorge","birthday":"2020-01-03T20:00:59.6991482Z","my-job-city":"Madrid"}