Skip to content

Commit

Permalink
Trying to parse datetime using CurrentUICulture.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Feb 15, 2024
1 parent 3e753b3 commit 3f805ac
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Check warning on line 56 in framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs

View check run for this annotation

Codecov / codecov/patch

framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs#L56

Added line #L56 was not covered by tests

throw new JsonException("Can't get datetime from the reader!");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public AbpNullableDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> 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);
}
}

Check warning on line 56 in framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs

View check run for this annotation

Codecov / codecov/patch

framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs#L56

Added line #L56 was not covered by tests

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand All @@ -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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestClass>("{\"Day\": \"Monday\"}", options);
testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday);
var testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": \"Monday\"}", options);
testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday);

testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": 1}", options);
testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday);
testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": 1}", options);
testClass.ShouldNotBeNull();
testClass.Day.ShouldBe(DayOfWeek.Monday);

var dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"Monday\":\"Mo\"}", options);
dictionary.ShouldNotBeNull();
dictionary.Keys.ShouldContain(DayOfWeek.Monday);
dictionary.Values.ShouldContain("Mo");
var dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"Monday\":\"Mo\"}", options);
dictionary.ShouldNotBeNull();
dictionary.Keys.ShouldContain(DayOfWeek.Monday);
dictionary.Values.ShouldContain("Mo");

dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"1\":\"Mo\"}", options);
dictionary.ShouldNotBeNull();
dictionary.Keys.ShouldContain(DayOfWeek.Monday);
dictionary.Values.ShouldContain("Mo");
}
dictionary = JsonSerializer.Deserialize<Dictionary<DayOfWeek, string>>("{\"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, string>
{
{DayOfWeek.Monday, "Mo"}
}, options);
testClassJson = JsonSerializer.Serialize(new Dictionary<DayOfWeek, string>
{
{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; }
}
}

0 comments on commit 3f805ac

Please sign in to comment.