有没有什么办法可以JSON.NET序列化的名单,其中一个子类; T>也有额外的属性?也有、子类、其中一个、有没有什么

2023-09-04 00:15:29 作者:一点痴念

好吧,我们使用Newtonsoft的JSON.NET产品,我真的很喜欢。不过,我有一个简单的类结构,看起来大致是这样的分层位置...

Ok, we're using Newtonsoft's JSON.NET product, which I really love. However, I have a simple class structure for hierarchical locations that look roughly like this...

public class Location
{
    public string Name{ get; set; }
    public LocationList Locations{ get; set; }
}

// Note: LocationList is simply a subclass of a List<T>
// which then adds an IsExpanded property for use by the UI.
public class LocationList : List<Location>
{
    public bool IsExpanded{ get; set; }
}

public class RootViewModel
{
    public LocationList RootLocations{ get; set; }
}

......当我他们序列化到JSON,这一切的伟大工程,除了在LocationList类IsExpanded属性被排除在外。只有列表中的内容被序列化。

...and when I serialize them to JSON, it all works great, except the IsExpanded property on the LocationList class is excluded. Only the list's contents are serialized.

现在,这里就是我设想将是一个很好的格式。这是esentially同样的事情,如果 LocationList 不是名单,其中的一个子类;地点&gt; 而只是一个普通对象有一个名为产品类型 名单,其中财产;地点&gt; 而不是

Now here's what I'm envisioning would be a good format. It's esentially the same thing as if LocationList wasn't a subclass of List<Location> but rather was just a regular object that had a property called Items of type List<Location> instead.

{
  "Locations":
  {
    "IsExpanded": true,
    "Items": [
      {
        "Name": "Main Residence",
        "Locations":
        {
          "IsExpanded": true,
          "Items": [
            {
              "Name": "First Floor",
              "Locations":
              {
                "IsExpanded": false,
                "Items": [
                  {
                    "Name": "Livingroom"
                  },
                  {
                    "Name": "Dining Room"
                  },
                  {
                    "Name": "Kitchen"
                  }
                ]
            },
            {
              "Name": "Second Floor",
              "Locations":
              {
                "IsExpanded": false,
                "Items": [
                  {
                    "Name": "Master Bedroom"
                  },
                  {
                    "Name": "Guest Bedroom"
                  }
                ]
            },
            {
              "Name": "Basement"
            }
          ]
        }
      }
    ]
  }
}

现在我也明白,Newtonsoft的产品是可扩展的,因为他们具体谈一下如何编写自己的自定义序列的特定的数据类型,这将是正是我想在这里。但是,他们没有对如何做到这一点有什么好处code的例子。

Now I also understand that Newtonsoft's product is extensible because they specifically talk about how you can write your own custom serializer for specific data types, which would be exactly what I'd want here. However, they don't have any good code examples on how to do this.

如果我们(SO社区)可以算出来,在技术上使用上述格式,我们应该能够序列表的任何子类(或其衍生物/相似的对象)提供的,他们不已经有一个属性叫做产品(这恕我直言,将是一个糟糕的设计摆在首位,因为这将是混乱的废话!),或许,我们甚至可以得到Newtonsoft推出这样的事情在他们的序列化本身!

If we (the SO community) can figure this out, technically by using the above format we should be able to serialize ANY subclass of List (or its derivatives/similar objects) provided they don't already have a property called Items (which IMHO would be a poor design in the first place since it would be confusing as crap!) Perhaps we can even get Newtonsoft to roll such a thing in their serializer natively!

这样说......任何人都知道如何自定义的串行器/解串器,以不同的方式处理这个对象?

So that said... anyone know how to customize the serializer/deserializer to treat this object differently?

M

推荐答案

通常,当我发现自己争取这样的事情,它告诉我,我应该考虑的另一种方法。在这种情况下,我建议以下视图模型结构作为替代方案:

Usually when I find myself fighting something like this it tells me I should consider another approach. In this case, I would recommend the following view model structure as an alternative:

public class Location
{
    public bool IsExpanded { get; set; }
    public string Name { get; set; }
    public List<Location> Locations { get; set; }
}

public class ViewModel
{
    public List<Location> RootLocations { get; set; }
}