Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
npfrizze opened this issue Nov 17, 2017 · 0 comments
Open

Comments

@npfrizze
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant