Self-regulatory loop error while trying to serialize

I am trying to serialize an object (specifically the distance object in the UnitClassLibrary dropdown ). Since, apparently, this library does not support serialization, I am ready to change it for my own purposes.

However, I am not sure how to diagnose this problem that occurs. I get the following error when trying to serialize an object using JSON.net (I also tried serializing the XML using the built-in tools and getting similar errors).

Additional information: Self referencing loop detected for property 'EqualityStrategy' with type 'UnitClassLibrary.DistanceEqualityStrategy'. Path ''.

However, I cannot find any self-regulation loop in the code for the Distance object. How can I diagnose this problem?

I'm currently just trying to serialize as follows:

        Distance newDistance = new Distance();
        var json = JsonConvert.SerializeObject(newDistance);

This leads to an error. I can change the library that I use, but I don't have one.

+4
source share
1 answer

In the past, I saw this problem caused by the fact that the object has an object embedded in it that refers to the original object.

For example, let's say you have an object called Project and it has an attribute that is a type of User object. Now inside the User object is a nested object that references the original Project object.

I managed to ignore serialization of nested loops using the following JsonSerializerSetting.

In the example below, projects are a list of project objects.

string json = Newtonsoft.Json.JsonConvert.SerializeObject(projects, Newtonsoft.Json.Formatting.Indented,
                new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                    }
                );
+2
source

All Articles