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

时间:2023-04-25
本文介绍了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 究竟做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:JSON.NET JObject 键比较不区分大小写 下一篇:将 .Net 对象序列化为 json,使用 xml 属性进行控制

相关文章