Browse Source

Revised AbpJsonOptions.

pull/1511/head
Halil İbrahim Kalkan 7 years ago
parent
commit
08fd42e1d4
  1. 9
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonOptions.cs
  2. 6
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs
  3. 7
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs
  4. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Json/JsonResultController_Tests.cs
  5. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Json/JsonSerializer_Tests.cs

9
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonOptions.cs

@ -1,9 +1,10 @@
using Newtonsoft.Json; namespace Volo.Abp.Json
namespace Volo.Abp.Json
{ {
public class AbpJsonOptions public class AbpJsonOptions
{ {
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings(); /// <summary>
/// Used to set default value for the DateTimeFormat.
/// </summary>
public string DefaultDateTimeFormat { get; set; }
} }
} }

6
framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs

@ -14,7 +14,11 @@ namespace Volo.Abp.Json.Newtonsoft
public AbpJsonIsoDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> abpJsonOptions) public AbpJsonIsoDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> abpJsonOptions)
{ {
_clock = clock; _clock = clock;
DateTimeFormat = abpJsonOptions.Value.SerializerSettings.DateFormatString;
if (abpJsonOptions.Value.DefaultDateTimeFormat != null)
{
DateTimeFormat = abpJsonOptions.Value.DefaultDateTimeFormat;
}
} }
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)

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

@ -1,5 +1,4 @@
using System; using System;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -9,12 +8,10 @@ namespace Volo.Abp.Json.Newtonsoft
public class NewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency public class NewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
{ {
private readonly AbpJsonIsoDateTimeConverter _dateTimeConverter; private readonly AbpJsonIsoDateTimeConverter _dateTimeConverter;
private readonly AbpJsonOptions _abpJsonOptions;
public NewtonsoftJsonSerializer(AbpJsonIsoDateTimeConverter dateTimeConverter, IOptions<AbpJsonOptions> abpJsonOptions) public NewtonsoftJsonSerializer(AbpJsonIsoDateTimeConverter dateTimeConverter)
{ {
_dateTimeConverter = dateTimeConverter; _dateTimeConverter = dateTimeConverter;
_abpJsonOptions = abpJsonOptions.Value;
} }
public string Serialize(object obj, bool camelCase = true, bool indented = false) public string Serialize(object obj, bool camelCase = true, bool indented = false)
@ -34,7 +31,7 @@ namespace Volo.Abp.Json.Newtonsoft
protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false) protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false)
{ {
var settings = _abpJsonOptions.SerializerSettings; var settings = new JsonSerializerSettings();
settings.Converters.Insert(0, _dateTimeConverter); settings.Converters.Insert(0, _dateTimeConverter);

2
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Json/JsonResultController_Tests.cs

@ -13,7 +13,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
{ {
services.Configure<AbpJsonOptions>(options => services.Configure<AbpJsonOptions>(options =>
{ {
options.SerializerSettings.DateFormatString = "yyyy*MM*dd"; options.DefaultDateTimeFormat = "yyyy*MM*dd";
}); });
base.ConfigureServices(context, services); base.ConfigureServices(context, services);

2
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Json/JsonSerializer_Tests.cs

@ -20,7 +20,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
{ {
services.Configure<AbpJsonOptions>(options => services.Configure<AbpJsonOptions>(options =>
{ {
options.SerializerSettings.DateFormatString = "yyyy*MM*dd"; options.DefaultDateTimeFormat = "yyyy*MM*dd";
}); });
base.ConfigureServices(context, services); base.ConfigureServices(context, services);

Loading…
Cancel
Save