How should I work with C # in JSON Serialization and circular links?

I am using the System.Web.Script.Serialization.JavaScriptSerializer contained in the System.Web.Extentions DLL. I have several circular links, such as a basic many-to-many relationship and a parent-child relationship. How can I deal with them? One of our ideas was to replace references to actual objects with foreign keys. For example, instead:

public class Node { public Node Parent { get; set; } public ICollection<Node> Children { get; set; } } 

We would do this:

 public class Node { public long ParentID { get; set; } public ICollection<long> ChildrenIDs { get; set; } } 

I was thinking about using the ScriptIgnore attribute, but how do you use it using many-to-many relationships? Consultations and suggestions will be appreciated. Thanks!

Edit: Here are some examples of classes for many-to-many relationships.

 public class Student { public long StudentID { get; private set; } public ICollection<Class> Classes { get; set; } } public class Class { public long ClassID { get; private set; } public ICollection<Student> Students { get; set; } } 
+4
source share
2 answers

Json.NET was exactly what I was looking for. Another option, however, is to create an anonymous type and serialize it.

+1
source

The usual approach is to use [ScriptIgnore] with the source link node, for example:

 public class Node { [ScriptIgnore] public Node Parent { get; set; } public ICollection<Node> Children { get; set; } } 
+3
source

All Articles