mirror of https://github.com/abpframework/abp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.2 KiB
74 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using Shouldly;
|
|
using Volo.Abp.Json.SystemTextJson.JsonConverters;
|
|
using Xunit;
|
|
|
|
namespace Volo.Abp.Json
|
|
{
|
|
public class AbpStringToEnum_Tests
|
|
{
|
|
[Fact]
|
|
public void Test_Read()
|
|
{
|
|
var options = new JsonSerializerOptions()
|
|
{
|
|
Converters =
|
|
{
|
|
new AbpStringToEnumFactory()
|
|
}
|
|
};
|
|
|
|
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);
|
|
|
|
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");
|
|
}
|
|
|
|
[Fact]
|
|
public void Test_Write()
|
|
{
|
|
var options = new JsonSerializerOptions()
|
|
{
|
|
Converters =
|
|
{
|
|
new AbpStringToEnumFactory()
|
|
}
|
|
};
|
|
|
|
var testClassJson = JsonSerializer.Serialize(new TestClass()
|
|
{
|
|
Day = DayOfWeek.Monday
|
|
});
|
|
|
|
testClassJson.ShouldBe("{\"Day\":1}");
|
|
|
|
testClassJson = JsonSerializer.Serialize(new Dictionary<DayOfWeek, string>
|
|
{
|
|
{DayOfWeek.Monday, "Mo"}
|
|
}, options);
|
|
|
|
testClassJson.ShouldBe("{\"Monday\":\"Mo\"}");
|
|
}
|
|
|
|
class TestClass
|
|
{
|
|
public DayOfWeek Day { get; set; }
|
|
}
|
|
}
|
|
}
|