Skip to content

Cannot get patch to work - throws: Failed to dereference pointer #5

Description

@npfrizze

I have 2 json files that I Diff to produce a PatchDocument. I want to get a string representation of the difference so I can store it for use later to regenerate a json file using the original json and the patch. Here is the code

        JsonDiffer jsonDiff = new JsonDiffer();
       JsonPatcher patcher = new JsonPatcher();
        try { 
            string modifiedJson = File.ReadAllText($"c:/temp/modified.json");
            string defaultJson = File.ReadAllText($"c:/temp/default.json");
            PatchDocument patch = jsonDiff.Diff(defaultJson, modifiedJson, true);
            JToken defJson = JToken.Parse(defaultJson);
            patcher.Patch(ref defJson, patch);  // works

            Console.WriteLine(defJson);

            // get a string from PatchDocument - so I can save it for  later and then patch original json
            var patchString= patch.ToString(Newtonsoft.Json.Formatting.None);
            Console.WriteLine(JToken.Parse(patchString));
            // take that string and create a PatchDocument so it can be patched with original json
            var patchDoc = PatchDocument.Parse(patchString);
            defJson = JToken.Parse(defaultJson);
            patcher.Patch(ref defJson, patchDoc);  // this throws - Failed to dereference pointer

            // Also tried creating PatchDocument using stream
            System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(patchString));
            patchDoc = PatchDocument.Load(mStream);
            patcher.Patch(ref defJson, patchDoc);  // this throws - Failed to dereference pointer

            // Also tried getting string from PatchDocument using ToStream
            var outputstream = patch.ToStream();
            patchString = new StreamReader(outputstream).ReadToEnd();
            Console.WriteLine("stream" + patchString);
            mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(patchString));
            patchDoc = PatchDocument.Load(mStream);
            patcher.Patch(ref defJson, patchDoc);  // this throws - Failed to dereference pointer

            // take that string and create a PatchDocument so it can be patched with original json
            patchDoc = PatchDocument.Parse(patchString);
            patcher.Patch(ref defJson, patchDoc);  // this throws - Failed to dereference pointer

The problem seems to be when I generate the string from the PatchDocument and/or use that string to build a PatchDocument. What am I doing wrong?

Attached is the json.
modified.txt
default.txt

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