New features:

Optional handles the "null" and "undefined" value for JObject.Merge() by setting MergeObjectHandling flag in JsonMergeSettings.
This commit is contained in:
MiNG
2015-04-15 16:34:45 +08:00
parent e99d9d42e4
commit b8400f8fb3
5 changed files with 124 additions and 2 deletions
@@ -569,5 +569,57 @@ 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()},
};
var patch = JObject.Parse("{Property1:null, Property2:null, Property3:null, Property4:null}");
source.Merge(patch, new JsonMergeSettings
{
MergetObjectHandling = MergeObjectHandling.Null,
});
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);
}
[Test]
public void MergeUndefinedlValue()
{
var source = new JObject
{
{"Property1", "value"},
{"Property2", new JObject()},
{"Property3", JValue.CreateNull()},
{"Property4", JValue.CreateUndefined()},
};
var patch = JObject.Parse("{Property1:undefined, Property2:undefined, Property3:undefined, Property4:undefined}");
source.Merge(patch, new JsonMergeSettings
{
MergetObjectHandling = MergeObjectHandling.Undefined,
});
Assert.IsNull(source.Property("Property1"));
Assert.IsNull(source.Property("Property2"));
Assert.IsNull(source.Property("Property3"));
Assert.IsNull(source.Property("Property4"));
}
}
}
+22 -2
View File
@@ -171,11 +171,31 @@ namespace Newtonsoft.Json.Linq
}
else if (contentItem.Value != null)
{
if (settings != null)
{
if (contentItem.Value.Type == JTokenType.Null)
{
if ((settings.MergetObjectHandling & MergeObjectHandling.Null) != 0)
{
existingProperty.Value = contentItem.Value;
}
continue;
}
if (contentItem.Value.Type == JTokenType.Undefined)
{
if ((settings.MergetObjectHandling & MergeObjectHandling.Undefined) != 0)
{
existingProperty.Remove();
}
continue;
}
}
JContainer existingContainer = existingProperty.Value as JContainer;
if (existingContainer == null)
{
if (contentItem.Value.Type != JTokenType.Null)
existingProperty.Value = contentItem.Value;
existingProperty.Value = contentItem.Value;
}
else if (existingContainer.Type != contentItem.Value.Type)
{
@@ -9,6 +9,8 @@ namespace Newtonsoft.Json.Linq
{
private MergeArrayHandling _mergeArrayHandling;
private MergeObjectHandling _mergeObjectHandling;
/// <summary>
/// Gets or sets the method used when merging JSON arrays.
/// </summary>
@@ -24,5 +26,21 @@ namespace Newtonsoft.Json.Linq
_mergeArrayHandling = value;
}
}
/// <summary>
/// Gets or sets the method used when merging JSON objects.
/// </summary>
/// <value>The method used when merging JSON objects.</value>
public MergeObjectHandling MergetObjectHandling
{
get { return _mergeObjectHandling; }
set
{
if ((value | MergeObjectHandling.All) != MergeObjectHandling.All)
throw new ArgumentOutOfRangeException("value");
_mergeObjectHandling = value;
}
}
}
}
@@ -0,0 +1,31 @@
using System;
namespace Newtonsoft.Json.Linq
{
/// <summary>
/// Specifies how JSON objects are merged together.
/// </summary>
[Flags]
public enum MergeObjectHandling
{
/// <summary>
/// Uses default behaviour (ignore null and undefined) to merge the objects.
/// </summary>
None = 0,
/// <summary>
/// The target object's null value will be set to the source object.
/// </summary>
Null = 1,
/// <summary>
/// The target objects's explicit <code>undefined</code> value will remove the source object's property.
/// </summary>
Undefined = 2,
/// <summary>
/// Merge objects uses <see cref="Null"/> and <see cref="Undefined"/> policy.
/// </summary>
All = Null | Undefined,
}
}
@@ -136,6 +136,7 @@
<Compile Include="Linq\JTokenWriter.cs" />
<Compile Include="Linq\JValue.cs" />
<Compile Include="Linq\MergeArrayHandling.cs" />
<Compile Include="Linq\MergeObjectHandling.cs" />
<Compile Include="MemberSerialization.cs" />
<Compile Include="MetadataPropertyHandling.cs" />
<Compile Include="MissingMemberHandling.cs" />