Browse Source

Set ReadCommentHandling, AllowTrailingCommas for JsonSerializerOptions

pull/5952/head
maliming 6 years ago
parent
commit
739ac37333
  1. 4
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs
  2. 12
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs
  3. 3
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs
  4. 10
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs
  5. 4
      framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs

4
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<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
}

12
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<AbpJsonOptions> options, IServiceScopeFactory serviceScopeFactory)
public AbpHybridJsonSerializer(IOptions<AbpJsonOptions> 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<T>(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<T>(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);

3
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<IConfigureOptions<AbpSystemTextJsonSerializerOptions>,
AbpSystemTextJsonSerializerOptionsSetup>());
.Transient<IConfigureOptions<AbpSystemTextJsonSerializerOptions>, AbpSystemTextJsonSerializerOptionsSetup>());
Configure<AbpJsonOptions>(options =>
{

10
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();
}

4
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<JsonLocalizationFile>(jsonString, options);

Loading…
Cancel
Save