gpt4 book ai didi

c# - Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?

转载 作者:IT老高 更新时间:2023-10-28 12:50:50 29 4
gpt4 key购买 nike

谁能给我一个可以使用它的场景。我对 ReferenceLoopHandling.Ignore 的理解是,如果您有一个引用对象 B 和 B 引用 C 和 C 再次引用 A (A->B->C->A) 的对象 A,那么在序列化时,它将在 C 和 A 之间陷入无限循环,可以使用下面的方法来避免。我说的对吗?

 JsonConvert.SerializeObject(data, 
Formatting.Indented,
new JsonSerializerSetting()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
));

我遇到了通过使用上述方法解决的自引用循环问题,但我想准确了解它在做什么,因为上面的行是应用程序的核心(关键肉)

最佳答案

有关这方面的文档可在此处获得:http://james.newtonking.com/projects/json/help/html/SerializationSettings.htm

在撰写本文时,该行为描述如下(重点是我的):

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 will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it.

ReferenceLoopHandling.Serialize: This option forces Json.NET to serialize objects in reference loops. This is useful if objects are nested but not indefinitely.

关于c# - Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979637/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com