Browse Source

Use action replace the `IConfigureOptions` classes.

pull/13357/head
maliming 4 years ago
parent
commit
e2748ce82a
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 43
      framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs
  2. 11
      framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftOptions.cs
  3. 31
      framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs
  4. 33
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs
  5. 23
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs
  6. 9
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs
  7. 5
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerOptions.cs
  8. 59
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs
  9. 30
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs
  10. 20
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs
  11. 31
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs

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

@ -1,10 +1,14 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Volo.Abp.AspNetCore.Mvc.Json;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Modularity;
namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson;
@ -14,29 +18,30 @@ public class AbpAspNetCoreMvcNewtonsoftModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var options = context.Services.ExecutePreConfiguredActions<AbpAspNetCoreMvcNewtonsoftOptions>();
context.Services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
if (options.UseHybridSerializer)
Configure<MvcOptions>(mvcOptions =>
{
context.Services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
mvcOptions.InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>();
mvcOptions.OutputFormatters.RemoveType<NewtonsoftJsonOutputFormatter>();
});
Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>();
mvcOptions.OutputFormatters.RemoveType<NewtonsoftJsonOutputFormatter>();
});
Configure<AbpHybridJsonFormatterOptions>(formatterOptions =>
{
formatterOptions.TextInputFormatters.Add<AbpNewtonsoftJsonFormatter>();
formatterOptions.TextOutputFormatters.Add<AbpNewtonsoftJsonFormatter>();
});
Configure<AbpHybridJsonFormatterOptions>(formatterOptions =>
context.Services.AddOptions<MvcNewtonsoftJsonOptions>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
formatterOptions.TextInputFormatters.Add<AbpNewtonsoftJsonFormatter>();
formatterOptions.TextOutputFormatters.Add<AbpNewtonsoftJsonFormatter>();
});
}
else
{
context.Services.AddMvcCore().AddNewtonsoftJson();
}
options.SerializerSettings.ContractResolver = serviceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>();
var converters = serviceProvider.GetRequiredService<IOptions<AbpNewtonsoftJsonSerializerOptions>>().Value
.Converters.Select(converterType => serviceProvider.GetRequiredService(converterType).As<JsonConverter>())
.ToList();
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcNewtonsoftJsonOptions>, AbpMvcNewtonsoftJsonOptionsSetup>());
options.SerializerSettings.Converters.InsertRange(0, converters);
});
}
}

11
framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftOptions.cs

@ -1,11 +0,0 @@
namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson;
public class AbpAspNetCoreMvcNewtonsoftOptions
{
public bool UseHybridSerializer { get; set; }
public AbpAspNetCoreMvcNewtonsoftOptions()
{
UseHybridSerializer = true;
}
}

31
framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs

@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Volo.Abp.Json.Newtonsoft;
namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson;
public class AbpMvcNewtonsoftJsonOptionsSetup : IConfigureOptions<MvcNewtonsoftJsonOptions>
{
private readonly IServiceProvider _serviceProvider;
public AbpMvcNewtonsoftJsonOptionsSetup(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void Configure(MvcNewtonsoftJsonOptions options)
{
options.SerializerSettings.ContractResolver = _serviceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>();
var converters = _serviceProvider.GetRequiredService<IOptions<AbpNewtonsoftJsonSerializerOptions>>().Value
.Converters.Select(converterType => _serviceProvider.GetRequiredService(converterType).As<JsonConverter>())
.ToList();
options.SerializerSettings.Converters.InsertRange(0, converters);
}
}

33
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs

@ -1,33 +0,0 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.Json.SystemTextJson.JsonConverters;
namespace Volo.Abp.AspNetCore.Mvc.Json;
public class AbpJsonOptionsSetup : IConfigureOptions<JsonOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpJsonOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(JsonOptions options)
{
options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
options.JsonSerializerOptions.AllowTrailingCommas = true;
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(ServiceProvider.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
}
}

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

@ -1,8 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.Json.SystemTextJson.JsonConverters;
namespace Volo.Abp.AspNetCore.Mvc.Json;
@ -10,8 +13,6 @@ public static class MvcCoreBuilderExtensions
{
public static IMvcCoreBuilder AddAbpHybridJson(this IMvcCoreBuilder builder)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<JsonOptions>, AbpJsonOptionsSetup>());
builder.Services.Configure<MvcOptions>(options =>
{
options.InputFormatters.RemoveType<SystemTextJsonInputFormatter>();
@ -27,6 +28,20 @@ public static class MvcCoreBuilderExtensions
options.TextOutputFormatters.Add<AbpSystemTextJsonFormatter>();
});
builder.Services.AddOptions<JsonOptions>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
options.JsonSerializerOptions.AllowTrailingCommas = true;
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider
.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
});
return builder;
}
}

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

@ -1,4 +1,5 @@
using Volo.Abp.Modularity;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.Timing;
namespace Volo.Abp.Json.Newtonsoft;
@ -12,5 +13,11 @@ public class AbpJsonNewtonsoftModule : AbpModule
{
options.Providers.Add<AbpNewtonsoftJsonSerializerProvider>();
});
context.Services.AddOptions<AbpNewtonsoftJsonSerializerOptions>()
.Configure<AbpCamelCasePropertyNamesContractResolver>((options, contractResolver) =>
{
options.JsonSerializerSettings.ContractResolver = contractResolver;
});
}
}

5
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerOptions.cs

@ -1,14 +1,13 @@
using Newtonsoft.Json;
using Volo.Abp.Collections;
namespace Volo.Abp.Json.Newtonsoft;
public class AbpNewtonsoftJsonSerializerOptions
{
public ITypeList<JsonConverter> Converters { get; }
public JsonSerializerSettings JsonSerializerSettings { get; }
public AbpNewtonsoftJsonSerializerOptions()
{
Converters = new TypeList<JsonConverter>();
JsonSerializerSettings = new JsonSerializerSettings();
}
}

59
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs

@ -1,8 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Volo.Abp.DependencyInjection;
@ -10,18 +8,11 @@ namespace Volo.Abp.Json.Newtonsoft;
public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency
{
protected IServiceProvider ServiceProvider{ get; }
protected List<JsonConverter> Converters { get; }
protected IOptions<AbpNewtonsoftJsonSerializerOptions> Options { get; }
public AbpNewtonsoftJsonSerializerProvider(
IServiceProvider serviceProvider,
IOptions<AbpNewtonsoftJsonSerializerOptions> options)
public AbpNewtonsoftJsonSerializerProvider(IOptions<AbpNewtonsoftJsonSerializerOptions> options)
{
ServiceProvider = serviceProvider;
Converters = options.Value
.Converters
.Select(c => (JsonConverter)serviceProvider.GetRequiredService(c))
.ToList();
Options = options;
}
public bool CanHandle(Type type)
@ -46,6 +37,7 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra
private readonly static ConcurrentDictionary<object, JsonSerializerSettings> JsonSerializerOptionsCache = new ConcurrentDictionary<object, JsonSerializerSettings>();
protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
{
return JsonSerializerOptionsCache.GetOrAdd(new
@ -54,14 +46,45 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra
indented
}, _ =>
{
var settings = new JsonSerializerSettings();
var settings = new JsonSerializerSettings {
Binder = Options.Value.JsonSerializerSettings.Binder, CheckAdditionalContent = Options.Value.JsonSerializerSettings.CheckAdditionalContent,
Context = Options.Value.JsonSerializerSettings.Context,
ContractResolver = Options.Value.JsonSerializerSettings.ContractResolver,
ConstructorHandling = Options.Value.JsonSerializerSettings.ConstructorHandling,
Converters = Options.Value.JsonSerializerSettings.Converters,
Culture = Options.Value.JsonSerializerSettings.Culture,
DateFormatHandling = Options.Value.JsonSerializerSettings.DateFormatHandling,
DateFormatString = Options.Value.JsonSerializerSettings.DateFormatString,
DateParseHandling = Options.Value.JsonSerializerSettings.DateParseHandling,
DateTimeZoneHandling = Options.Value.JsonSerializerSettings.DateTimeZoneHandling,
DefaultValueHandling = Options.Value.JsonSerializerSettings.DefaultValueHandling,
Error = Options.Value.JsonSerializerSettings.Error,
EqualityComparer = Options.Value.JsonSerializerSettings.EqualityComparer,
FloatFormatHandling = Options.Value.JsonSerializerSettings.FloatFormatHandling,
FloatParseHandling = Options.Value.JsonSerializerSettings.FloatParseHandling,
Formatting = Options.Value.JsonSerializerSettings.Formatting,
MaxDepth = Options.Value.JsonSerializerSettings.MaxDepth,
MetadataPropertyHandling = Options.Value.JsonSerializerSettings.MetadataPropertyHandling,
MissingMemberHandling = Options.Value.JsonSerializerSettings.MissingMemberHandling,
NullValueHandling = Options.Value.JsonSerializerSettings.NullValueHandling,
ObjectCreationHandling = Options.Value.JsonSerializerSettings.ObjectCreationHandling,
PreserveReferencesHandling = Options.Value.JsonSerializerSettings.PreserveReferencesHandling,
ReferenceLoopHandling = Options.Value.JsonSerializerSettings.ReferenceLoopHandling,
ReferenceResolver = Options.Value.JsonSerializerSettings.ReferenceResolver,
ReferenceResolverProvider = Options.Value.JsonSerializerSettings.ReferenceResolverProvider,
SerializationBinder = Options.Value.JsonSerializerSettings.SerializationBinder,
StringEscapeHandling = Options.Value.JsonSerializerSettings.StringEscapeHandling,
TraceWriter = Options.Value.JsonSerializerSettings.TraceWriter,
TypeNameAssemblyFormat = Options.Value.JsonSerializerSettings.TypeNameAssemblyFormat,
TypeNameHandling = Options.Value.JsonSerializerSettings.TypeNameHandling,
TypeNameAssemblyFormatHandling = Options.Value.JsonSerializerSettings.TypeNameAssemblyFormatHandling
};
settings.Converters.InsertRange(0, Converters);
if (camelCase)
{
settings.ContractResolver = ServiceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>();
}
// if (camelCase)
// {
// settings.ContractResolver = ServiceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>();
// }
if (indented)
{

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

@ -1,6 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Text.Encodings.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.SystemTextJson.JsonConverters;
using Volo.Abp.Json.SystemTextJson.Modifiers;
using Volo.Abp.Modularity;
using Volo.Abp.Timing;
@ -11,12 +14,29 @@ public class AbpJsonSystemTextJsonModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AbpSystemTextJsonSerializerOptions>, AbpSystemTextJsonSerializerOptionsSetup>());
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AbpSystemTextJsonSerializerModifiersOptions>, AbpSystemTextJsonSerializerModifiersOptionsSetup>());
Configure<AbpJsonOptions>(options =>
{
options.Providers.Add<AbpSystemTextJsonSerializerProvider>();
});
context.Services.AddOptions<AbpSystemTextJsonSerializerOptions>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
// 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.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(serviceProvider
.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
});
context.Services.AddOptions<AbpSystemTextJsonSerializerModifiersOptions>()
.Configure<IServiceProvider>((options, serviceProvider) =>
{
options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(serviceProvider));
});
}
}

20
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs

@ -1,20 +0,0 @@
using System;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.SystemTextJson.Modifiers;
namespace Volo.Abp.Json.SystemTextJson;
public class AbpSystemTextJsonSerializerModifiersOptionsSetup : IConfigureOptions<AbpSystemTextJsonSerializerModifiersOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpSystemTextJsonSerializerModifiersOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(AbpSystemTextJsonSerializerModifiersOptions options)
{
options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(ServiceProvider));
}
}

31
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs

@ -1,31 +0,0 @@
using System;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.SystemTextJson.JsonConverters;
namespace Volo.Abp.Json.SystemTextJson;
public class AbpSystemTextJsonSerializerOptionsSetup : IConfigureOptions<AbpSystemTextJsonSerializerOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpSystemTextJsonSerializerOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(AbpSystemTextJsonSerializerOptions options)
{
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
// 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.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(ServiceProvider.GetRequiredService<IOptions<AbpSystemTextJsonSerializerModifiersOptions>>());
}
}
Loading…
Cancel
Save