双向链表到 JSON

Zia*_*ani 6 c# json json.net data-structures doubly-linked-list

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

3D 实际数据结构的 2D 样本

实际上这是一个 3D 结构,但为了便于理解,我给出了一个 2D 示例。现在我必须将其转换为 JSON 格式,以便通过 WCF 将此数据发送到客户端,但由于它包含循环,因此无法将其转换为 JSON。我有这些问题

  1. 这种双向链表可以转成JSON吗?
  2. 有没有另一种方法可以做到这一点?
  3. 任何其他推荐的数据结构?如果使用双向链表这是不可能的。

我正在使用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)

Bri*_*ers 4

PreserveReferencesHandling如果您在设置中设置该选项,Json.Net 可以处理引用循环。

JsonSerializerSettings settings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(rootNode, settings);
Run Code Online (Sandbox Code Playgroud)

$id此设置将导致使用特殊属性编写 JSON,$ref这些属性允许将 JSON 反序列化回原始引用(假设您使用 Json.Net 在客户端进行反序列化)。有了这个,您应该能够毫无问题地使用现有的对象结构。

演示:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        Node center = new Node { Name = "In House" };
        Node north = new Node { Name = "North of House" };
        Node west = new Node { Name = "Front of House" };
        Node east = new Node { Name = "Back of House" };
        Node south = new Node { Name = "South of House" };

        center.East = east;
        east.West = center;

        center.West = west;
        west.East = center;

        east.North = north;
        north.East = east;

        east.South = south;
        south.East = east;

        south.West = west;
        west.South = south;

        west.North = north;
        north.West = west;

        DumpNodes(center);

        Console.WriteLine();

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
        settings.NullValueHandling = NullValueHandling.Ignore;
        settings.Formatting = Formatting.Indented;

        string json = JsonConvert.SerializeObject(center, settings);
        Console.WriteLine(json);

        Node node = JsonConvert.DeserializeObject<Node>(json, settings);

        Console.WriteLine();

        DumpNodes(node);
    }

    private static void DumpNodes(Node startingNode)
    {
        HashSet<Node> seen = new HashSet<Node>();
        List<Node> queue = new List<Node>();
        queue.Add(startingNode);
        while (queue.Count > 0)
        {
            Node node = queue[0];
            queue.RemoveAt(0);
            if (!seen.Contains(node))
            {
                seen.Add(node);
                Console.WriteLine(node.Name);
                Look("north", node.North, queue, seen);
                Look("west", node.West, queue, seen);
                Look("east", node.East, queue, seen);
                Look("south", node.South, queue, seen);
            }
        }
    }

    private static void Look(string dir, Node node, List<Node> queue, HashSet<Node> seen)
    {
        if (node != null)
        {
            Console.WriteLine("   " + dir + ": " + node.Name);
            if (!seen.Contains(node))
            {
                queue.Add(node);
            }
        }
    }
}

public class Node
{
    public string Name { get; set; }
    public Node North { get; set; }
    public Node South { get; set; }
    public Node East { get; set; }
    public Node West { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

输出:

In House
   west: Front of House
   east: Back of House
Front of House
   north: North of House
   east: In House
   south: South of House
Back of House
   north: North of House
   west: In House
   south: South of House
North of House
   west: Front of House
   east: Back of House
South of House
   west: Front of House
   east: Back of House

{
  "$id": "1",
  "Name": "In House",
  "East": {
    "$id": "2",
    "Name": "Back of House",
    "North": {
      "$id": "3",
      "Name": "North of House",
      "East": {
        "$ref": "2"
      },
      "West": {
        "$id": "4",
        "Name": "Front of House",
        "North": {
          "$ref": "3"
        },
        "South": {
          "$id": "5",
          "Name": "South of House",
          "East": {
            "$ref": "2"
          },
          "West": {
            "$ref": "4"
          }
        },
        "East": {
          "$ref": "1"
        }
      }
    },
    "South": {
      "$ref": "5"
    },
    "West": {
      "$ref": "1"
    }
  },
  "West": {
    "$ref": "4"
  }
}

In House
   west: Front of House
   east: Back of House
Front of House
   north: North of House
   east: In House
   south: South of House
Back of House
   north: North of House
   west: In House
   south: South of House
North of House
   west: Front of House
   east: Back of House
South of House
   west: Front of House
   east: Back of House
Run Code Online (Sandbox Code Playgroud)

在这里工作小提琴: https: //dotnetfiddle.net/EojsFA