Browse Source

Merge pull request #15994 from abpframework/auto-merge/rel-7-1/1806

Merge branch dev with rel-7.1
pull/16009/head
maliming 4 years ago
committed by GitHub
parent
commit
abc9d31359
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs
  2. 4
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs
  3. 18
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs
  4. 16
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs
  5. 7
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs
  6. 16
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializer.cs
  7. 3
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs
  8. 8
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs
  9. 4
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs

7
framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Json.Newtonsoft; using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -13,9 +14,9 @@ public class AbpAspNetCoreMvcNewtonsoftModule : AbpModule
context.Services.AddMvcCore().AddNewtonsoftJson(); context.Services.AddMvcCore().AddNewtonsoftJson();
context.Services.AddOptions<MvcNewtonsoftJsonOptions>() context.Services.AddOptions<MvcNewtonsoftJsonOptions>()
.Configure<AbpCamelCasePropertyNamesContractResolver>((options, contractResolver) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
options.SerializerSettings.ContractResolver = contractResolver; options.SerializerSettings.ContractResolver = new AbpCamelCasePropertyNamesContractResolver(rootServiceProvider.GetRequiredService<AbpDateTimeConverter>());
}); });
} }
} }

4
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs

@ -13,7 +13,7 @@ public static class MvcCoreBuilderExtensions
public static IMvcCoreBuilder AddAbpJson(this IMvcCoreBuilder builder) public static IMvcCoreBuilder AddAbpJson(this IMvcCoreBuilder builder)
{ {
builder.Services.AddOptions<JsonOptions>() builder.Services.AddOptions<JsonOptions>()
.Configure<IServiceProvider>((options, serviceProvider) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip; options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
options.JsonSerializerOptions.AllowTrailingCommas = true; options.JsonSerializerOptions.AllowTrailingCommas = true;
@ -24,7 +24,7 @@ public static class MvcCoreBuilderExtensions
options.JsonSerializerOptions.Converters.Add(new AbpNullableStringToGuidConverter()); options.JsonSerializerOptions.Converters.Add(new AbpNullableStringToGuidConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter()); options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(rootServiceProvider
.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>()); .GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
}); });

18
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs

@ -1,22 +1,16 @@
using System; using System.Reflection;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Json.Newtonsoft; namespace Volo.Abp.Json.Newtonsoft;
public class AbpCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver, ITransientDependency public class AbpCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver
{ {
private readonly Lazy<AbpDateTimeConverter> _dateTimeConverter; private readonly AbpDateTimeConverter _dateTimeConverter;
public AbpCamelCasePropertyNamesContractResolver(IServiceProvider serviceProvider) public AbpCamelCasePropertyNamesContractResolver(AbpDateTimeConverter dateTimeConverter)
{ {
_dateTimeConverter = new Lazy<AbpDateTimeConverter>( _dateTimeConverter = dateTimeConverter;
serviceProvider.GetRequiredService<AbpDateTimeConverter>,
true
);
NamingStrategy = new CamelCaseNamingStrategy NamingStrategy = new CamelCaseNamingStrategy
{ {
@ -30,7 +24,7 @@ public class AbpCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesC
if (AbpDateTimeConverter.ShouldNormalize(member, property)) if (AbpDateTimeConverter.ShouldNormalize(member, property))
{ {
property.Converter = _dateTimeConverter.Value; property.Converter = _dateTimeConverter;
} }
return property; return property;

16
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs

@ -1,22 +1,16 @@
using System;
using System.Reflection; using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Json.Newtonsoft; namespace Volo.Abp.Json.Newtonsoft;
public class AbpDefaultContractResolver : DefaultContractResolver, ITransientDependency public class AbpDefaultContractResolver : DefaultContractResolver
{ {
private readonly Lazy<AbpDateTimeConverter> _dateTimeConverter; private readonly AbpDateTimeConverter _dateTimeConverter;
public AbpDefaultContractResolver(IServiceProvider serviceProvider) public AbpDefaultContractResolver(AbpDateTimeConverter dateTimeConverter)
{ {
_dateTimeConverter = new Lazy<AbpDateTimeConverter>( _dateTimeConverter = dateTimeConverter;
serviceProvider.GetRequiredService<AbpDateTimeConverter>,
true
);
} }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
@ -25,7 +19,7 @@ public class AbpDefaultContractResolver : DefaultContractResolver, ITransientDep
if (AbpDateTimeConverter.ShouldNormalize(member, property)) if (AbpDateTimeConverter.ShouldNormalize(member, property))
{ {
property.Converter = _dateTimeConverter.Value; property.Converter = _dateTimeConverter;
} }
return property; return property;

7
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs

@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection; using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Timing; using Volo.Abp.Timing;
@ -10,9 +11,9 @@ public class AbpJsonNewtonsoftModule : AbpModule
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.AddOptions<AbpNewtonsoftJsonSerializerOptions>() context.Services.AddOptions<AbpNewtonsoftJsonSerializerOptions>()
.Configure<AbpCamelCasePropertyNamesContractResolver>((options, contractResolver) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
options.JsonSerializerSettings.ContractResolver = contractResolver; options.JsonSerializerSettings.ContractResolver = new AbpCamelCasePropertyNamesContractResolver(rootServiceProvider.GetRequiredService<AbpDateTimeConverter>());
}); });
} }
} }

16
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializer.cs

@ -10,12 +10,12 @@ namespace Volo.Abp.Json.Newtonsoft;
[Dependency(ReplaceServices = true)] [Dependency(ReplaceServices = true)]
public class AbpNewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency public class AbpNewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
{ {
protected IServiceProvider ServiceProvider { get; } protected IRootServiceProvider RootServiceProvider { get; }
protected IOptions<AbpNewtonsoftJsonSerializerOptions> Options { get; } protected IOptions<AbpNewtonsoftJsonSerializerOptions> Options { get; }
public AbpNewtonsoftJsonSerializer(IServiceProvider serviceProvider, IOptions<AbpNewtonsoftJsonSerializerOptions> options) public AbpNewtonsoftJsonSerializer(IRootServiceProvider rootServiceProvider, IOptions<AbpNewtonsoftJsonSerializerOptions> options)
{ {
ServiceProvider = serviceProvider; RootServiceProvider = rootServiceProvider;
Options = options; Options = options;
} }
@ -34,7 +34,7 @@ public class AbpNewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
return JsonConvert.DeserializeObject(jsonString, type, CreateJsonSerializerOptions(camelCase)); return JsonConvert.DeserializeObject(jsonString, type, CreateJsonSerializerOptions(camelCase));
} }
private static readonly ConcurrentDictionary<object, JsonSerializerSettings> JsonSerializerOptionsCache = private readonly static ConcurrentDictionary<object, JsonSerializerSettings> JsonSerializerOptionsCache =
new ConcurrentDictionary<object, JsonSerializerSettings>(); new ConcurrentDictionary<object, JsonSerializerSettings>();
protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
@ -81,9 +81,11 @@ public class AbpNewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
TypeNameAssemblyFormatHandling = Options.Value.JsonSerializerSettings.TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling = Options.Value.JsonSerializerSettings.TypeNameAssemblyFormatHandling
}; };
settings.ContractResolver = camelCase if (!camelCase)
? ServiceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>() {
: ServiceProvider.GetRequiredService<AbpDefaultContractResolver>(); //Default contract resolver is AbpCamelCasePropertyNamesContractResolver}
settings.ContractResolver = new AbpDefaultContractResolver(RootServiceProvider.GetRequiredService<AbpDateTimeConverter>());
}
if (indented) if (indented)
{ {

3
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs

@ -1,10 +1,9 @@
using System.Text.Json.Serialization.Metadata; using System.Text.Json.Serialization.Metadata;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Json.SystemTextJson; namespace Volo.Abp.Json.SystemTextJson;
public class AbpDefaultJsonTypeInfoResolver : DefaultJsonTypeInfoResolver, ITransientDependency public class AbpDefaultJsonTypeInfoResolver : DefaultJsonTypeInfoResolver
{ {
public AbpDefaultJsonTypeInfoResolver(IOptions<AbpSystemTextJsonSerializerModifiersOptions> options) public AbpDefaultJsonTypeInfoResolver(IOptions<AbpSystemTextJsonSerializerModifiersOptions> options)
{ {

8
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs

@ -15,7 +15,7 @@ public class AbpJsonSystemTextJsonModule : AbpModule
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.AddOptions<AbpSystemTextJsonSerializerOptions>() context.Services.AddOptions<AbpSystemTextJsonSerializerOptions>()
.Configure<IServiceProvider>((options, serviceProvider) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
// If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters. // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
options.JsonSerializerOptions.Encoder ??= JavaScriptEncoder.UnsafeRelaxedJsonEscaping; options.JsonSerializerOptions.Encoder ??= JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
@ -26,14 +26,14 @@ public class AbpJsonSystemTextJsonModule : AbpModule
options.JsonSerializerOptions.Converters.Add(new AbpNullableStringToGuidConverter()); options.JsonSerializerOptions.Converters.Add(new AbpNullableStringToGuidConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter()); options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(rootServiceProvider
.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>()); .GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
}); });
context.Services.AddOptions<AbpSystemTextJsonSerializerModifiersOptions>() context.Services.AddOptions<AbpSystemTextJsonSerializerModifiersOptions>()
.Configure<IServiceProvider>((options, serviceProvider) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(serviceProvider)); options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(rootServiceProvider));
}); });
} }
} }

4
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs

@ -35,10 +35,10 @@ public class AbpMemoryDbTestModule : AbpModule
}); });
context.Services.AddOptions<Utf8JsonMemoryDbSerializerOptions>() context.Services.AddOptions<Utf8JsonMemoryDbSerializerOptions>()
.Configure<IServiceProvider>((options, serviceProvider) => .Configure<IServiceProvider>((options, rootServiceProvider) =>
{ {
options.JsonSerializerOptions.Converters.Add(new EntityJsonConverter<EntityWithIntPk, int>()); options.JsonSerializerOptions.Converters.Add(new EntityJsonConverter<EntityWithIntPk, int>());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(rootServiceProvider
.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>()); .GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
}); });
} }

Loading…
Cancel
Save