36

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

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

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

4

1 回答 1

45

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: 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.

于 2013-01-07T22:53:03.223 回答