Skip to content

Commit

Permalink
Support deserializing string to numeric values (#16060)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored May 14, 2024
1 parent 7c64d96 commit 87334ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<CustomListValueOption>(json, JOptions.Base);

Assert.Equal(expectedWeight, item.Weight);
}

[Fact]
public void Serialize_WhenCalled_ReturnValidJson()
{
Expand Down Expand Up @@ -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; }
}
}

0 comments on commit 87334ea

Please sign in to comment.