Serializing a Tree into Json Object

前端 未结 2 1542
盖世英雄少女心
盖世英雄少女心 2021-01-03 09:30

I have the following classes:

TreeNode.cs

public class TreeNode : IEnumerable
{
    public readonly Dictionary

        
2条回答
  •  隐瞒了意图╮
    2021-01-03 09:59

    As I said in my comment, your TreeNode class is serialized as an array because it implements IEnumerable. So the only thing you will ever see when a TreeNode is serialized, are the children of a node. These children are (of course) also serialized as an array - down to the last leaf node. The leaf nodes don't have children and are therefore serialized as empty arrays. So that's why your JSON output looks like this.

    You didn't exactly specify what JSON output you want to have, but I think what you want is like this:

    {
        "Sales Order": { },
        "RFP":
        {
            "2169": { }
        },
        "Purchase Order":
        {
            "2216": { }
        }
    }
    

    To achieve this, your TreeNode class must be serialized as object. In my opinion (and assuming/suggesting you're using Newtonsoft Json.NET), you should write a custom converter class which might look like this:

    class TreeNodeConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            // we can serialize everything that is a TreeNode
            return typeof(TreeNode).IsAssignableFrom(objectType);
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // we currently support only writing of JSON
            throw new NotImplementedException();
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            // we serialize a node by just serializing the _children dictionary
            var node = value as TreeNode;
            serializer.Serialize(writer, node._children);
        }
    }
    

    Then you have to decorate your TreeNode class with the JsonConverterAttribute so the serializer knows that it has to use your converter when serializing a TreeNode object.

    [JsonConverter(typeof(TreeNodeConverter))]
    public class TreeNode : IEnumerable
    {
        // ...
    }
    

    If you don't use Json.NET, I can't exactly tell you what to do but it might help if you would implement IDictionary instead of IEnumerable so the serializer knows you're dealing with key-value-pairs.

提交回复
热议问题