From ad5d09b8f5480c4b91aab98ec6e21a92a6e5d208 Mon Sep 17 00:00:00 2001 From: Flor Guilini Date: Mon, 19 Dec 2022 17:21:38 +0100 Subject: [PATCH] fix quote bug --- JsonDiffPatch/JsonDiffer.cs | 19 ++++++++----------- JsonDiffPatchTests/DiffTests.cs | 5 +++++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/JsonDiffPatch/JsonDiffer.cs b/JsonDiffPatch/JsonDiffer.cs index 3bd531a..98baa26 100644 --- a/JsonDiffPatch/JsonDiffer.cs +++ b/JsonDiffPatch/JsonDiffer.cs @@ -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) @@ -81,8 +78,8 @@ internal static IEnumerable CalculatePatch(JToken left, JToken right, } else if (left.Type == JTokenType.Object) { - var lprops = ((IDictionary) left).OrderBy(p => p.Key); - var rprops = ((IDictionary) right).OrderBy(p => p.Key); + var lprops = ((IDictionary)left).OrderBy(p => p.Key); + var rprops = ((IDictionary)right).OrderBy(p => p.Key); foreach (var removed in lprops.Except(rprops, MatchesKey.Instance)) { @@ -95,7 +92,7 @@ internal static IEnumerable 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) { diff --git a/JsonDiffPatchTests/DiffTests.cs b/JsonDiffPatchTests/DiffTests.cs index e7b1ad1..57a0eac 100644 --- a/JsonDiffPatchTests/DiffTests.cs +++ b/JsonDiffPatchTests/DiffTests.cs @@ -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);