-Added MergeNullValueHandling
This commit is contained in:
@@ -569,5 +569,36 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual("name1", p.Name);
|
||||
Assert.AreEqual(0, p.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MergeNullValue()
|
||||
{
|
||||
var source = new JObject
|
||||
{
|
||||
{"Property1", "value"},
|
||||
{"Property2", new JObject()},
|
||||
{"Property3", JValue.CreateNull()},
|
||||
{"Property4", JValue.CreateUndefined()},
|
||||
{"Property5", new JArray()}
|
||||
};
|
||||
|
||||
var patch = JObject.Parse("{Property1: null, Property2: null, Property3: null, Property4: null, Property5: null}");
|
||||
|
||||
source.Merge(patch, new JsonMergeSettings
|
||||
{
|
||||
MergeNullValueHandling = MergeNullValueHandling.Merge
|
||||
});
|
||||
|
||||
Assert.IsNotNull(source["Property1"]);
|
||||
Assert.AreEqual(JTokenType.Null, source["Property1"].Type);
|
||||
Assert.IsNotNull(source["Property2"]);
|
||||
Assert.AreEqual(JTokenType.Null, source["Property2"].Type);
|
||||
Assert.IsNotNull(source["Property3"]);
|
||||
Assert.AreEqual(JTokenType.Null, source["Property3"].Type);
|
||||
Assert.IsNotNull(source["Property4"]);
|
||||
Assert.AreEqual(JTokenType.Null, source["Property4"].Type);
|
||||
Assert.IsNotNull(source["Property5"]);
|
||||
Assert.AreEqual(JTokenType.Null, source["Property5"].Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JContainer existingContainer = existingProperty.Value as JContainer;
|
||||
if (existingContainer == null)
|
||||
{
|
||||
if (contentItem.Value.Type != JTokenType.Null)
|
||||
if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
|
||||
{
|
||||
existingProperty.Value = contentItem.Value;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Linq
|
||||
public class JsonMergeSettings
|
||||
{
|
||||
private MergeArrayHandling _mergeArrayHandling;
|
||||
private MergeNullValueHandling _mergeNullValueHandling;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the method used when merging JSON arrays.
|
||||
@@ -26,5 +27,23 @@ namespace Newtonsoft.Json.Linq
|
||||
_mergeArrayHandling = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how how null value properties are merged.
|
||||
/// </summary>
|
||||
/// <value>How null value properties are merged.</value>
|
||||
public MergeNullValueHandling MergeNullValueHandling
|
||||
{
|
||||
get { return _mergeNullValueHandling; }
|
||||
set
|
||||
{
|
||||
if (value < MergeNullValueHandling.Ignore || value > MergeNullValueHandling.Merge)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
}
|
||||
|
||||
_mergeNullValueHandling = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies how null value properties are merged.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum MergeNullValueHandling
|
||||
{
|
||||
/// <summary>
|
||||
/// The content's null value properties will be ignored during merging.
|
||||
/// </summary>
|
||||
Ignore = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The content's null value properties will be merged.
|
||||
/// </summary>
|
||||
Merge = 1
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,7 @@
|
||||
<Compile Include="Linq\JsonPath\QueryFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ScanFilter.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="SerializationBinder.cs" />
|
||||
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
|
||||
|
||||
@@ -139,6 +139,7 @@
|
||||
<Compile Include="Linq\JsonPath\QueryFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ScanFilter.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="SerializationBinder.cs" />
|
||||
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
|
||||
|
||||
@@ -139,6 +139,7 @@
|
||||
<Compile Include="Linq\JsonPath\QueryFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ScanFilter.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
|
||||
<Compile Include="Serialization\ExpressionValueProvider.cs" />
|
||||
|
||||
@@ -136,6 +136,7 @@
|
||||
<Compile Include="Linq\JTokenWriter.cs" />
|
||||
<Compile Include="Linq\JValue.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MemberSerialization.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="MissingMemberHandling.cs" />
|
||||
|
||||
@@ -141,6 +141,7 @@
|
||||
<Compile Include="Linq\JTokenWriter.cs" />
|
||||
<Compile Include="Linq\JValue.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MemberSerialization.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="MissingMemberHandling.cs" />
|
||||
|
||||
@@ -140,6 +140,7 @@
|
||||
<Compile Include="Linq\JValue.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="Linq\MergeNullValueHandling.cs" />
|
||||
<Compile Include="MemberSerialization.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
<Compile Include="MissingMemberHandling.cs" />
|
||||
|
||||
Reference in New Issue
Block a user