-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure JsonOptions by default (#15460)
- Loading branch information
1 parent
804b814
commit d99deaa
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/OrchardCore/OrchardCore.Abstractions/Json/Extensions/JsonSerializerOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Text.Json; | ||
|
||
namespace OrchardCore.Json.Extensions; | ||
|
||
public static class JsonSerializerOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Merges the given <see cref="JsonSerializerOptions"/> into the current options. | ||
/// </summary> | ||
public static JsonSerializerOptions Merge(this JsonSerializerOptions destination, JsonSerializerOptions source) | ||
{ | ||
destination.DefaultIgnoreCondition = source.DefaultIgnoreCondition; | ||
destination.ReferenceHandler = source.ReferenceHandler; | ||
destination.ReadCommentHandling = source.ReadCommentHandling; | ||
destination.PropertyNameCaseInsensitive = source.PropertyNameCaseInsensitive; | ||
destination.AllowTrailingCommas = source.AllowTrailingCommas; | ||
destination.WriteIndented = source.WriteIndented; | ||
destination.PropertyNamingPolicy = source.PropertyNamingPolicy; | ||
destination.Encoder = source.Encoder; | ||
destination.TypeInfoResolver = source.TypeInfoResolver; | ||
|
||
foreach (var resolver in source.TypeInfoResolverChain) | ||
{ | ||
if (destination.TypeInfoResolverChain.Contains(resolver)) | ||
{ | ||
continue; | ||
} | ||
|
||
destination.TypeInfoResolverChain.Add(resolver); | ||
} | ||
|
||
foreach (var converter in source.Converters) | ||
{ | ||
if (destination.Converters.Contains(converter)) | ||
{ | ||
continue; | ||
} | ||
|
||
destination.Converters.Add(converter); | ||
} | ||
|
||
return destination; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/OrchardCore/OrchardCore.Abstractions/Json/JsonOptionsConfigurations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.Http.Json; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Json.Extensions; | ||
|
||
namespace OrchardCore.Json; | ||
|
||
public class JsonOptionsConfigurations : IConfigureOptions<JsonOptions> | ||
{ | ||
private readonly JsonSerializerOptions _jsonSerializerOptions; | ||
|
||
public JsonOptionsConfigurations(IOptions<JsonSerializerOptions> jsonSerializerOptions) | ||
{ | ||
_jsonSerializerOptions = jsonSerializerOptions.Value; | ||
} | ||
|
||
public void Configure(JsonOptions options) | ||
{ | ||
options.SerializerOptions.Merge(_jsonSerializerOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters