【问题标题】:Serializing and deserializing of type T, derived from Dictionary<string, T>, using Json.NET使用 Json.NET 对从 Dictionary<string, T> 派生的类型 T 进行序列化和反序列化
【发布时间】:2015-05-12 08:01:57
【问题描述】:

我创建了一个示例项目。我正在序列化以下类型:

[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;
}

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

【问题讨论】:

    标签: c# dictionary json.net


    【解决方案1】:

    我没有在 Main 函数中声明 Dictionary,而是向 SampleTree 本身添加了一个 Dictionary 类型的名为 Children 的属性(并删除了对 c 的继承)。

    【讨论】:

      【解决方案2】:

      我不确定这是您需要的。

      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",
                          B = 1,
                          C = true
                      },
                      Parent = null
                  };
      
                  var treeChild = new SampleTree
                  {
                      Value = new SampleClass
                      {
                          A = "def",
                          B = 2,
                          C = false
                      },
                      Parent = a
                  };
      
                  tree.Add("parent", a);
                  tree.Add("child", treeChild);
      
                  var serializerSettings = new JsonSerializerSettings
                  {
                      PreserveReferencesHandling = PreserveReferencesHandling.All,
                      ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                      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; }
          }
      
      }
      

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

      【讨论】:

      • 这与我的解决方案很接近。我没有在 Main 函数中声明 Dictionary,而是向 SampleTree 本身添加了一个名为 Children 的 Dictionary 类型的属性(并删除了对 c 的继承)。但主要问题是:如果我不想删除继承,如何完成这项工作?
      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 2017-01-15
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多