Skip to content

Commit 0a732ab

Browse files
committed
config JsonOptions globally /3
1 parent 7b78e58 commit 0a732ab

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

aspnetcore/fundamentals/openapi/include-metadata.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ An enum type without a [`[JsonConverter]`](xref:System.Text.Json.Serialization.
630630

631631
**Note:** The [`[AllowedValues]`](xref:System.ComponentModel.DataAnnotations.AllowedValuesAttribute) attribute does not set the `enum` values of a property.
632632

633+
[Set JSON options globally](#set-json-serialization-options-globally) shows how to set the the `JsonStringEnumConverter` globally.
634+
633635
#### nullable
634636

635637
Properties defined as a nullable value or reference type appear in the generated schema with a `type` keyword whose value is an array that includes `null` as one of the types. This is consistent with the default behavior of the <xref:System.Text.Json> deserializer, which accepts `null` as a valid value for a nullable property.
@@ -668,9 +670,9 @@ A schema transformer can be used to override any default metadata or add additio
668670

669671
## Set JSON serialization options globally
670672

671-
The following code shows how to configure JSON options globally, for Minimal APIs and Controler based APIs:
673+
The following code configures some JSON options globally, for Minimal APIs and Controler based APIs:
672674

673-
[!code-csharp[](~/fundamentals/openapi/samples/10.x/WebJson/Program.cs?highlight=8-19)]
675+
[!code-csharp[](~/fundamentals/openapi/samples/10.x/WebJson/Program.cs?highlight=8-29)]
674676

675677
## MVC JSON options and global JSON options
676678

aspnetcore/fundamentals/openapi/samples/10.x/WebJson/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using System.Text.Json.Serialization;
23
using Microsoft.AspNetCore.Http.Json;
34

@@ -9,13 +10,22 @@
910
{
1011
options.SerializerOptions.Converters.Add(
1112
new JsonStringEnumConverter<DayOfTheWeekAsString>());
13+
options.SerializerOptions.DefaultIgnoreCondition =
14+
JsonIgnoreCondition.WhenWritingNull;
15+
options.SerializerOptions.PropertyNamingPolicy =
16+
JsonNamingPolicy.CamelCase;
17+
1218
});
1319

1420
builder.Services.AddControllers()
1521
.AddJsonOptions(options =>
1622
{
1723
options.JsonSerializerOptions.Converters.Add(
1824
new JsonStringEnumConverter<DayOfTheWeekAsString>());
25+
options.JsonSerializerOptions.DefaultIgnoreCondition =
26+
JsonIgnoreCondition.WhenWritingNull;
27+
options.JsonSerializerOptions.PropertyNamingPolicy =
28+
JsonNamingPolicy.CamelCase;
1929
});
2030

2131
var app = builder.Build();

0 commit comments

Comments
 (0)