微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用Json.NET从Dictionary派生的类型T的序列化和反序列化

我创建了一个示例项目.我正在序列化以下类型:
[JsonObject(IsReference = true,ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
public class SampleTree : Dictionary<string,SampleTree>
{
    [JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
    public SampleClass Value { get; set; }

    [JsonProperty(IsReference = true,ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
    public SampleTree Parent { get; set; }
}

[JsonObject(IsReference = true)]
public class SampleClass
{
    public string A { get; set; }

    public int B { get; set; }

    public bool C { get; set; }
}

程序代码(简化控制台应用程序):

static void Main(string[] args)
{
    var tree = new SampleTree
    {
        Value = new SampleClass
        {
            A = "abc",B = 1,C = true
        },Parent = null
    };

    var treeChild = new SampleTree
    {
        Value = new SampleClass
        {
            A = "def",B = 2,C = false
        },Parent = tree
    };

    tree.Add("firstChild",treeChild);

    var serializerSettings = new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.All,ReferenceLoopHandling = ReferenceLoopHandling.Serialize,Formatting = Formatting.Indented
    };

    var serialized = JsonConvert.SerializeObject(tree,serializerSettings);

    var deserialized = JsonConvert.DeserializeObject<SampleTree>(serialized,serializerSettings);

    var d = deserialized;
}

序列化的结果是完美的:结果字符串包含我之前提交给树的所有数据.但是,使用相同的序列化程序设置对该字符串进行反序列化是不正确的:结果对象根本没有子项.也许主要问题是属性……这样的行为是什么原因?

解决方法

我不确定这是你所要求的.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Dictionary<string,SampleTree> tree = new Dictionary<string,SampleTree>();
            var a = new SampleTree
            {
                Value = new SampleClass
                {
                    A = "abc",C = true
                },Parent = null
            };

            var treeChild = new SampleTree
            {
                Value = new SampleClass
                {
                    A = "def",C = false
                },Parent = a
            };

            tree.Add("parent",a);
            tree.Add("child",treeChild);

            var serializerSettings = new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.All,Formatting = Formatting.Indented
            };

            var serialized = JsonConvert.SerializeObject(tree,serializerSettings);

            var deserialized = JsonConvert.DeserializeObject<Dictionary<string,SampleTree>>(serialized,serializerSettings);

            var d = deserialized;
        }
    }

    [JsonObject(IsReference = true,ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
    public class SampleTree
    {
        [JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
        public SampleClass Value { get; set; }

        [JsonProperty(IsReference = true,ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
        public SampleTree Parent { get; set; }
    }

    [JsonObject(IsReference = true)]
    public class SampleClass
    {
        public string A { get; set; }

        public int B { get; set; }

        public bool C { get; set; }
    }

}

这对我来说完美无缺.如果这是您想要的,请告诉我.

原文地址:https://www.jb51.cc/csharp/99765.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐