11

好的,我们正在使用我非常喜欢的 Newtonsoft 的 JSON.NET 产品。但是,我有一个简单的类结构,用于分层位置,大致如下所示......

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 属性被排除在外。只有列表的内容被序列化。

现在这就是我所设想的一种很好的格式。它本质上就像LocationList不是一个子类,List<Location>而是一个普通对象,它有一个名为Itemstype的属性List<Location>

{
  "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 的产品是可扩展的,因为他们专门讨论了如何为特定数据类型编写自己的自定义序列化程序,这正是我在这里想要的。但是,他们没有关于如何执行此操作的任何好的代码示例。

如果我们(SO 社区)能够解决这个问题,从技术上讲,通过使用上述格式,我们应该能够序列化 List 的任何子类(或其衍生物/类似对象),只要它们还没有一个名为的属性Items(恕我直言)首先是一个糟糕的设计,因为它会像垃圾一样令人困惑!)也许我们甚至可以让 Newtonsoft 在他们的序列化器中原生地推出这样的东西!

这么说……有人知道如何自定义序列化器/反序列化器以区别对待这个对象吗?

4

3 回答 3

2

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

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; }
}
于 2011-05-03T01:12:17.100 回答
1

好的......这就是我想出的。我必须编写自己的 JsonConverter。我基本上使用它来创建一个内联 JObject,该 JObject 具有我希望它们持续存在的属性,然后我坚持它。然后,当我读回它时,我会做相反的事情。

然而,缺点是它不使用反射或任何其他类似的东西,所以这只适用于我必须逐个属性手动编码属性的特定类型(在这种情况下只有两个,所以这很好!)和它也没有利用我必须手动重新模拟的 DefaultValues 处理,这意味着除非我反思它们,否则这些属性基本上会被忽略。尽管如此,这仍然有效。完美的?不,但是,嘿......事情很少!

当然,欢迎和鼓励评论!

public class LocationListJsonConverter : JsonConverter
{
    public override bool CanConvert(System.Type objectType)
    {
        return objectType == typeof(LocationList);
    }

    public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
    {
        var locationList = (existingValue as LocationList) ?? new LocationList();
        var jLocationList = JObject.ReadFrom(reader);

        locationList.IsExpanded = (bool)(jLocationList["IsExpanded"] ?? false);

        var jLocations = jLocationList["_Items"];
        if(jLocations != null)
        {
            foreach(var jLocation in jLocations)
            {
                var location = serializer.Deserialize<Location>(new JTokenReader(jLocation));
                locationList.Add(location);
            }
        }

        return locationList;

    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var locationList = value as LocationList;

        JObject jLocationList = new JObject();

        if(locationList.IsExpanded)
            jLocationList.Add("IsExpanded", true);

        if(locationList.Count > 0)
        {
            var jLocations = new JArray();

            foreach(var location in locationList)
            {
                jLocations.Add(JObject.FromObject(location, serializer));
            }

            jLocationList.Add("_Items", jLocations);

        }

        jLocationList.WriteTo(writer);

    }

}
于 2011-05-04T20:18:46.833 回答
0

我需要一个名为 FieldGroup 的类,它还具有一些属性来对一些字段进行分组。我首先这样做了。

public class FieldGroup : List<Field>{ ... }

正如帖子所说,它有序列化的问题。所以我修改了这个类,如下所示。所以我可以像从List<Field>派生的 * FieldGroup类一样处理它。

public class FieldGroup : IPrintable, IEnumerable<Field>
{
    public PrintFormat GroupFormat { get; set; } = new PrintFormat();
    public List<Field> Fields { get; set; } = new List<Field>();

    public Field this[int index]
    {
        get => Fields[index];
        set => Fields[index] = value;
    }

    public void Add(Field field)
    {
        Fields.Add(field);
    }
    public IEnumerator<Field> GetEnumerator()
    {
        return new FieldEnumerator(Fields);
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    ...
}
于 2020-09-12T01:49:05.927 回答