diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs index b7fe56c2c1e..cde64d5a524 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs @@ -18,7 +18,8 @@ public static class JOptions ReadCommentHandling = JsonCommentHandling.Skip, PropertyNameCaseInsensitive = true, AllowTrailingCommas = true, - WriteIndented = false + WriteIndented = false, + NumberHandling = JsonNumberHandling.AllowReadingFromString, }; public static readonly JsonSerializerOptions Default; diff --git a/test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs b/test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs index 422d44e6d12..202092b0178 100644 --- a/test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs +++ b/test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using OrchardCore.ContentFields.Settings; using OrchardCore.Tests.Apis.Context; using OrchardCore.Users.Core.Json; using OrchardCore.Users.Indexes; @@ -28,6 +29,18 @@ public void Deserialize_WhenCalled_ReturnValidUserLoginInfo() Assert.Equal("default", obj.ProviderDisplayName); } + [Theory] + [InlineData("{\"name\":\"One\",\"value\":\"1\",\"Weight\":\"1.75\"}", 1.75)] + [InlineData("{\"name\":\"One\",\"value\":\"1\",\"Weight\":\"1\"}", 1)] + [InlineData("{\"name\":\"One\",\"value\":\"1\",\"Weight\":1}", 1)] + [InlineData("{\"name\":\"One\",\"value\":\"1\",\"Weight\":2.75}", 2.75)] + public void Deserialize_WhenCalled_ReturnDoubleFromStringWithBaseOptions(string json, double expectedWeight) + { + var item = JsonSerializer.Deserialize(json, JOptions.Base); + + Assert.Equal(expectedWeight, item.Weight); + } + [Fact] public void Serialize_WhenCalled_ReturnValidJson() { @@ -74,4 +87,9 @@ await context.UsingTenantScopeAsync(async scope => Assert.Equal(loginInfo.ProviderDisplayName, userLoginInfo.ProviderDisplayName); }); } + + public sealed class CustomListValueOption : ListValueOption + { + public double? Weight { get; set; } + } }