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

What does ReferenceLoopHandling.Ignore in Newtonsoft.json exactly do?(Newtonsoft.json 中的 ReferenceLoopHandling.Ignore 究竟做了什么?)
本文介绍了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 究竟做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)