I have the following environment and version of .NET Core
.NET SDK (reflecting any global.json): Version: 5.0.100 Commit: 5044b93829 Runtime Environment: OS Name: Mac OS X OS Version: 11.0 OS Platform: Darwin RID: osx.11.0-x64 Base Path: /usr/local/share/dotnet/sdk/5.0.100/ Using the following code and endpoint.
public class Trie { public string Id { get; set; } public Dictionary<string, TrieNode> Nodes { get; set; } } public class TrieNode { public string Name { get; set; } } public async Task<IActionResult> Patch(string id, [FromBody] JsonPatchDocument<Trie> patchDoc) { var trie = [get from database] patchDoc.ApplyTo(trie, ModelState); } When I use the following op:
[ { "op": "add", "path": "/nodes/-", "value": { "/": { "name": "hello, world" } } } ] an item is added to the dictionary like this:
"Nodes": { "-": { "Name": null } } If I use the following syntax for the op
[ { "op": "add", "path": "/nodes", "value": { "/": { "name": "hello, world" } } } ] The item is added like this:
"Nodes": { "/": { "Name": "hello, world" } } But when I try to add a new item with another key like this:
[ { "op": "add", "path": "/nodes", "value": { "/my-key": { "name": "hello, world" } } } ] The item is not added, it's replaces the first entry so the result looks like this:
"Nodes": { "/my-key": { "Name": "hello, world" } } How can I add, remove, replace, copy, move entries in a Dictionary<string, TrieNode>?
https://stackoverflow.com/questions/66723977/adding-an-item-to-a-dictionary-using-jsonpatch-in-asp-net-core March 21, 2021 at 12:33AM
没有评论:
发表评论