Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions JsonDiffPatch/JsonDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ internal static string Extend(string path, string extension)

private static Operation Build(string op, string path, string key, JToken value)
{
if (string.IsNullOrEmpty(key))
return
Operation.Parse("{ 'op' : '" + op + "' , path: '" + path + "', value: " +
(value == null ? "null" : value.ToString(Formatting.None)) + "}");
else
return
Operation.Parse("{ op : '" + op + "' , path : '" + Extend(path, key) + "' , value : " +
(value == null ? "null" : value.ToString(Formatting.None)) + "}");
var operation = string.Empty;
var fullPath = string.IsNullOrEmpty(key) ? path : Extend(path, key);
var stringValue = value == null ? "null" : value.ToString(Formatting.None);
operation = $"{{ \"op\": \"{op}\", \"path\": \"{fullPath}\", \"value\": {stringValue}}}";
return Operation.Parse(operation);
}

internal static Operation Add(string path, string key, JToken value)
Expand Down Expand Up @@ -81,8 +78,8 @@ internal static IEnumerable<Operation> CalculatePatch(JToken left, JToken right,
}
else if (left.Type == JTokenType.Object)
{
var lprops = ((IDictionary<string, JToken>) left).OrderBy(p => p.Key);
var rprops = ((IDictionary<string, JToken>) right).OrderBy(p => p.Key);
var lprops = ((IDictionary<string, JToken>)left).OrderBy(p => p.Key);
var rprops = ((IDictionary<string, JToken>)right).OrderBy(p => p.Key);

foreach (var removed in lprops.Except(rprops, MatchesKey.Instance))
{
Expand All @@ -95,7 +92,7 @@ internal static IEnumerable<Operation> CalculatePatch(JToken left, JToken right,
}

var matchedKeys = lprops.Select(x => x.Key).Intersect(rprops.Select(y => y.Key));
var zipped = matchedKeys.Select(k => new {key = k, left = left[k], right = right[k]});
var zipped = matchedKeys.Select(k => new { key = k, left = left[k], right = right[k] });

foreach (var match in zipped)
{
Expand Down
5 changes: 5 additions & 0 deletions JsonDiffPatchTests/DiffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public class DiffTests
"{a:[1,2,3,{name:'b'}]}",
ExpectedResult = "[{\"op\":\"replace\",\"path\":\"/a/3/name\",\"value\":\"b\"}]",
TestName = "JsonPatch handles same array containing different objects")]
[TestCase("{\"isn't\": true}",
"{\"isn't\": false}",
ExpectedResult = "[{\"op\":\"replace\",\"path\":\"/isn't\",\"value\":false}]",
TestName = "JsonPatch handles quotation marks in property name"
)]
public string JsonPatchesWorks(string leftString, string rightString)
{
var left = JToken.Parse(leftString);
Expand Down