Browse Source

Add AbpJsonIsoDateTimeConverter to AbpNewtonsoftJsonSerializerOptions.

pull/5952/head
maliming 6 years ago
parent
commit
e0204e9576
  1. 4
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs
  2. 2
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs
  3. 12
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs
  4. 5
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs

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

@ -23,7 +23,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
return await GetTextInputFormatter(context).ReadRequestBodyAsync(context, encoding); return await GetTextInputFormatter(context).ReadRequestBodyAsync(context, encoding);
} }
private TextInputFormatter GetTextInputFormatter(InputFormatterContext context) protected virtual TextInputFormatter GetTextInputFormatter(InputFormatterContext context)
{ {
var typesMatcher = context.HttpContext.RequestServices.GetRequiredService<SystemTextJsonSupportTypeMatcher>(); var typesMatcher = context.HttpContext.RequestServices.GetRequiredService<SystemTextJsonSupportTypeMatcher>();
if (typesMatcher.Match(context.ModelType)) if (typesMatcher.Match(context.ModelType))
@ -34,6 +34,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
return context.HttpContext.RequestServices.GetRequiredService<NewtonsoftJsonInputFormatter>(); return context.HttpContext.RequestServices.GetRequiredService<NewtonsoftJsonInputFormatter>();
} }
public InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; public virtual InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions;
} }
} }

2
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs

@ -23,7 +23,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
await GetTextInputFormatter(context).WriteResponseBodyAsync(context, selectedEncoding); await GetTextInputFormatter(context).WriteResponseBodyAsync(context, selectedEncoding);
} }
private TextOutputFormatter GetTextInputFormatter(OutputFormatterWriteContext context) protected virtual TextOutputFormatter GetTextInputFormatter(OutputFormatterWriteContext context)
{ {
var typesMatcher = context.HttpContext.RequestServices.GetRequiredService<SystemTextJsonSupportTypeMatcher>(); var typesMatcher = context.HttpContext.RequestServices.GetRequiredService<SystemTextJsonSupportTypeMatcher>();
if (typesMatcher.Match(context.ObjectType)) if (typesMatcher.Match(context.ObjectType))

12
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs

@ -1,5 +1,6 @@
using System; 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; using Volo.Abp.DependencyInjection;
@ -11,11 +12,14 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
{ {
public class AbpMvcJsonContractResolver : DefaultContractResolver, ITransientDependency public class AbpMvcJsonContractResolver : DefaultContractResolver, ITransientDependency
{ {
private readonly AbpJsonIsoDateTimeConverter _dateTimeConverter; private readonly Lazy<AbpJsonIsoDateTimeConverter> _dateTimeConverter;
public AbpMvcJsonContractResolver(AbpJsonIsoDateTimeConverter dateTimeConverter) public AbpMvcJsonContractResolver(IServiceProvider serviceProvider)
{ {
_dateTimeConverter = dateTimeConverter; _dateTimeConverter = new Lazy<AbpJsonIsoDateTimeConverter>(
serviceProvider.GetRequiredService<AbpJsonIsoDateTimeConverter>,
true
);
NamingStrategy = new CamelCaseNamingStrategy(); NamingStrategy = new CamelCaseNamingStrategy();
} }
@ -38,7 +42,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableDateTimeNormalizationAttribute>(member) == null) if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableDateTimeNormalizationAttribute>(member) == null)
{ {
property.Converter = _dateTimeConverter; property.Converter = _dateTimeConverter.Value;
} }
} }
} }

5
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs

@ -27,6 +27,11 @@ namespace Volo.Abp.Json
{ {
options.UnsupportedAttributes.Add<DisableDateTimeNormalizationAttribute>(); options.UnsupportedAttributes.Add<DisableDateTimeNormalizationAttribute>();
}); });
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<AbpJsonIsoDateTimeConverter>();
});
} }
} }
} }

Loading…
Cancel
Save