DynamicJsonPropertyNamingPolicy 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DynamicJsonPropertyNamingPolicy --version 1.1.0
NuGet\Install-Package DynamicJsonPropertyNamingPolicy -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicJsonPropertyNamingPolicy" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicJsonPropertyNamingPolicy --version 1.1.0
#r "nuget: DynamicJsonPropertyNamingPolicy, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DynamicJsonPropertyNamingPolicy as a Cake Addin
#addin nuget:?package=DynamicJsonPropertyNamingPolicy&version=1.1.0

// Install DynamicJsonPropertyNamingPolicy as a Cake Tool
#tool nuget:?package=DynamicJsonPropertyNamingPolicy&version=1.1.0

DynamicJsonPropertyNamingPolicy

Nuget

A System.Text.Json.JsonNamingPolicy that reacts to request headers.

Usage

Based on the value of the json-naming-strategy request header, this package will determine what type of serialization method to follow when one of the following is used:

  1. camel for camelCase
  2. pascal for PascalCase
  3. snake for snake_case

If nothing is sent in the header, it will default to snake_case for compatibility with the systems it was originally built for.

As an InputFormatter

This method will allow your ASP.NET controllers to correctly deserialize incoming JSON following the json-naming-strategy.

builder.Services.AddControllers(o =>
{
    o.InputFormatters.Insert(0, new DynamicSystemTextJsonInputFormatter());
});

Which would then allow you to receive a request body of:

{
    "time_stamp": "2021-09-16T22:05:29.846Z"
}

and correctly deserialize it into:

public record WeatherForecast
{
    public DateTime TimeStamp { get; set; }
}

In JsonSerializerOptions

If you just want to use JsonSerializerOptions that apply the correct property naming use the following:

return new JsonResult(result, HttpContext.GetJsonSerializerOptions());

Otherwise, you can just reference the JsonNamingPolicy to add additional options:

var namingPolicy = HttpContext.GetJsonNamingPolicy();
return new JsonResult(result, new JsonSerializerOptions
{
    PropertyNamingPolicy = namingPolicy,
    NumberHandling = JsonNumberHandling.AllowReadingFromString
});

This allows you to send a response back to the requestor that will respect their request for a specific serialization strategy.

Example

You can see usage as an InputFormatter in Program.cs and as JsonSerializerOptions in WeatherForecastController.cs.

Additionally, you can run the Sample project with:

dotnet run

And then load https://localhost:5001/swagger to try out some requests. Within the SwaggerUI, you can specify the json-naming-strategy header to see the differences.

Contributing

Please see CONTRIBUTING.md for details on how to contribute to the project.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.4 1,877 4/25/2022
1.1.3 430 4/15/2022
1.1.2 497 3/8/2022
1.1.1 585 9/28/2021
1.1.0 375 9/22/2021
1.0.1 282 9/22/2021