Skip to content

Commit

Permalink
Configure JsonOptions by default
Browse files Browse the repository at this point in the history
Fix #15416
  • Loading branch information
MikeAlhayek committed Mar 5, 2024
1 parent c45aadc commit 179cbd9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Text.Json;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.Options;

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.DefaultIgnoreCondition = _jsonSerializerOptions.DefaultIgnoreCondition;
options.SerializerOptions.ReferenceHandler = _jsonSerializerOptions.ReferenceHandler;
options.SerializerOptions.ReadCommentHandling = _jsonSerializerOptions.ReadCommentHandling;
options.SerializerOptions.PropertyNameCaseInsensitive = _jsonSerializerOptions.PropertyNameCaseInsensitive;
options.SerializerOptions.AllowTrailingCommas = _jsonSerializerOptions.AllowTrailingCommas;
options.SerializerOptions.WriteIndented = _jsonSerializerOptions.WriteIndented;
options.SerializerOptions.PropertyNamingPolicy = _jsonSerializerOptions.PropertyNamingPolicy;
options.SerializerOptions.Encoder = _jsonSerializerOptions.Encoder;
options.SerializerOptions.TypeInfoResolver = _jsonSerializerOptions.TypeInfoResolver;

foreach (var resolver in _jsonSerializerOptions.TypeInfoResolverChain)
{
options.SerializerOptions.TypeInfoResolverChain.Add(resolver);
}

foreach (var converter in _jsonSerializerOptions.Converters)
{
options.SerializerOptions.Converters.Add(converter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Localization;
Expand All @@ -30,6 +31,7 @@
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Environment.Shell.Descriptor.Models;
using OrchardCore.Extensions;
using OrchardCore.Json;
using OrchardCore.Localization;
using OrchardCore.Locking;
using OrchardCore.Locking.Distributed;
Expand Down Expand Up @@ -154,6 +156,7 @@ private static void AddDefaultServices(OrchardCoreBuilder builder)

services.AddSingleton<IPoweredByMiddlewareOptions, PoweredByMiddlewareOptions>();

services.AddTransient<IConfigureOptions<JsonOptions>, JsonOptionsConfigurations>();
services.AddTransient<IConfigureOptions<JsonSerializerOptions>, JsonSerializerOptionsConfiguration>();

services.AddScoped<IOrchardHelper, DefaultOrchardHelper>();
Expand Down

0 comments on commit 179cbd9

Please sign in to comment.