Browse Source

Merge pull request #19063 from abpframework/auto-merge/rel-8-0/2510

Merge branch rel-8.1 with rel-8.0
pull/19064/head
maliming 3 years ago
committed by GitHub
parent
commit
61508298b0
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);
}
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!");
}

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);
}
var dateText = reader.GetString();
if (!dateText.IsNullOrWhiteSpace())
{
if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d3))
{
return _clock.Normalize(d3);
}
}
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 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 @@ namespace Volo.Abp.Json
testClass.Enabled.ShouldBe(true);
}
[Fact]
public void Test_Write()
{
[Fact]
public void Test_Write()
{
var options = new JsonSerializerOptions()
{
Converters =
@ -46,9 +46,8 @@ namespace Volo.Abp.Json
testClassJson.ShouldBe("{\"Enabled\":true}");
}
class TestClass
{
public bool Enabled { get; set; }
}
class TestClass
{
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 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…
Cancel
Save