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("{\"Day\": \"Monday\"}", 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"); dictionary = JsonSerializer.Deserialize>("{\"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.Monday, "Mo"} }, options); testClassJson.ShouldBe("{\"Monday\":\"Mo\"}"); } class TestClass { public DayOfWeek Day { get; set; } } } }