mcintyre321 / JsonDiffPatch

Apache License 2.0
50 stars 25 forks source link

Differ fails when path contains a ' #22

Open Fl0GUI opened 1 year ago

Fl0GUI commented 1 year ago

An example: from:

{
    "isn't": true
}

This is valid json. to:

{
    "isn't": false
}

This is also valid json, but running the Differ gives:

> new JsonDiffer().Differ(from, to, false)
   Newtonsoft.Json.JsonReaderException : After parsing a value an unexpected character was encountered: t. Path 'path', line 1, position 33.
     at Newtonsoft.Json.JsonTextReader.ParsePostValue(Boolean ignoreComments)
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Linq.JContainer.ReadContentFrom(JsonReader r, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom(JsonReader reader, JsonLoadSettings options)
   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json)
   at JsonDiffPatch.Operation.Parse(String json)
   at JsonDiffPatch.JsonDiffer.Build(String op, String path, String key, JToken value)
   at JsonDiffPatch.JsonDiffer.CalculatePatch(JToken left, JToken right, Boolean useIdToDetermineEquality, String path)+MoveNext()
   at JsonDiffPatch.JsonDiffer.CalculatePatch(JToken left, JToken right, Boolean useIdToDetermineEquality, String path)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at JsonDiffPatch.JsonDiffer.Diff(JToken from, JToken to, Boolean useIdPropertyToDetermineEquality)

I am pretty certain the error happens in https://github.com/mcintyre321/JsonDiffPatch/blob/dd56de0475dbb3a5b136c518c3792b6efab426bf/JsonDiffPatch/JsonDiffer.cs#L26-L27 or https://github.com/mcintyre321/JsonDiffPatch/blob/dd56de0475dbb3a5b136c518c3792b6efab426bf/JsonDiffPatch/JsonDiffer.cs#L30-L31. This code creates { 'op' : 'replace' , path: '/isn't', value: true} which isn't valid json at all. The only reason it doesn't fail in other cases is that JObject.Parse is generous.