Merge pull request #58 from vooba/ObjectHash

Implementation of Object Hash
master
William Bishop 2023-01-16 16:27:49 -06:00 committed by GitHub
commit 483f897751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 588 additions and 446 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

@ -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

@ -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

@ -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; }
}
}