From 3f805ac2e90387850b1fed37498c50672a47e2d6 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 15 Feb 2024 10:55:51 +0800 Subject: [PATCH] Trying to parse `datetime` using `CurrentUICulture`. --- .../JsonConverters/AbpDateTimeConverter.cs | 9 ++ .../AbpNullableDateTimeConverter.cs | 9 ++ .../Abp/Json/AbpDateTimeConverter_Tests.cs | 52 ++++++++++ .../Volo/Abp/Json/AbpStringToBoolean_Tests.cs | 25 +++-- .../Volo/Abp/Json/AbpStringToEnum_Tests.cs | 95 +++++++++---------- 5 files changed, 129 insertions(+), 61 deletions(-) create mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpDateTimeConverter_Tests.cs diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs index 366a933d13a..a8653bb54a9 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs @@ -46,6 +46,15 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso 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!"); } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs index da5983250d1..e8a94892dad 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs @@ -46,6 +46,15 @@ public AbpNullableDateTimeConverter(IClock clock, IOptions abpJs 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; } diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpDateTimeConverter_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpDateTimeConverter_Tests.cs new file mode 100644 index 00000000000..7b1a0d81ce8 --- /dev/null +++ b/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($"{{\"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($"{{\"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; } + } +} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs index c46a616db07..e595875c991 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToBoolean_Tests.cs @@ -3,13 +3,13 @@ using Volo.Abp.Json.SystemTextJson.JsonConverters; 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() { Converters = @@ -27,9 +27,9 @@ public void Test_Read() testClass.Enabled.ShouldBe(true); } - [Fact] - public void Test_Write() - { + [Fact] + public void Test_Write() + { var options = new JsonSerializerOptions() { Converters = @@ -46,9 +46,8 @@ public void Test_Write() testClassJson.ShouldBe("{\"Enabled\":true}"); } - class TestClass - { - public bool Enabled { get; set; } - } + class TestClass + { + public bool Enabled { get; set; } } -} +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs index 8336b4ce7d2..8d00afd1095 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpStringToEnum_Tests.cs @@ -5,69 +5,68 @@ using Volo.Abp.Json.SystemTextJson.JsonConverters; using Xunit; -namespace Volo.Abp.Json +namespace Volo.Abp.Json; + +public class AbpStringToEnum_Tests { - public class AbpStringToEnum_Tests + [Fact] + public void Test_Read() { - [Fact] - public void Test_Read() + var options = new JsonSerializerOptions() { - var options = new JsonSerializerOptions() + Converters = { - Converters = - { - new AbpStringToEnumFactory() - } - }; + new AbpStringToEnumFactory() + } + }; - var testClass = JsonSerializer.Deserialize("{\"Day\": \"Monday\"}", options); - testClass.ShouldNotBeNull(); - testClass.Day.ShouldBe(DayOfWeek.Monday); + var testClass = JsonSerializer.Deserialize("{\"Day\": \"Monday\"}", options); + testClass.ShouldNotBeNull(); + testClass.Day.ShouldBe(DayOfWeek.Monday); - testClass = JsonSerializer.Deserialize("{\"Day\": 1}", options); - testClass.ShouldNotBeNull(); - testClass.Day.ShouldBe(DayOfWeek.Monday); + testClass = JsonSerializer.Deserialize("{\"Day\": 1}", options); + testClass.ShouldNotBeNull(); + testClass.Day.ShouldBe(DayOfWeek.Monday); - var dictionary = JsonSerializer.Deserialize>("{\"Monday\":\"Mo\"}", options); - dictionary.ShouldNotBeNull(); - dictionary.Keys.ShouldContain(DayOfWeek.Monday); - dictionary.Values.ShouldContain("Mo"); + var dictionary = JsonSerializer.Deserialize>("{\"Monday\":\"Mo\"}", options); + dictionary.ShouldNotBeNull(); + dictionary.Keys.ShouldContain(DayOfWeek.Monday); + dictionary.Values.ShouldContain("Mo"); - dictionary = JsonSerializer.Deserialize>("{\"1\":\"Mo\"}", options); - dictionary.ShouldNotBeNull(); - dictionary.Keys.ShouldContain(DayOfWeek.Monday); - dictionary.Values.ShouldContain("Mo"); - } + dictionary = JsonSerializer.Deserialize>("{\"1\":\"Mo\"}", options); + dictionary.ShouldNotBeNull(); + dictionary.Keys.ShouldContain(DayOfWeek.Monday); + dictionary.Values.ShouldContain("Mo"); + } - [Fact] - public void Test_Write() + [Fact] + public void Test_Write() + { + var options = new JsonSerializerOptions() { - var options = new JsonSerializerOptions() + Converters = { - Converters = - { - new AbpStringToEnumFactory() - } - }; + new AbpStringToEnumFactory() + } + }; - var testClassJson = JsonSerializer.Serialize(new TestClass() - { - Day = DayOfWeek.Monday - }); + var testClassJson = JsonSerializer.Serialize(new TestClass() + { + Day = DayOfWeek.Monday + }); - testClassJson.ShouldBe("{\"Day\":1}"); + testClassJson.ShouldBe("{\"Day\":1}"); - testClassJson = JsonSerializer.Serialize(new Dictionary - { - {DayOfWeek.Monday, "Mo"} - }, options); + testClassJson = JsonSerializer.Serialize(new Dictionary + { + {DayOfWeek.Monday, "Mo"} + }, options); - testClassJson.ShouldBe("{\"Monday\":\"Mo\"}"); - } + testClassJson.ShouldBe("{\"Monday\":\"Mo\"}"); + } - class TestClass - { - public DayOfWeek Day { get; set; } - } + class TestClass + { + public DayOfWeek Day { get; set; } } }