Browse Source

Merge pull request #19064 from abpframework/auto-merge/rel-8-1/2511

Merge branch dev with rel-8.1
pull/19119/head
maliming 3 years ago
committed by GitHub
parent
commit
ede3bcf857
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs
  2. 9
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs
  3. 52
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpDateTimeConverter_Tests.cs
  4. 25
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs
  5. 95
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs

9
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs

@ -46,6 +46,15 @@ public class AbpDateTimeConverter : JsonConverter<DateTime>, ITransientDependenc
return _clock.Normalize(d3); return _clock.Normalize(d3);
} }
var dateText = reader.GetString();
if (!dateText.IsNullOrWhiteSpace())
{
if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d4))
{
return _clock.Normalize(d4);
}
}
throw new JsonException("Can't get datetime from the reader!"); throw new JsonException("Can't get datetime from the reader!");
} }

9
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs

@ -46,6 +46,15 @@ public class AbpNullableDateTimeConverter : JsonConverter<DateTime?>, ITransient
return _clock.Normalize(d2); return _clock.Normalize(d2);
} }
var dateText = reader.GetString();
if (!dateText.IsNullOrWhiteSpace())
{
if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d3))
{
return _clock.Normalize(d3);
}
}
return null; return null;
} }

52
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpDateTimeConverter_Tests.cs

@ -0,0 +1,52 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Shouldly;
using Volo.Abp.Json.SystemTextJson.Modifiers;
using Volo.Abp.Localization;
using Xunit;
namespace Volo.Abp.Json;
public class AbpDatetimeToEnum_Tests : AbpJsonSystemTextJsonTestBase
{
[Theory]
[InlineData("tr", "14.02.2024")]
[InlineData("en-US", "2/14/2024")]
[InlineData("en-GB", "14/02/2024")]
public void Test_Read(string culture, string datetime)
{
var options = new JsonSerializerOptions()
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
{
Modifiers = { new AbpDateTimeConverterModifier().CreateModifyAction(ServiceProvider) }
}
};
using(CultureHelper.Use(culture))
{
var testClass = JsonSerializer.Deserialize<TestClass>($"{{\"DateTime\": \"{datetime}\", \"NullableDateTime\": \"{datetime}\"}}", options);
testClass.ShouldNotBeNull();
testClass.DateTime.ToString(CultureInfo.CurrentCulture).ShouldStartWith(datetime);
testClass.NullableDateTime.ShouldNotBeNull();
testClass.NullableDateTime.Value.ToString(CultureInfo.CurrentCulture).ShouldStartWith(datetime);
}
using(CultureHelper.Use(culture))
{
var testClass = JsonSerializer.Deserialize<TestClass>($"{{\"DateTime\": \"{datetime}\", \"NullableDateTime\": null}}", options);
testClass.ShouldNotBeNull();
testClass.DateTime.ToString(CultureInfo.CurrentCulture).ShouldStartWith(datetime);
testClass.NullableDateTime.ShouldBeNull();
}
}
class TestClass
{
public DateTime DateTime { get; set; }
public DateTime? NullableDateTime { get; set; }
}
}

25
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs

@ -3,13 +3,13 @@ using Shouldly;
using Volo.Abp.Json.SystemTextJson.JsonConverters; using Volo.Abp.Json.SystemTextJson.JsonConverters;
using Xunit; using Xunit;
namespace Volo.Abp.Json namespace Volo.Abp.Json;
public class AbpStringToBoolean_Tests
{ {
public class AbpStringToBoolean_Tests [Fact]
public void Test_Read()
{ {
[Fact]
public void Test_Read()
{
var options = new JsonSerializerOptions() var options = new JsonSerializerOptions()
{ {
Converters = Converters =
@ -27,9 +27,9 @@ namespace Volo.Abp.Json
testClass.Enabled.ShouldBe(true); testClass.Enabled.ShouldBe(true);
} }
[Fact] [Fact]
public void Test_Write() public void Test_Write()
{ {
var options = new JsonSerializerOptions() var options = new JsonSerializerOptions()
{ {
Converters = Converters =
@ -46,9 +46,8 @@ namespace Volo.Abp.Json
testClassJson.ShouldBe("{\"Enabled\":true}"); testClassJson.ShouldBe("{\"Enabled\":true}");
} }
class TestClass class TestClass
{ {
public bool Enabled { get; set; } public bool Enabled { get; set; }
}
} }
} }

95
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs

@ -5,69 +5,68 @@ using Shouldly;
using Volo.Abp.Json.SystemTextJson.JsonConverters; using Volo.Abp.Json.SystemTextJson.JsonConverters;
using Xunit; using Xunit;
namespace Volo.Abp.Json namespace Volo.Abp.Json;
public class AbpStringToEnum_Tests
{ {
public class AbpStringToEnum_Tests [Fact]
public void Test_Read()
{ {
[Fact] var options = new JsonSerializerOptions()
public void Test_Read()
{ {
var options = new JsonSerializerOptions() Converters =
{ {
Converters = new AbpStringToEnumFactory()
{ }
new AbpStringToEnumFactory() };
}
};
var testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": \"Monday\"}", options); var testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": \"Monday\"}", options);
testClass.ShouldNotBeNull(); testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday); testClass.Day.ShouldBe(DayOfWeek.Monday);
testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": 1}", options); testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": 1}", options);
testClass.ShouldNotBeNull(); testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday); testClass.Day.ShouldBe(DayOfWeek.Monday);
var dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"Monday\":\"Mo\"}", options); var dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"Monday\":\"Mo\"}", options);
dictionary.ShouldNotBeNull(); dictionary.ShouldNotBeNull();
dictionary.Keys.ShouldContain(DayOfWeek.Monday); dictionary.Keys.ShouldContain(DayOfWeek.Monday);
dictionary.Values.ShouldContain("Mo"); dictionary.Values.ShouldContain("Mo");
dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"1\":\"Mo\"}", options); dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"1\":\"Mo\"}", options);
dictionary.ShouldNotBeNull(); dictionary.ShouldNotBeNull();
dictionary.Keys.ShouldContain(DayOfWeek.Monday); dictionary.Keys.ShouldContain(DayOfWeek.Monday);
dictionary.Values.ShouldContain("Mo"); dictionary.Values.ShouldContain("Mo");
} }
[Fact] [Fact]
public void Test_Write() public void Test_Write()
{
var options = new JsonSerializerOptions()
{ {
var options = new JsonSerializerOptions() Converters =
{ {
Converters = new AbpStringToEnumFactory()
{ }
new AbpStringToEnumFactory() };
}
};
var testClassJson = JsonSerializer.Serialize(new TestClass() var testClassJson = JsonSerializer.Serialize(new TestClass()
{ {
Day = DayOfWeek.Monday Day = DayOfWeek.Monday
}); });
testClassJson.ShouldBe("{\"Day\":1}"); testClassJson.ShouldBe("{\"Day\":1}");
testClassJson = JsonSerializer.Serialize(new Dictionary<DayOfWeek, string> testClassJson = JsonSerializer.Serialize(new Dictionary<DayOfWeek, string>
{ {
{DayOfWeek.Monday, "Mo"} {DayOfWeek.Monday, "Mo"}
}, options); }, options);
testClassJson.ShouldBe("{\"Monday\":\"Mo\"}"); testClassJson.ShouldBe("{\"Monday\":\"Mo\"}");
} }
class TestClass class TestClass
{ {
public DayOfWeek Day { get; set; } public DayOfWeek Day { get; set; }
}
} }
} }

Loading…
Cancel
Save