From dd807a80a35b043c8e2fe2d67f84c833b9ee6fbd Mon Sep 17 00:00:00 2001 From: Khanhnguyen Date: Sun, 21 Feb 2021 18:55:26 +0700 Subject: [PATCH] Add switch Json Serializer capability --- .../Controllers/PageControllerBase.cs | 15 +++++++++++++-- src/SilkierQuartz/Helpers/JsonErrorResponse.cs | 11 ++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/SilkierQuartz/Controllers/PageControllerBase.cs b/src/SilkierQuartz/Controllers/PageControllerBase.cs index 17b7cb5..ef2b1a3 100644 --- a/src/SilkierQuartz/Controllers/PageControllerBase.cs +++ b/src/SilkierQuartz/Controllers/PageControllerBase.cs @@ -13,21 +13,32 @@ namespace SilkierQuartz.Controllers #region Target-Specific Directives #if ( NETSTANDARD || NETCOREAPP ) + using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; + using System.Text.Json; - public abstract partial class PageControllerBase : Microsoft.AspNetCore.Mvc.ControllerBase + public abstract partial class PageControllerBase : Microsoft.AspNetCore.Mvc.ControllerBase { private static readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver(), // PascalCase as default }; + private static readonly object _systemTextSerializerOptions = new JsonSerializerOptions(); protected Services Services => (Services) Request.HttpContext.Items[typeof(Services)]; protected string GetRouteData(string key) => RouteData.Values[key].ToString(); - protected IActionResult Json( object content ) => new JsonResult( content, _serializerSettings ); + protected IActionResult Json( object content ) + { + var executor = HttpContext.RequestServices.GetRequiredService>(); + var serializerOptions = executor.GetType().FullName.Contains("SystemTextJson") + ? _systemTextSerializerOptions + : _serializerSettings; + return new JsonResult(content, serializerOptions); + } protected IActionResult NotModified() => new StatusCodeResult(304); diff --git a/src/SilkierQuartz/Helpers/JsonErrorResponse.cs b/src/SilkierQuartz/Helpers/JsonErrorResponse.cs index 59b897c..d4f09ed 100644 --- a/src/SilkierQuartz/Helpers/JsonErrorResponse.cs +++ b/src/SilkierQuartz/Helpers/JsonErrorResponse.cs @@ -3,9 +3,12 @@ namespace SilkierQuartz.Helpers { #if (NETSTANDARD || NETCOREAPP) using Microsoft.AspNetCore.Mvc.Filters; + using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; + using System.Text.Json; public class JsonErrorResponseAttribute : ActionFilterAttribute { @@ -13,12 +16,18 @@ namespace SilkierQuartz.Helpers { ContractResolver = new DefaultContractResolver(), // PascalCase as default }; + private static readonly object _systemTextSerializerOptions = new JsonSerializerOptions(); public override void OnActionExecuted(ActionExecutedContext context) { if (context.Exception != null) { - context.Result = new JsonResult(new { ExceptionMessage = context.Exception.Message }, _serializerSettings) { StatusCode = 400 }; + var executor = context.HttpContext.RequestServices.GetRequiredService>(); + var serializerOptions = executor.GetType().FullName.Contains("SystemTextJson") + ? _systemTextSerializerOptions + : _serializerSettings; + + context.Result = new JsonResult(new { ExceptionMessage = context.Exception.Message }, serializerOptions) { StatusCode = 400 }; context.ExceptionHandled = true; } }