我有一个三维结构......实际上是一个双向链表,有六个节点,即左、右、上、下、进、出。如果一个节点在另一个节点的右侧,那么该节点将明显位于第一个节点的左侧。喜欢

实际上这是一个 3D 结构,但为了便于理解,我给出了一个 2D 示例。现在我必须将其转换为 JSON 格式,以便通过 WCF 将此数据发送到客户端,但由于它包含循环,因此无法将其转换为 JSON。我有这些问题
我正在使用Json.Net来处理 JSON。
我的班级是
public class Node
{
public Document document = null;
public Node left = null;
public Node right = null;
public Node up = null;
public Node down = null;
public Node inside = null;
public Node outside = null;
}
Run Code Online (Sandbox Code Playgroud) 我的模型类如下所示:
public class ModelType
{
public string Name { get; set; }
public ModelType SuperType { get; set }
public IEnumerable<ModelType> SubTypes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图序列化对象,但得到StackOverflowException.我试着打电话
JsonConvert.SerializeObject(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)
以及
JsonConvert.SerializeObject(model, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
Run Code Online (Sandbox Code Playgroud)
这两个电话都导致了StackOverflowException.知道如何序列化ModelType实例吗?
编辑:
实例示例,无法序列化:
{
Name: "Child",
SuperType: {
Name: "Parent",
SuperType: null,
SubTypes: [{
Name: "Child",
SuperType: {
Name: "Parent",
SuperType: null,
SubTypes: [{Name: "Child", ...}]
},
SubTypes: …Run Code Online (Sandbox Code Playgroud)