Vib*_*bit 9 .net c# json system.text.json .net-6.0
我System.Text.Json.Nodes在 .NET 6.0 中使用,我想做的很简单:从一个 JsonNode 复制一个 JsonNode 并将该节点附加到另一个 JsonNode。
以下是我的代码。
public static string concQuest(string input, string allQuest, string questId) {
JsonNode inputNode = JsonNode.Parse(input)!;
JsonNode allQuestNode = JsonNode.Parse(allQuest)!;
JsonNode quest = allQuestNode.AsArray().First(quest =>
quest!["id"]!.GetValue<string>() == questId) ?? throw new KeyNotFoundException("No matching questId found.");
inputNode["quest"] = quest; // Exception occured
return inputNode.ToJsonString(options);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行它时,我得到了一个System.InvalidOperationException说法"The node already has a parent."
我尝试过编辑
inputNode["quest"] = quest;
Run Code Online (Sandbox Code Playgroud)
到
inputNode["quest"] = quest.Root; // quest.Root is also a JsonNode
Run Code Online (Sandbox Code Playgroud)
然后代码运行良好,但它返回所有节点,而不是我指定的节点,这不是我想要的结果。另外,由于代码工作正常,我认为直接将 JsonNode 设置为另一个 JsonNode 是可行的。
根据异常消息,似乎如果我想将 JsonNode 添加到另一个 JsonNode,我必须首先将其从其父级中取消附加,但我该怎么做呢?
请注意,我的 JSON 文件非常大(超过 6MB),因此我想确保我的解决方案不存在性能问题。
在 .NET 8 之前,JsonNode没有Clone()方法,因此复制它的最简单方法可能是调用序列化器的JsonSerializer.Deserialize<TValue>(JsonNode, JsonSerializerOptions)扩展方法以将节点直接反序列化到另一个节点。首先引入以下扩展方法来复制或移动节点:
public static partial class JsonExtensions
{
public static TNode? CopyNode<TNode>(this TNode? node) where TNode : JsonNode => node?.Deserialize<TNode>();
public static JsonNode? MoveNode(this JsonArray array, int id, JsonObject newParent, string name)
{
var node = array[id];
array.RemoveAt(id);
return newParent[name] = node;
}
public static JsonNode? MoveNode(this JsonObject parent, string oldName, JsonObject newParent, string name)
{
parent.Remove(oldName, out var node);
return newParent[name] = node;
}
public static TNode ThrowOnNull<TNode>(this TNode? value) where TNode : JsonNode => value ?? throw new JsonException("Null JSON value");
}
Run Code Online (Sandbox Code Playgroud)
现在你的代码可以写成如下:
public static string concQuest(string input, string allQuest, string questId)
{
var inputObject = JsonNode.Parse(input).ThrowOnNull().AsObject();
var allQuestArray = JsonNode.Parse(allQuest).ThrowOnNull().AsArray();
concQuest(inputObject, allQuestArray, questId);
return inputObject.ToJsonString();
}
public static JsonNode? concQuest(JsonObject inputObject, JsonArray allQuestArray, string questId)
{
// Enumerable.First() will throw an InvalidOperationException if no element is found satisfying the predicate.
var node = allQuestArray.First(quest => quest!["id"]!.GetValue<string>() == questId);
return inputObject["quest"] = node.CopyNode();
}
Run Code Online (Sandbox Code Playgroud)
.NET 8 更新: .NET 8 引入了JsonNode.DeepClone()所以在 .NET 8 及更高版本中JsonExtensions.CopyNode()可能会被淘汰,第二种concQuest()方法编写如下:
public static JsonNode? concQuest(JsonObject inputObject, JsonArray allQuestArray, string questId)
{
// Enumerable.First() will throw an InvalidOperationException if no element is found satisfying the predicate.
var node = allQuestArray.First(quest => quest!["id"]!.GetValue<string>() == questId);
return inputObject["quest"] = node?.DeepClone();
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不打算保留任务数组,您可以将节点从数组移动到目标,如下所示:
public static string concQuest(string input, string allQuest, string questId)
{
var inputObject = JsonNode.Parse(input).ThrowOnNull().AsObject();
var allQuestArray = JsonNode.Parse(allQuest).ThrowOnNull().AsArray();
concQuest(inputObject, allQuestArray, questId);
return inputObject.ToJsonString();
}
public static JsonNode? concQuest(JsonObject inputObject, JsonArray allQuestArray, string questId)
{
// Enumerable.First() will throw an InvalidOperationException if no element is found satisfying the predicate.
var (_, index) = allQuestArray.Select((quest, index) => (quest, index)).First(p => p.quest!["id"]!.GetValue<string>() == questId);
return allQuestArray.MoveNode(index, inputObject, "quest");
}
Run Code Online (Sandbox Code Playgroud)
另外,你写了
由于我的 json 文件很大(超过 6MB),我担心可能会出现一些性能问题。
在这种情况下,我会避免将 JSON 文件加载到input和allQuest字符串中,因为大于 85,000 字节的字符串会出现在大型对象堆上,这可能会导致后续性能下降。相反,直接从相关文件反序列化为JsonNode数组和对象,如下所示:
var questId = "2"; // Or whatever
JsonArray allQuest;
using (var stream = new FileStream(allQuestFileName, new FileStreamOptions { Mode = FileMode.Open, Access = FileAccess.Read }))
allQuest = JsonNode.Parse(stream).ThrowOnNull().AsArray();
JsonObject input;
using (var stream = new FileStream(inputFileName, new FileStreamOptions { Mode = FileMode.Open, Access = FileAccess.Read }))
input = JsonNode.Parse(stream).ThrowOnNull().AsObject();
JsonExtensions.concQuest(input, allQuest, questId);
using (var stream = new FileStream(inputFileName, new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write }))
using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }))
input.WriteTo(writer);
Run Code Online (Sandbox Code Playgroud)
或者,如果您的应用程序是异步的,您可以执行以下操作:
JsonArray allQuest;
await using (var stream = new FileStream(allQuestFileName, new FileStreamOptions { Mode = FileMode.Open, Access = FileAccess.Read, Options = FileOptions.Asynchronous }))
allQuest = (await JsonSerializer.DeserializeAsync<JsonArray>(stream)).ThrowOnNull();
JsonObject input;
await using (var stream = new FileStream(inputFileName, new FileStreamOptions { Mode = FileMode.Open, Access = FileAccess.Read, Options = FileOptions.Asynchronous }))
input = (await JsonSerializer.DeserializeAsync<JsonObject>(stream)).ThrowOnNull();
JsonExtensions.concQuest(input, allQuest, questId);
await using (var stream = new FileStream(inputFileName, new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Options = FileOptions.Asynchronous }))
await JsonSerializer.SerializeAsync(stream, input, new JsonSerializerOptions { WriteIndented = true });
Run Code Online (Sandbox Code Playgroud)
笔记:
演示小提琴:
有关在 .NET 6 中复制节点的信息,请参阅https://dotnetfiddle.net/LX3AqH
有关在 .NET 8 中复制节点的信息,请参阅https://dotnetfiddle.net/B84Ba1。
要移动节点,请参阅https://dotnetfiddle.net/cI8DuB。
对于异步读写,请参阅https://dotnetfiddle.net/VjKstQ
| 归档时间: |
|
| 查看次数: |
6401 次 |
| 最近记录: |