Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to parse datetime using CurrentUICulture. #19031

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
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 @@
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; }
}
}
Loading