Skip to content

Commit

Permalink
Configure JsonOptions by default (#15460)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Mar 7, 2024
1 parent 804b814 commit d99deaa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
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;
}
}
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);
}
}
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 d99deaa

Please sign in to comment.