From 739ac37333efc6831441f2c6834ad471b7cc6f75 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 2 Nov 2020 15:08:20 +0800 Subject: [PATCH] Set ReadCommentHandling, AllowTrailingCommas for JsonSerializerOptions --- .../Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs | 4 ++++ .../Volo/Abp/Json/AbpHybridJsonSerializer.cs | 12 ++++++------ .../src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs | 3 +-- .../AbpSystemTextJsonSerializerOptions.cs | 10 +++++----- .../Json/JsonLocalizationDictionaryBuilder.cs | 4 +++- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs index b456204ed8..4b63828997 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -17,6 +18,9 @@ namespace Volo.Abp.AspNetCore.Mvc.Json public void Configure(JsonOptions options) { + options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip; + options.JsonSerializerOptions.AllowTrailingCommas = true; + options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService()); options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService()); } diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs index 7a108fbac0..69f27d4b3c 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs @@ -10,17 +10,17 @@ namespace Volo.Abp.Json { protected AbpJsonOptions Options { get; } - protected IServiceScopeFactory ServiceScopeFactory { get; } + protected IHybridServiceScopeFactory HybridServiceScopeFactory { get; } - public AbpHybridJsonSerializer(IOptions options, IServiceScopeFactory serviceScopeFactory) + public AbpHybridJsonSerializer(IOptions options, IHybridServiceScopeFactory hybridServiceScopeFactory) { Options = options.Value; - ServiceScopeFactory = serviceScopeFactory; + HybridServiceScopeFactory = hybridServiceScopeFactory; } public string Serialize(object obj, bool camelCase = true, bool indented = false) { - using (var scope = ServiceScopeFactory.CreateScope()) + using (var scope = HybridServiceScopeFactory.CreateScope()) { var serializerProvider = GetSerializerProvider(scope.ServiceProvider, obj.GetType()); return serializerProvider.Serialize(obj, camelCase, indented); @@ -29,7 +29,7 @@ namespace Volo.Abp.Json public T Deserialize(string jsonString, bool camelCase = true) { - using (var scope = ServiceScopeFactory.CreateScope()) + using (var scope = HybridServiceScopeFactory.CreateScope()) { var serializerProvider = GetSerializerProvider(scope.ServiceProvider, typeof(T)); return serializerProvider.Deserialize(jsonString, camelCase); @@ -38,7 +38,7 @@ namespace Volo.Abp.Json public object Deserialize(Type type, string jsonString, bool camelCase = true) { - using (var scope = ServiceScopeFactory.CreateScope()) + using (var scope = HybridServiceScopeFactory.CreateScope()) { var serializerProvider = GetSerializerProvider(scope.ServiceProvider, type); return serializerProvider.Deserialize(type, jsonString, camelCase); diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs index c5f102f9c9..fae54ec08b 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs @@ -14,8 +14,7 @@ namespace Volo.Abp.Json public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.TryAddEnumerable(ServiceDescriptor - .Transient, - AbpSystemTextJsonSerializerOptionsSetup>()); + .Transient, AbpSystemTextJsonSerializerOptionsSetup>()); Configure(options => { diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs index 17a4ba661a..e2774df58a 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs @@ -11,11 +11,11 @@ namespace Volo.Abp.Json.SystemTextJson public AbpSystemTextJsonSerializerOptions() { - //TODO:Defaults? - //https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/JsonOptions.cs#L18 - //https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs - - JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); + JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web) + { + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; UnsupportedTypes = new TypeList(); } diff --git a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs index c13fb643bd..9ccc6738a6 100644 --- a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs +++ b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs @@ -36,7 +36,9 @@ namespace Volo.Abp.Localization.Json var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, - DictionaryKeyPolicy = JsonNamingPolicy.CamelCase + DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true }; jsonFile = JsonSerializer.Deserialize(jsonString, options);