Compare commits

...

14 Commits

Author SHA1 Message Date
William Bishop c2b2458823 Update JsonDeltaFormatterUnitTests.cs 2023-01-16 17:02:08 -06:00
William Bishop 88937bdf8e
Merge pull request #43 from adam-mccoy/master
Fix obscure bug with array sorting
2023-01-16 16:29:11 -06:00
William Bishop cf4373e441
Merge branch 'master' into master 2023-01-16 16:29:05 -06:00
William Bishop 483f897751
Merge pull request #58 from vooba/ObjectHash
Implementation of Object Hash
2023-01-16 16:27:49 -06:00
William Bishop baaad880cc
Merge pull request #61 from lingbohome/master
fix: escape the key with ~ or/in its the json Pointer name
2023-01-16 16:27:35 -06:00
William Bishop 0d6bf19c9f
Merge pull request #64 from wbish/dependabot/nuget/Src/JsonDiffPatchDotNet/Newtonsoft.Json-13.0.2
Bump Newtonsoft.Json from 11.0.1 to 13.0.2 in /Src/JsonDiffPatchDotNet
2023-01-16 16:26:52 -06:00
dependabot[bot] f71f76e1de
Bump Newtonsoft.Json from 11.0.1 to 13.0.2 in /Src/JsonDiffPatchDotNet
Bumps [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json) from 11.0.1 to 13.0.2.
- [Release notes](https://github.com/JamesNK/Newtonsoft.Json/releases)
- [Commits](https://github.com/JamesNK/Newtonsoft.Json/compare/11.0.1...13.0.2)

---
updated-dependencies:
- dependency-name: Newtonsoft.Json
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-12-07 21:25:04 +00:00
lingbo 9c557daf91 fix: escape the key with ~ or/in its the json Pointer name 2022-02-01 21:45:02 +08:00
Mathias Kolb dd7ace5338 feat: implement object hash
based on pull request #52
fixes #54, #42
2021-09-08 12:30:08 +02:00
Mathias Kolb 8f91145d61 fix: lcs backtrack function
function know search the matrix from bottom right to top left
2021-09-08 12:23:36 +02:00
Mathias Kolb c98bb3f378 Merge remote-tracking branch 'khiemnd777/master'
Conflicts:
              Src/JsonDiffPatchDotNet/JsonDiffPatch.cs
	Src/JsonDiffPatchDotNet/Options.cs
2021-07-20 08:48:11 +02:00
Adam McCoy 078ccf1ce2 Use stable sort 2020-11-29 00:46:13 +11:00
Adam McCoy 8cd59b6532 Add test to cover issue with unstable array sort 2020-11-29 00:43:18 +11:00
khiemnd777 5e2808445a [add] item match serves object hash 2018-10-05 11:49:32 +07:00
10 changed files with 663 additions and 454 deletions

View File

@ -1,302 +1,332 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
namespace JsonDiffPatchDotNet.UnitTests
{
[TestFixture]
public class DiffUnitTests
{
[Test]
public void Diff_EmptyObjects_EmptyPatch()
{
var jdp = new JsonDiffPatch();
var empty = JObject.Parse(@"{}");
JToken result = jdp.Diff(empty, empty);
Assert.IsNull(result);
}
[Test]
public void Diff_EqualBooleanProperty_NoDiff()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{""p"": true }");
var right = JObject.Parse(@"{""p"": true }");
JToken result = jdp.Diff(left, right);
Assert.IsNull(result);
}
[Test]
public void Diff_DiffBooleanProperty_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{""p"": true }");
var right = JObject.Parse(@"{""p"": false }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(2, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Old Value");
Assert.IsFalse(((JArray)obj.Property("p").Value)[1].ToObject<bool>(), "Array New Value");
}
[Test]
public void Diff_BooleanPropertyDeleted_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{ ""p"": true }");
var right = JObject.Parse(@"{ }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(3, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Old Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[1].ToObject<int>(), "Array New Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[2].ToObject<int>(), "Array Deleted Indicator");
}
[Test]
public void Diff_BooleanPropertyAdded_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{ }");
var right = JObject.Parse(@"{ ""p"": true }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(1, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Added Value");
}
[Test]
public void Diff_EfficientStringDiff_ValidPatch()
{
var jdp = new JsonDiffPatch(new Options { TextDiff = TextDiffMode.Efficient });
var left = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
var right = JObject.Parse(@"{ ""p"": ""blah1"" }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(3, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.AreEqual("@@ -1,64 +1,5 @@\n-lp.Value.ToString().Length %3e _options.MinEfficientTextDiffLength\n+blah1\n", ((JArray)obj.Property("p").Value)[0].ToString(), "Array Added Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[1].ToObject<int>(), "Array Added Value");
Assert.AreEqual(2, ((JArray)obj.Property("p").Value)[2].ToObject<int>(), "Array String Diff Indicator");
}
[Test]
public void Diff_EfficientStringDiff_NoChanges()
{
var jdp = new JsonDiffPatch(new Options { TextDiff = TextDiffMode.Efficient });
var left = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
var right = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
JToken result = jdp.Diff(left, right);
Assert.IsNull(result, "No Changes");
}
[Test]
public void Diff_LeftNull_Exception()
{
var jdp = new JsonDiffPatch();
var obj = JObject.Parse(@"{ }");
JToken result = jdp.Diff(null, obj);
Assert.AreEqual(JTokenType.Array, result.Type);
}
[Test]
public void Diff_RightNull_Exception()
{
var jdp = new JsonDiffPatch();
var obj = JObject.Parse(@"{ }");
JToken result = jdp.Diff(obj, null);
Assert.AreEqual(JTokenType.Array, result.Type);
}
[Test]
public void Diff_EfficientArrayDiffSame_NullDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var array = JToken.Parse(@"[1,2,3]");
JToken diff = jdp.Diff(array, array);
Assert.IsNull(diff);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadRemoved_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[2,3,4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["_0"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentTailRemoved_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[1,2,3]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["_3"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[0,1,2,3,4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["0"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentTailAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[1,2,3,4,5]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["4"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadTailAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[0,1,2,3,4,5]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(3, diff.Properties().Count());
Assert.IsNotNull(diff["0"]);
Assert.IsNotNull(diff["5"]);
}
[Test]
public void Diff_EfficientArrayDiffSameLengthNested_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,{""p"":false},4]");
var right = JToken.Parse(@"[1,2,{""p"":true},4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["2"]);
}
[Test]
public void Diff_EfficientArrayDiffSameWithObject_NoDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"
{
""@context"": [
""http://www.w3.org/ns/csvw"",
{
""@language"": ""en"",
""@base"": ""http://example.org""
}
]
}");
var right = left.DeepClone();
JToken diff = jdp.Diff(left, right);
Assert.IsNull(diff);
}
[Test]
public void Diff_EfficientArrayDiffHugeArrays_NoStackOverflow()
{
const int arraySize = 1000;
Func<int, int, JToken> hugeArrayFunc = (startIndex, count) =>
{
var builder = new StringBuilder("[");
foreach (var i in Enumerable.Range(startIndex, count))
{
builder.Append($"{i},");
}
builder.Append("]");
return JToken.Parse(builder.ToString());
};
var jdp = new JsonDiffPatch();
var left = hugeArrayFunc(0, arraySize);
var right = hugeArrayFunc(arraySize / 2, arraySize);
JToken diff = jdp.Diff(left, right);
var restored = jdp.Patch(left, diff);
Assert.That(JToken.DeepEquals(restored, right));
}
[Test]
public void Diff_IntStringDiff_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JToken.Parse(@"1");
var right = JToken.Parse(@"""hello""");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Array, result.Type);
JArray array = (JArray)result;
Assert.AreEqual(2, array.Count);
Assert.AreEqual(left, array[0]);
Assert.AreEqual(right, array[1]);
}
}
}
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
namespace JsonDiffPatchDotNet.UnitTests
{
[TestFixture]
public class DiffUnitTests
{
[Test]
public void Diff_EmptyObjects_EmptyPatch()
{
var jdp = new JsonDiffPatch();
var empty = JObject.Parse(@"{}");
JToken result = jdp.Diff(empty, empty);
Assert.IsNull(result);
}
[Test]
public void Diff_EqualBooleanProperty_NoDiff()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{""p"": true }");
var right = JObject.Parse(@"{""p"": true }");
JToken result = jdp.Diff(left, right);
Assert.IsNull(result);
}
[Test]
public void Diff_DiffBooleanProperty_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{""p"": true }");
var right = JObject.Parse(@"{""p"": false }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(2, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Old Value");
Assert.IsFalse(((JArray)obj.Property("p").Value)[1].ToObject<bool>(), "Array New Value");
}
[Test]
public void Diff_BooleanPropertyDeleted_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{ ""p"": true }");
var right = JObject.Parse(@"{ }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(3, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Old Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[1].ToObject<int>(), "Array New Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[2].ToObject<int>(), "Array Deleted Indicator");
}
[Test]
public void Diff_BooleanPropertyAdded_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JObject.Parse(@"{ }");
var right = JObject.Parse(@"{ ""p"": true }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(1, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.IsTrue(((JArray)obj.Property("p").Value)[0].ToObject<bool>(), "Array Added Value");
}
[Test]
public void Diff_EfficientStringDiff_ValidPatch()
{
var jdp = new JsonDiffPatch(new Options { TextDiff = TextDiffMode.Efficient });
var left = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
var right = JObject.Parse(@"{ ""p"": ""blah1"" }");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Object, result.Type);
JObject obj = (JObject)result;
Assert.IsNotNull(obj.Property("p"), "Property Name");
Assert.AreEqual(JTokenType.Array, obj.Property("p").Value.Type, "Array Value");
Assert.AreEqual(3, ((JArray)obj.Property("p").Value).Count, "Array Length");
Assert.AreEqual("@@ -1,64 +1,5 @@\n-lp.Value.ToString().Length %3e _options.MinEfficientTextDiffLength\n+blah1\n", ((JArray)obj.Property("p").Value)[0].ToString(), "Array Added Value");
Assert.AreEqual(0, ((JArray)obj.Property("p").Value)[1].ToObject<int>(), "Array Added Value");
Assert.AreEqual(2, ((JArray)obj.Property("p").Value)[2].ToObject<int>(), "Array String Diff Indicator");
}
[Test]
public void Diff_EfficientStringDiff_NoChanges()
{
var jdp = new JsonDiffPatch(new Options { TextDiff = TextDiffMode.Efficient });
var left = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
var right = JObject.Parse(@"{ ""p"": ""lp.Value.ToString().Length > _options.MinEfficientTextDiffLength"" }");
JToken result = jdp.Diff(left, right);
Assert.IsNull(result, "No Changes");
}
[Test]
public void Diff_LeftNull_Exception()
{
var jdp = new JsonDiffPatch();
var obj = JObject.Parse(@"{ }");
JToken result = jdp.Diff(null, obj);
Assert.AreEqual(JTokenType.Array, result.Type);
}
[Test]
public void Diff_RightNull_Exception()
{
var jdp = new JsonDiffPatch();
var obj = JObject.Parse(@"{ }");
JToken result = jdp.Diff(obj, null);
Assert.AreEqual(JTokenType.Array, result.Type);
}
[Test]
public void Diff_EfficientArrayDiffSame_NullDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var array = JToken.Parse(@"[1,2,3]");
JToken diff = jdp.Diff(array, array);
Assert.IsNull(diff);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadRemoved_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[2,3,4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["_0"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentTailRemoved_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[1,2,3]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["_3"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[0,1,2,3,4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["0"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentTailAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[1,2,3,4,5]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["4"]);
}
[Test]
public void Diff_EfficientArrayDiffDifferentHeadTailAdded_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[1,2,3,4]");
var right = JToken.Parse(@"[0,1,2,3,4,5]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(3, diff.Properties().Count());
Assert.IsNotNull(diff["0"]);
Assert.IsNotNull(diff["5"]);
}
[Test]
public void Diff_EfficientArrayDiffSameLengthNested_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient, ObjectHash = (jObj) => jObj["Id"].Value<string>() });
var left = JToken.Parse(@"[1,2,{""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC8"", ""p"":false},4]");
var right = JToken.Parse(@"[1,2,{""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC8"", ""p"":true},4]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(2, diff.Properties().Count());
Assert.IsNotNull(diff["2"]);
}
[Test]
public void Diff_EfficientArrayDiffWithComplexObject_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient, ObjectHash = (jObj) => jObj["Id"].Value<string>() });
//var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"[{""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC8"", ""p"":false}, {""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC9"", ""p"":true}]");
var right = JToken.Parse(@"[{""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC8"", ""p"":true}, {""Id"" : ""F12B21EF-F57D-4958-ADDC-A3F52EC25EC10"", ""p"":false}]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(4, diff.Properties().Count());
}
[Test]
public void Diff_EfficientArrayDiffWithComplexObjectHash_ValidDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient, ObjectHash = (jObj) => jObj["Id"].Value<string>() });
var left = JToken.Parse(@"[{""Id"": ""22ff56c7-2307-414b-8a3a-9bf1cdba2095"",""city"":""São Paulo""},{""Id"":""3fca9cdb-dd9b-4b7c-afc1-587751e25bd6"",""city"":""abc""},{""Id"":""1fe6a0f9-3974-427f-81cb-6004748cb179"",""city"":""xyz""}]");
var right = JToken.Parse(@"[{""Id"":""3fca9cdb-dd9b-4b7c-afc1-587751e25bd6"",""city"":""abc""},{""Id"":""1fe6a0f9-3974-427f-81cb-6004748cb179"",""city"":""xyz""}, {""Id"":""3fca9cdb-dd9b-4b7c-afc1-587751e25b44"",""city"":""new""}]");
JObject diff = jdp.Diff(left, right) as JObject;
Assert.IsNotNull(diff);
Assert.AreEqual(3, diff.Properties().Count());
Assert.IsNotNull(diff["_0"]);
Assert.IsNotNull(diff["2"]);
Assert.AreEqual(((JArray)diff["2"])[0].Value<string>("city"), "new");
}
[Test]
public void Diff_EfficientArrayDiffSameWithObject_NoDiff()
{
var jdp = new JsonDiffPatch(new Options { ArrayDiff = ArrayDiffMode.Efficient });
var left = JToken.Parse(@"
{
""@context"": [
""http://www.w3.org/ns/csvw"",
{
""@language"": ""en"",
""@base"": ""http://example.org""
}
]
}");
var right = left.DeepClone();
JToken diff = jdp.Diff(left, right);
Assert.IsNull(diff);
}
[Test]
public void Diff_EfficientArrayDiffHugeArrays_NoStackOverflow()
{
const int arraySize = 1000;
Func<int, int, JToken> hugeArrayFunc = (startIndex, count) =>
{
var builder = new StringBuilder("[");
foreach (var i in Enumerable.Range(startIndex, count))
{
builder.Append($"{i},");
}
builder.Append("]");
return JToken.Parse(builder.ToString());
};
var jdp = new JsonDiffPatch();
var left = hugeArrayFunc(0, arraySize);
var right = hugeArrayFunc(arraySize / 2, arraySize);
JToken diff = jdp.Diff(left, right);
var restored = jdp.Patch(left, diff);
Assert.That(JToken.DeepEquals(restored, right));
}
[Test]
public void Diff_IntStringDiff_ValidPatch()
{
var jdp = new JsonDiffPatch();
var left = JToken.Parse(@"1");
var right = JToken.Parse(@"""hello""");
JToken result = jdp.Diff(left, right);
Assert.AreEqual(JTokenType.Array, result.Type);
JArray array = (JArray)result;
Assert.AreEqual(2, array.Count);
Assert.AreEqual(left, array[0]);
Assert.AreEqual(right, array[1]);
}
}
}

View File

@ -1,6 +1,8 @@
using System.Linq;
using JsonDiffPatchDotNet.Formatters.JsonPatch;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using System.Linq;
namespace JsonDiffPatchDotNet.UnitTests
{
@ -121,6 +123,64 @@ namespace JsonDiffPatchDotNet.UnitTests
AssertOperation(operations[1], OperationTypes.Remove, "/1");
}
[Test]
public void Format_SortsRemoveOperations_Success()
{
const string patchJson = @"
{
""a"": {
""a"": [0,0,0],
""b"": [0,0,0],
""c"": {
""a"": {
""a"": [0,0,0],
""b"": [0,0,0],
""c"": [0,0,0],
""d"": [0,0,0],
""e"": [0,0,0],
""f"": [0,0,0]
}
}
},
""b"": [0,0,0],
""c"": [0,0,0],
""d"": [0,0,0],
""e"": [0,0,0],
""f"": [0,0,0],
""g"": [0,0,0],
""h"": [0,0,0],
""i"": {
""a"": {
""a"": {
""_t"": ""a"",
""_0"": [0,0,0],
""_1"": [0,0,0]
}
}
}
}
";
var patch = JToken.Parse(patchJson);
var operations = Formatter.Format(patch);
var paths = operations.Select(o => o.Path).ToList();
// removal of the array item at index 1 should come before the item at index 0
Assert.Less(paths.IndexOf("/i/a/a/1"), paths.IndexOf("/i/a/a/0"));
}
public void Format_EscapeOfJsonPointer_Success()
{
var left = JObject.Parse(@"{ ""a/b"": ""a"", ""a~b"": ""ab"", ""a/~b"": ""abb"",""a/b/c~"": ""abc"" }");
var right = JObject.Parse(@"{ ""a/b"": ""ab"", ""a~b"": ""ba"", ""a/~b"": ""bba"",""a/b/c~"": ""cba"" }");
var patch = Differ.Diff(left, right);
var operations = Formatter.Format(patch);
Assert.IsTrue(operations.Any(x => x.Path.Equals("/a~1b") && x.Value.ToString().Equals("ab")));
Assert.IsTrue(operations.Any(x => x.Path.Equals("/a~0b") && x.Value.ToString().Equals("ba")));
Assert.IsTrue(operations.Any(x => x.Path.Equals("/a~1~0b") && x.Value.ToString().Equals("bba")));
Assert.IsTrue(operations.Any(x => x.Path.Equals("/a~1b~1c~0") && x.Value.ToString().Equals("cba")));
}
private void AssertOperation(Operation operation, string expectedOp, string expectedPath, JValue expectedValue = null)
{
Assert.AreEqual(expectedOp, operation.Op);

View File

@ -16,6 +16,6 @@
/// the entire left and entire right arrays are added to the patch document as a simple
/// JSON token replace. If they are the same, then token is skipped in the patch document.
/// </summary>
Simple,
Simple
}
}

View File

@ -0,0 +1,17 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonDiffPatchDotNet
{
public class DefaultItemMatch : ItemMatch
{
public DefaultItemMatch(Func<JToken, object> objectHash):base(objectHash)
{
}
}
}

View File

@ -51,7 +51,7 @@ namespace JsonDiffPatchDotNet.Formatters.JsonPatch
protected override void NodeBegin(JsonFormatContext context, string key, string leftKey, DeltaType type, NodeType nodeType, bool isLast)
{
context.Path.Add(leftKey);
context.Path.Add(Escape(leftKey));
}
protected override void NodeEnd(JsonFormatContext context, string key, string leftKey, DeltaType type, NodeType nodeType, bool isLast)
@ -64,6 +64,13 @@ namespace JsonDiffPatchDotNet.Formatters.JsonPatch
protected override void RootEnd(JsonFormatContext context, DeltaType type, NodeType nodeType) { }
private string Escape(string key)
{
if (string.IsNullOrEmpty(key)) return key;
return key.Replace("~", "~0")
.Replace("/", "~1");
}
private void FormatNode(JsonFormatContext context, JToken delta, JToken left)
{
FormatDeltaChildren(context, delta, left);
@ -94,8 +101,8 @@ namespace JsonDiffPatchDotNet.Formatters.JsonPatch
var removeOpsOtherOps = PartitionRemoveOps(result);
var removeOps = removeOpsOtherOps[0];
var otherOps = removeOpsOtherOps[1];
Array.Sort(removeOps, new RemoveOperationComparer());
return removeOps.Concat(otherOps).ToList();
var removeOpsReverse = removeOps.OrderBy(x => x.Path, new PathComparer());
return removeOpsReverse.Concat(otherOps).ToList();
}
private IList<Operation[]> PartitionRemoveOps(IList<Operation> result)
@ -109,15 +116,15 @@ namespace JsonDiffPatchDotNet.Formatters.JsonPatch
return new List<Operation[]> {left.ToArray(), right.ToArray()};
}
private class RemoveOperationComparer : IComparer<Operation>
private class PathComparer : IComparer<string>
{
public int Compare(Operation a, Operation b)
public int Compare(string a, string b)
{
if (a == null) throw new ArgumentNullException(nameof(a));
if (b == null) throw new ArgumentNullException(nameof(b));
var splitA = a.Path.Split('/');
var splitB = b.Path.Split('/');
var splitA = a.Split('/');
var splitB = b.Split('/');
return splitA.Length != splitB.Length
? splitA.Length - splitB.Length

View File

@ -0,0 +1,49 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonDiffPatchDotNet
{
public abstract class ItemMatch
{
internal Func<JToken, object> ObjectHash;
protected ItemMatch()
{
}
protected ItemMatch(Func<JToken, object> objectHash)
{
ObjectHash = objectHash;
}
public virtual bool Match(JToken object1, JToken object2)
{
return Match(object1, object2, ObjectHash);
}
public virtual bool Match(JToken object1, JToken object2, Func<JToken, object> objectHash)
{
if(objectHash == null || object1.Type != JTokenType.Object)
{
return JToken.DeepEquals(object1, object2);
}
var hash1 = objectHash.Invoke(object1);
if(hash1 == null)
{
return false;
}
var hash2 = objectHash.Invoke(object2);
if(hash2 == null)
{
return false;
}
return hash1.Equals(hash2);
}
}
}

View File

@ -38,6 +38,11 @@ namespace JsonDiffPatchDotNet
/// <returns>JSON Patch Document</returns>
public JToken Diff(JToken left, JToken right)
{
var objectHash = this._options.ObjectHash;
var itemMatch = new DefaultItemMatch(objectHash);
if (left == null)
left = new JValue("");
if (right == null)
@ -67,7 +72,7 @@ namespace JsonDiffPatchDotNet
: null;
}
if (!JToken.DeepEquals(left, right))
if (!itemMatch.Match(left, right))
{
return new JArray(left, right);
}
@ -371,91 +376,116 @@ namespace JsonDiffPatchDotNet
}
private JObject ArrayDiff(JArray left, JArray right)
{
var result = JObject.Parse(@"{ ""_t"": ""a"" }");
{
var objectHash = this._options.ObjectHash;
var itemMatch = new DefaultItemMatch(objectHash);
var result = JObject.Parse(@"{ ""_t"": ""a"" }");
int commonHead = 0;
int commonTail = 0;
int commonHead = 0;
int commonTail = 0;
if (JToken.DeepEquals(left, right))
return null;
// Find common head
while (commonHead < left.Count
&& commonHead < right.Count
&& JToken.DeepEquals(left[commonHead], right[commonHead]))
{
commonHead++;
}
var childContext = new List<JToken>();
// Find common tail
while (commonTail + commonHead < left.Count
&& commonTail + commonHead < right.Count
&& JToken.DeepEquals(left[left.Count - 1 - commonTail], right[right.Count - 1 - commonTail]))
{
commonTail++;
}
// Find common head
while (commonHead < left.Count
&& commonHead < right.Count
&& itemMatch.Match(left[commonHead], right[commonHead]))
{
var index = commonHead;
var child = Diff(left[index], right[index]);
if(child != null)
{
result[$"{index}"] = child;
}
commonHead++;
}
if (commonHead + commonTail == left.Count)
{
// Trivial case, a block (1 or more consecutive items) was added
for (int index = commonHead; index < right.Count - commonTail; ++index)
{
result[$"{index}"] = new JArray(right[index]);
}
// Find common tail
while (commonTail + commonHead < left.Count
&& commonTail + commonHead < right.Count
&& itemMatch.Match(left[left.Count - 1 - commonTail], right[right.Count - 1 - commonTail]))
{
var index1 = left.Count - 1 - commonTail;
var index2 = right.Count - 1 - commonTail;
var child = Diff(left[index1], right[index2]);
if(child != null)
{
result[$"{index2}"] = child;
}
commonTail++;
}
return result;
}
if (commonHead + commonTail == right.Count)
{
// Trivial case, a block (1 or more consecutive items) was removed
for (int index = commonHead; index < left.Count - commonTail; ++index)
{
result[$"_{index}"] = new JArray(left[index], 0, (int)DiffOperation.Deleted);
}
if (commonHead + commonTail == left.Count)
{
// Trivial case, a block (1 or more consecutive items) was added
for (int index = commonHead; index < right.Count - commonTail; ++index)
{
result[$"{index}"] = new JArray(right[index]);
}
return result;
}
return result;
}
if (commonHead + commonTail == right.Count)
{
// Trivial case, a block (1 or more consecutive items) was removed
for (int index = commonHead; index < left.Count - commonTail; ++index)
{
if (result.ContainsKey(index.ToString()))
{
result.Remove(index.ToString());
}
result[$"_{index}"] = new JArray(left[index], 0, (int)DiffOperation.Deleted);
}
// Complex Diff, find the LCS (Longest Common Subsequence)
List<JToken> trimmedLeft = left.ToList().GetRange(commonHead, left.Count - commonTail - commonHead);
List<JToken> trimmedRight = right.ToList().GetRange(commonHead, right.Count - commonTail - commonHead);
Lcs lcs = Lcs.Get(trimmedLeft, trimmedRight);
return result;
}
for (int index = commonHead; index < left.Count - commonTail; ++index)
{
if (lcs.Indices1.IndexOf(index - commonHead) < 0)
{
// Removed
result[$"_{index}"] = new JArray(left[index], 0, (int)DiffOperation.Deleted);
}
}
// Complex Diff, find the LCS (Longest Common Subsequence)
List<JToken> trimmedLeft = left.ToList().GetRange(commonHead, left.Count - commonTail - commonHead);
List<JToken> trimmedRight = right.ToList().GetRange(commonHead, right.Count - commonTail - commonHead);
Lcs lcs = Lcs.Get(trimmedLeft, trimmedRight, itemMatch);
for (int index = commonHead; index < right.Count - commonTail; index++)
{
int indexRight = lcs.Indices2.IndexOf(index - commonHead);
for (int index = commonHead; index < left.Count - commonTail; ++index)
{
if (lcs.Indices1.IndexOf(index - commonHead) < 0)
{
// Removed
if (result.ContainsKey(index.ToString()))
{
result.Remove(index.ToString());
}
result[$"_{index}"] = new JArray(left[index], 0, (int)DiffOperation.Deleted);
}
}
if (indexRight < 0)
{
// Added
result[$"{index}"] = new JArray(right[index]);
}
else
{
int li = lcs.Indices1[indexRight] + commonHead;
int ri = lcs.Indices2[indexRight] + commonHead;
for (int index = commonHead; index < right.Count - commonTail; index++)
{
int indexRight = lcs.Indices2.IndexOf(index - commonHead);
JToken diff = Diff(left[li], right[ri]);
if (indexRight < 0)
{
// Added
result[$"{index}"] = new JArray(right[index]);
}
else
{
int li = lcs.Indices1[indexRight] + commonHead;
int ri = lcs.Indices2[indexRight] + commonHead;
if (diff != null)
{
result[$"{index}"] = diff;
}
}
}
JToken diff = Diff(left[li], right[ri]);
return result;
}
if (diff != null)
{
result[$"{index}"] = diff;
}
}
}
return result;
}
private JObject ObjectPatch(JObject obj, JObject patch)
{

View File

@ -25,6 +25,6 @@
<Version>2.3.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

View File

@ -1,84 +1,87 @@
using System;
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace JsonDiffPatchDotNet
{
internal class Lcs
{
internal List<JToken> Sequence { get; set; }
internal class Lcs
{
internal List<JToken> Sequence { get; set; }
internal List<int> Indices1 { get; set; }
internal List<int> Indices1 { get; set; }
internal List<int> Indices2 { get; set; }
internal List<int> Indices2 { get; set; }
private Lcs()
{
Sequence = new List<JToken>();
Indices1 = new List<int>();
Indices2 = new List<int>();
}
internal static Lcs Get(List<JToken> left, List<JToken> right)
{
var matrix = LcsInternal(left, right);
var result = Backtrack(matrix, left, right, left.Count, right.Count);
return result;
}
private static int[,] LcsInternal(List<JToken> left, List<JToken> right)
{
var arr = new int[left.Count + 1, right.Count + 1];
for (int i = 1; i <= left.Count; i++)
{
for (int j = 1; j <= right.Count; j++)
{
if (JToken.DeepEquals(left[i - 1], right[j - 1]))
{
arr[i, j] = arr[i - 1, j - 1] + 1;
}
else
{
arr[i, j] = Math.Max(arr[i - 1, j], arr[i, j - 1]);
}
}
}
return arr;
}
private static Lcs Backtrack(int[,] matrix, List<JToken> left, List<JToken> right, int li, int ri)
private Lcs()
{
var result = new Lcs();
for (int i = 1, j = 1; i <= li && j <= ri;)
{
// If the JSON tokens at the same position are both Objects or both Arrays, we just say they
// are the same even if they are not, because we can package smaller deltas than an entire
// object or array replacement by doing object to object or array to array diff.
if (JToken.DeepEquals(left[i - 1], right[j - 1])
|| left[i - 1].Type == JTokenType.Object && right[j - 1].Type == JTokenType.Object
|| left[i - 1].Type == JTokenType.Array && right[j - 1].Type == JTokenType.Array)
{
result.Sequence.Add(left[i - 1]);
result.Indices1.Add(i - 1);
result.Indices2.Add(j - 1);
i++;
j++;
continue;
}
Sequence = new List<JToken>();
Indices1 = new List<int>();
Indices2 = new List<int>();
}
if (matrix[i, j - 1] > matrix[i - 1, j])
internal static Lcs Get(List<JToken> left, List<JToken> right, ItemMatch match)
{
var matrix = LcsInternal(left, right, match);
var result = Backtrack(matrix, left, right, left.Count, right.Count, match);
return result;
}
private static int[,] LcsInternal(List<JToken> left, List<JToken> right, ItemMatch match)
{
var arr = new int[left.Count + 1, right.Count + 1];
for (int i = 1; i <= left.Count; i++)
{
for (int j = 1; j <= right.Count; j++)
{
i++;
if (match.Match(left[i - 1], right[j - 1]))
{
arr[i, j] = arr[i - 1, j - 1] + 1;
}
else
{
arr[i, j] = Math.Max(arr[i - 1, j], arr[i, j - 1]);
}
}
}
return arr;
}
private static Lcs Backtrack(int[,] matrix, List<JToken> left, List<JToken> right, int li, int ri, ItemMatch match)
{
var index1 = li;
var index2 = ri;
var result = new Lcs();
while (index1 != 0 && index2 != 0)
{
var sameLetter = match.Match(left[index1 - 1], right[index2 - 1]);
if (sameLetter)
{
result.Sequence.Add(left[index1 - 1]);
result.Indices1.Add(index1 - 1);
result.Indices2.Add(index2 - 1);
--index1;
--index2;
}
else
{
j++;
var valueAtMatrixAbove = matrix[index1, index2 - 1];
var valueAtMatrixLeft = matrix[index1 - 1, index2];
if (valueAtMatrixAbove > valueAtMatrixLeft)
{
--index2;
}
else
{
--index1;
}
}
}
return result;
}
}
}
}

View File

@ -1,3 +1,4 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
@ -18,16 +19,16 @@ namespace JsonDiffPatchDotNet
/// </summary>
public ArrayDiffMode ArrayDiff { get; set; }
/// <summary>
/// Specifies how string values are diffed. The default is Efficient.
/// </summary>
public TextDiffMode TextDiff { get; set; }
/// <summary>
/// Specifies how string values are diffed. The default is Efficient.
/// </summary>
public TextDiffMode TextDiff { get; set; }
/// <summary>
/// The minimum string length required to use Efficient text diff. If the minimum
/// length is not met, simple text diff will be used. The default length is 50 characters.
/// </summary>
public long MinEfficientTextDiffLength { get; set; }
/// <summary>
/// The minimum string length required to use Efficient text diff. If the minimum
/// length is not met, simple text diff will be used. The default length is 50 characters.
/// </summary>
public long MinEfficientTextDiffLength { get; set; }
/// <summary>
/// Specifies which paths to exclude from the diff patch set
@ -38,5 +39,17 @@ namespace JsonDiffPatchDotNet
/// Specifies behaviors to apply to the diff patch set
/// </summary>
public DiffBehavior DiffBehaviors { get; set; }
}
/// <summary>
/// for LCS to work, it needs a way to match items between previous/original (or left/right) arrays. In traditional text diff tools this is trivial, as two lines of text are compared char
/// char.
/// When no matches by reference or value are found, array diffing fallbacks to a dumb behavior: matching items by position.
/// Matching by position is not the most efficient option (eg. if an item is added at the first position, all the items below will be considered modified), but it produces expected results
/// in most trivial cases.This is good enough as soon as movements/insertions/deletions only happen near the bottom of the array.
/// This is because if 2 objects are not equal by reference(ie.the same object) both objects are considered different values, as there is no trivial solution to compare two arbitrary objects
/// in JavaScript.
/// To improve the results leveraging the power of LCS(and position move detection) you need to provide a way to compare 2 objects.
/// </summary>
public Func<JToken, object> ObjectHash { get; set; }
}
}