DynamicJsonPropertyNamingPolicy 1.1.0
See the version list below for details.
dotnet add package DynamicJsonPropertyNamingPolicy --version 1.1.0
NuGet\Install-Package DynamicJsonPropertyNamingPolicy -Version 1.1.0
<PackageReference Include="DynamicJsonPropertyNamingPolicy" Version="1.1.0" />
paket add DynamicJsonPropertyNamingPolicy --version 1.1.0
#r "nuget: DynamicJsonPropertyNamingPolicy, 1.1.0"
// 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
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:
camel
forcamelCase
pascal
forPascalCase
snake
forsnake_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 | Versions 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. |
-
net5.0
- System.Net.Http.Json (>= 5.0.0)
- System.Text.Json (>= 5.0.2)
-
net6.0
- System.Net.Http.Json (>= 5.0.0)
- System.Text.Json (>= 5.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.