What does ReferenceLoopHandling.Ignore in Newtonsoft.json exactly do?(Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?)
问题描述
谁能给我一个可以使用它的场景.我对 ReferenceLoopHandling.Ignore 的理解是,如果您有一个引用对象 B 和 B 引用 C 和 C 再次引用 A (A->B->C->A) 的对象 A,那么在序列化时,它将在 C 和 A 之间陷入无限循环,可以使用下面的方法来避免.我说的对吗?
Can anyone present me a scenario where it can be used. What I understand by ReferenceLoopHandling.Ignore is if you have an object A which references object B and B references C and C again references A (A->B->C->A), then when serializing, it will end up in endless loop between C and A, which can be avoided using below. Am I right?
JsonConvert.SerializeObject(data,
Formatting.Indented,
new JsonSerializerSetting()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
));
我遇到了通过使用上述方法解决的自引用循环问题,但我想准确了解它在做什么,因为上面的行是应用程序的核心(关键肉)
I am having self referencing loop issue which gets solved by using the above, but I want to understand exactly what it is doing as the above line is the meat of the application (critical meat)
推荐答案
相关文档可在此处获得:http://james.newtonking.com/projects/json/help/html/SerializationSettings.htm
The documentation on this is available here: http://james.newtonking.com/projects/json/help/html/SerializationSettings.htm
在撰写本文时,该行为描述如下(重点是我的):
As of this writing, the behavior is described there as follows (with emphasis mine):
ReferenceLoopHandling.Error:默认情况下,Json.NET 会出错,如果遇到引用循环(否则序列化程序将进入一个无限循环).
ReferenceLoopHandling.Error: By default Json.NET will error if a reference loop is encountered (otherwise the serializer will get into an infinite loop).
ReferenceLoopHandling.Ignore:Json.NET 将忽略引用循环而不是序列化它们.第一次对象是遇到它会像往常一样被序列化,但如果对象是作为自身的子对象遇到的序列化程序将跳过序列化它.
ReferenceLoopHandling.Serialize:此选项强制 Json.NET在引用循环中序列化对象.如果对象是嵌套但不是无限期的.
ReferenceLoopHandling.Serialize: This option forces Json.NET to
serialize objects in reference loops. This is useful if objects are
nested but not indefinitely.
这篇关于Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?
基础教程推荐
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01
