-Fix merging null with complex type and MergeNullValueHandling.Ignore

This commit is contained in:
James Newton-King
2016-12-08 09:47:01 +13:00
parent be8ca7a870
commit 4d425ccd06
2 changed files with 66 additions and 5 deletions
@@ -596,5 +596,70 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsNotNull(source["Property5"]);
Assert.AreEqual(JTokenType.Null, source["Property5"].Type);
}
[Test]
public void MergeNullValueHandling_Array()
{
string originalJson = @"{
""Bar"": [
""a"",
""b"",
""c""
]
}";
string newJson = @"{
""Bar"": null
}";
JObject oldFoo = JObject.Parse(originalJson);
JObject newFoo = JObject.Parse(newJson);
oldFoo.Merge(newFoo, new JsonMergeSettings
{
MergeNullValueHandling = MergeNullValueHandling.Ignore
});
StringAssert.AreEqual(originalJson, oldFoo.ToString());
oldFoo.Merge(newFoo, new JsonMergeSettings
{
MergeNullValueHandling = MergeNullValueHandling.Merge
});
StringAssert.AreEqual(newJson, newFoo.ToString());
}
[Test]
public void MergeNullValueHandling_Object()
{
string originalJson = @"{
""Bar"": {}
}";
string newJson = @"{
""Bar"": null
}";
JObject oldFoo = JObject.Parse(originalJson);
JObject newFoo = JObject.Parse(newJson);
oldFoo.Merge(newFoo, new JsonMergeSettings
{
MergeNullValueHandling = MergeNullValueHandling.Ignore
});
StringAssert.AreEqual(originalJson, oldFoo.ToString());
oldFoo.Merge(newFoo, new JsonMergeSettings
{
MergeNullValueHandling = MergeNullValueHandling.Merge
});
StringAssert.AreEqual(newJson, newFoo.ToString());
}
public class Foo
{
public IEnumerable<string> Bar { get; set; }
}
}
}
+1 -5
View File
@@ -190,17 +190,13 @@ namespace Newtonsoft.Json.Linq
else if (contentItem.Value != null)
{
JContainer existingContainer = existingProperty.Value as JContainer;
if (existingContainer == null)
if (existingContainer == null || existingContainer.Type != contentItem.Value.Type)
{
if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
{
existingProperty.Value = contentItem.Value;
}
}
else if (existingContainer.Type != contentItem.Value.Type)
{
existingProperty.Value = contentItem.Value;
}
else
{
existingContainer.Merge(contentItem.Value, settings);