Creating a Custom JsonNamingPolicy

System.Text.Json is one of the most used JSON libraries in dotnet, and it supports Pascal casing by default and provides an extra Camel casing support as the configuration parameter.

Let's start our Custom Naming Policy

Domain Logic

First is the domain or algorithm to convert the string to a snake case.

I got this from an Entity framework core project, and they support different naming Policies.

The next step is to Override the JsonNamingPolicy.

Now that the Snake Case Policy is available, it's time to add it to the Json Serialization Options property.

The JsonSerializerOptions class should not be created multiple times to optimize the code, as is the naming policy. We have created a static instance of the Naming class.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store