-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from gregsdennis/patch/copying-nodes
Patch/copying nodes
- Loading branch information
Showing
8 changed files
with
179 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Json.More; | ||
using Json.Pointer; | ||
using NUnit.Framework; | ||
|
||
namespace Json.Patch.Tests; | ||
|
||
public class GithubTests | ||
{ | ||
[Test] | ||
public void Issue393_PatchDoesNothing() | ||
{ | ||
const string mask = "*****"; | ||
var maskJson = JsonValue.Create(mask); | ||
|
||
var pathsToPatch = new[] { "/first_name", "/last_name" }; | ||
|
||
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson)); | ||
var patchConfig = new JsonPatch(patchOperations); | ||
|
||
Console.WriteLine(JsonSerializer.Serialize(patchConfig)); | ||
|
||
const string singleObjectJson = "{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}"; | ||
|
||
var singleObject = JsonDocument.Parse(singleObjectJson).RootElement; | ||
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result; | ||
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject)); | ||
|
||
const string arrayObjectJson = "[" + | ||
"{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}," + | ||
"{\"_id\":\"640729d45b5824ffcabc30a5\"," + | ||
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," + | ||
"\"first_name\":\"Rivers\"," + | ||
"\"last_name\":\"Smith\"" + | ||
"}" + | ||
"]"; | ||
|
||
var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement; | ||
|
||
// Way 1: patch whole array | ||
var patchedArray = patchConfig.Apply(arrayObject.AsNode()).Result; // <- does nothing | ||
|
||
Console.WriteLine(JsonSerializer.Serialize(patchedArray)); | ||
} | ||
[Test] | ||
public void Issue393_NodeAlreadyHasParent_2() | ||
{ | ||
const string mask = "*****"; | ||
var maskJson = JsonValue.Create(mask); | ||
|
||
var pathsToPatch = new[] { "/first_name", "/last_name" }; | ||
|
||
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson)); | ||
var patchConfig = new JsonPatch(patchOperations); | ||
|
||
const string singleObjectJson = "{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}"; | ||
|
||
var singleObject = JsonDocument.Parse(singleObjectJson).RootElement; | ||
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result; | ||
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject)); | ||
|
||
const string arrayObjectJson = "[" + | ||
"{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}," + | ||
"{\"_id\":\"640729d45b5824ffcabc30a5\"," + | ||
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," + | ||
"\"first_name\":\"Rivers\"," + | ||
"\"last_name\":\"Smith\"" + | ||
"}" + | ||
"]"; | ||
|
||
var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement; | ||
|
||
var jsonArray = arrayObject.AsNode().AsArray(); | ||
|
||
// Way 2: just patch every element | ||
foreach (var element in jsonArray) | ||
{ | ||
var patchedNode = patchConfig.Apply(element).Result; // <- throws an error | ||
Console.WriteLine(JsonSerializer.Serialize(patchedNode)); | ||
} | ||
} | ||
[Test] | ||
public void Issue393_NodeAlreadyHasParent_3() | ||
{ | ||
const string mask = "*****"; | ||
var maskJson = JsonValue.Create(mask); | ||
|
||
var pathsToPatch = new[] { "/first_name", "/last_name" }; | ||
|
||
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson)); | ||
var patchConfig = new JsonPatch(patchOperations); | ||
|
||
const string singleObjectJson = "{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}"; | ||
|
||
var singleObject = JsonNode.Parse(singleObjectJson); | ||
var patchedSingleObject = patchConfig.Apply(singleObject).Result; | ||
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject)); | ||
|
||
const string arrayObjectJson = "[" + | ||
"{" + | ||
"\"_id\":\"640729d45434f90313d25c78\"," + | ||
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," + | ||
"\"first_name\":\"Kathrine\"," + | ||
"\"last_name\":\"Pate\"" + | ||
"}," + | ||
"{\"_id\":\"640729d45b5824ffcabc30a5\"," + | ||
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," + | ||
"\"first_name\":\"Rivers\"," + | ||
"\"last_name\":\"Smith\"" + | ||
"}" + | ||
"]"; | ||
|
||
var arrayObject = JsonNode.Parse(arrayObjectJson); | ||
|
||
var jsonArray = arrayObject.AsArray(); | ||
|
||
// Way 3: remove from initial array and then patch | ||
for (int currentIndex = jsonArray.Count - 1; currentIndex >= 0; currentIndex--) | ||
{ | ||
var nodeToPatch = jsonArray[currentIndex]; | ||
jsonArray.RemoveAt(currentIndex); | ||
|
||
var patchedNode = patchConfig.Apply(nodeToPatch).Result; // <- throws an error | ||
Console.WriteLine(JsonSerializer.Serialize(patchedNode)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule JSON-Schema-Test-Suite
updated
12 files