Skip to content

Commit

Permalink
Added support for custom JSON settings. Fixes #1354
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Dec 15, 2020
1 parent 10dabad commit f75594f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion core/Piranha.Manager/ManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*
*/

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
Expand Down Expand Up @@ -301,7 +303,8 @@ public static void MapPiranhaManager(this IEndpointRouteBuilder builder)
builder.MapRazorPages();
}

public static IMvcBuilder AddPiranhaManagerOptions(this IMvcBuilder builder)
public static IMvcBuilder AddPiranhaManagerOptions(this IMvcBuilder builder,
Action<MvcNewtonsoftJsonOptions> jsonOptions = null)
{
return builder
.AddRazorPagesOptions(options =>
Expand All @@ -313,6 +316,10 @@ public static IMvcBuilder AddPiranhaManagerOptions(this IMvcBuilder builder)
.AddDataAnnotationsLocalization()
.AddNewtonsoftJson(options =>
{
// Invoke custom json options
jsonOptions?.Invoke(options);

// Set required options for Piranha
options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
});
}
Expand Down
8 changes: 6 additions & 2 deletions core/Piranha.Manager/ManagerStartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*
*/

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Piranha;
using Piranha.AspNetCore;
Expand All @@ -19,16 +21,18 @@ public static class ManagerStartupExtensions
/// Uses the Piranha Manager services if simple startup is used.
/// </summary>
/// <param name="serviceBuilder">The service builder</param>
/// <param name="jsonOptions">Optional JSON options</param>
/// <returns>The updated builder</returns>
public static PiranhaServiceBuilder UseManager(this PiranhaServiceBuilder serviceBuilder)
public static PiranhaServiceBuilder UseManager(this PiranhaServiceBuilder serviceBuilder,
Action<MvcNewtonsoftJsonOptions> jsonOptions = null)
{
// Add dependent services
serviceBuilder.Services.AddLocalization(options =>
options.ResourcesPath = "Resources"
);
serviceBuilder.Services.AddControllersWithViews();
serviceBuilder.Services.AddRazorPages()
.AddPiranhaManagerOptions();
.AddPiranhaManagerOptions(jsonOptions);

// Add manager services
serviceBuilder.Services.AddPiranhaManager();
Expand Down

0 comments on commit f75594f

Please sign in to comment.