diff --git a/src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs b/src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs index eb8d32f..e4f55b3 100644 --- a/src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs +++ b/src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs @@ -13,7 +13,7 @@ private static object ToObject(this JsonElement jsonElement) { case JsonValueKind.Null: return null; case JsonValueKind.String: return jsonElement.GetString(); - case JsonValueKind.Number: return jsonElement.GetInt64(); + case JsonValueKind.Number: return jsonElement.GetGenericNumber(); case JsonValueKind.True: return true; case JsonValueKind.False: return false; case JsonValueKind.Undefined: @@ -24,6 +24,17 @@ private static object ToObject(this JsonElement jsonElement) } } + private static object GetGenericNumber (this JsonElement jsonElement) + { + + // Attempt to parse the JSON Element as an Int32 first + if (jsonElement.TryGetInt32(out int int32)) return int32; + + // Failing that, parse it as a Decimal instead + return jsonElement.GetDecimal(); + + } + private static bool IsValue(this JsonValueKind valueKind) => (valueKind == JsonValueKind.False) || (valueKind == JsonValueKind.True) diff --git a/src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs b/src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs index e2cfe71..a0a5b0b 100644 --- a/src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs +++ b/src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs @@ -119,6 +119,25 @@ await p.MergePatchAsync(null, new[] } } + [Theory] + [MemberData(nameof(GetCombinations))] + public async Task NullableDecimalFloatingPoint(bool core, bool newtonsoft) + { + using (var p = new TestHelper(core, newtonsoft)) { + await p.PostAsync("0", p.GetTestModel()); + + await p.MergePatchAsync(null, new[] + { + new { id = 0, NullableDecimal = 7.5 } + }); + + var patchedModel = await p.GetAsync("0"); + var expected = p.GetTestModel(); + expected.NullableDecimal = (decimal?) 7.5; + Assert.Equal(expected, patchedModel); + } + } + [Theory(Skip = "Does not work")] [MemberData(nameof(GetCombinations))] public async Task MissingRequiredProperty(bool core, bool newtonsoft)