-Fixed merging null string values

This commit is contained in:
James Newton-King
2017-12-04 18:53:50 +13:00
parent 977884d779
commit d0d01f2a36
2 changed files with 26 additions and 1 deletions
@@ -46,6 +46,16 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture] [TestFixture]
public class MergeTests : TestFixtureBase public class MergeTests : TestFixtureBase
{ {
[Test]
public void MergeNullString()
{
var a = new JObject { ["a"] = 1 };
var b = new JObject { ["a"] = false ? "2" : null };
a.Merge(b);
Assert.AreEqual(1, (int)a["a"]);
}
[Test] [Test]
public void MergeObjectProperty() public void MergeObjectProperty()
{ {
+16 -1
View File
@@ -185,7 +185,7 @@ namespace Newtonsoft.Json.Linq
{ {
if (!(existingProperty.Value is JContainer existingContainer) || existingContainer.Type != contentItem.Value.Type) if (!(existingProperty.Value is JContainer existingContainer) || existingContainer.Type != contentItem.Value.Type)
{ {
if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge) if (!IsNull(contentItem.Value) || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
{ {
existingProperty.Value = contentItem.Value; existingProperty.Value = contentItem.Value;
} }
@@ -198,6 +198,21 @@ namespace Newtonsoft.Json.Linq
} }
} }
private static bool IsNull(JToken token)
{
if (token.Type == JTokenType.Null)
{
return true;
}
if (token is JValue v && v.Value == null)
{
return true;
}
return false;
}
internal void InternalPropertyChanged(JProperty childProperty) internal void InternalPropertyChanged(JProperty childProperty)
{ {
OnPropertyChanged(childProperty.Name); OnPropertyChanged(childProperty.Name);