-Merge demo
This commit is contained in:
@@ -29,6 +29,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
#if !NETFX_CORE
|
||||
@@ -515,5 +516,39 @@ namespace Newtonsoft.Json.Tests
|
||||
// "address": "221B Baker Street"
|
||||
// }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MergeJson()
|
||||
{
|
||||
JObject o1 = JObject.Parse(@"{
|
||||
'FirstName': 'John',
|
||||
'LastName': 'Smith',
|
||||
'Enabled': false,
|
||||
'Roles': [ 'User' ]
|
||||
}");
|
||||
JObject o2 = JObject.Parse(@"{
|
||||
'Enabled': true,
|
||||
'Roles': [ 'User', 'Admin' ]
|
||||
}");
|
||||
|
||||
o1.Merge(o2, new JsonMergeSettings
|
||||
{
|
||||
// union arrays together to avoid duplicates
|
||||
MergeArrayHandling = MergeArrayHandling.Union
|
||||
});
|
||||
|
||||
string json = o1.ToString();
|
||||
// {
|
||||
// "FirstName": "John",
|
||||
// "LastName": "Smith",
|
||||
// "Enabled": true,
|
||||
// "Roles": [
|
||||
// "User",
|
||||
// "Admin"
|
||||
// ]
|
||||
// }
|
||||
|
||||
Console.WriteLine(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -492,6 +492,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
|
||||
|
||||
@@ -118,6 +118,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
JsonObjectContract contract = base.CreateObjectContract(objectType);
|
||||
|
||||
// use Autofac to create types that have been registered with it
|
||||
if (_container.IsRegistered(objectType))
|
||||
contract.DefaultCreator = () => _container.Resolve(objectType);
|
||||
|
||||
|
||||
@@ -111,9 +111,11 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(other, "c");
|
||||
|
||||
int i = 0;
|
||||
foreach (JToken child in other)
|
||||
{
|
||||
Add(child);
|
||||
AddInternal(i, child, false);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user