|
|
@ -1,8 +1,6 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
|
|
using Microsoft.Extensions.Options; |
|
|
using Microsoft.Extensions.Options; |
|
|
using Newtonsoft.Json; |
|
|
using Newtonsoft.Json; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
@ -10,18 +8,11 @@ namespace Volo.Abp.Json.Newtonsoft; |
|
|
|
|
|
|
|
|
public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency |
|
|
public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency |
|
|
{ |
|
|
{ |
|
|
protected IServiceProvider ServiceProvider{ get; } |
|
|
protected IOptions<AbpNewtonsoftJsonSerializerOptions> Options { get; } |
|
|
protected List<JsonConverter> Converters { get; } |
|
|
|
|
|
|
|
|
|
|
|
public AbpNewtonsoftJsonSerializerProvider( |
|
|
public AbpNewtonsoftJsonSerializerProvider(IOptions<AbpNewtonsoftJsonSerializerOptions> options) |
|
|
IServiceProvider serviceProvider, |
|
|
|
|
|
IOptions<AbpNewtonsoftJsonSerializerOptions> options) |
|
|
|
|
|
{ |
|
|
{ |
|
|
ServiceProvider = serviceProvider; |
|
|
Options = options; |
|
|
Converters = options.Value |
|
|
|
|
|
.Converters |
|
|
|
|
|
.Select(c => (JsonConverter)serviceProvider.GetRequiredService(c)) |
|
|
|
|
|
.ToList(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool CanHandle(Type type) |
|
|
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>(); |
|
|
private readonly static ConcurrentDictionary<object, JsonSerializerSettings> JsonSerializerOptionsCache = new ConcurrentDictionary<object, JsonSerializerSettings>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) |
|
|
protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) |
|
|
{ |
|
|
{ |
|
|
return JsonSerializerOptionsCache.GetOrAdd(new |
|
|
return JsonSerializerOptionsCache.GetOrAdd(new |
|
|
@ -54,14 +46,45 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra |
|
|
indented |
|
|
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) |
|
|
// if (camelCase)
|
|
|
{ |
|
|
// {
|
|
|
settings.ContractResolver = ServiceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>(); |
|
|
// settings.ContractResolver = ServiceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>();
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
if (indented) |
|
|
if (indented) |
|
|
{ |
|
|
{ |
|
|
|