Skip to content

Differ fails when path contains a ' #22

@Fl0GUI

Description

@Fl0GUI

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

Operation.Parse("{ 'op' : '" + op + "' , path: '" + path + "', value: " +
(value == null ? "null" : value.ToString(Formatting.None)) + "}");
or
Operation.Parse("{ op : '" + op + "' , path : '" + Extend(path, key) + "' , value : " +
(value == null ? "null" : value.ToString(Formatting.None)) + "}");
.
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions