Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 774
DotNetInfo Avatar asked Aug 16 '12 01:08

DotNetInfo


1 Answers

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.

like image 100
DuckMaestro Avatar answered Oct 13 '22 09:10

DuckMaestro