简体   繁体   English

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

[英]What does ReferenceLoopHandling.Ignore in Newtonsoft.json exactly do?

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. 我通过ReferenceLoopHandling.Ignore理解的是,如果你有一个对象A引用对象B和B引用C和C再次引用A(A-> B-> C-> A),那么在序列化时,它将最终无穷无尽在C和A之间循环,可以使用下面的方法避免。 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) 我有自我引用循环问题,通过使用上面的问题得到解决,但我想知道它正在做什么,因为上面的行是应用程序的关键(关键肉)

The documentation on this is available here: http://james.newtonking.com/projects/json/help/html/SerializationSettings.htm 有关此文档,请访问: 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 : By default Json.NET will error if a reference loop is encountered (otherwise the serializer will get into an infinite loop). ReferenceLoopHandling.Error :默认情况下,如果遇到引用循环,Json.NET将会出错(否则序列化程序将进入无限循环)。

ReferenceLoopHandling.Ignore : Json.NET will ignore objects in reference loops and not serialize them. ReferenceLoopHandling.Ignore :Json.NET将忽略引用循环中的对象而不是序列化它们。 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. ReferenceLoopHandling.Serialize :此选项强制Json.NET序列化引用循环中的对象。 This is useful if objects are nested but not indefinitely. 如果对象嵌套但不是无限期,这将非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM