以类型名称作为根名称序列化具体对象的数组

时间:2019-02-22 13:19:35

标签: c# json.net

假设我有以下课程:

class Casket
{
    public List<Fruit> Fruits = new List<Fruit>();
}

interface Fruit
{
}

[JsonObject("orange")]
class Orange : Fruit
{
    public string Bar = "asd";
}

[JsonObject("apple")]    
class Apple : Fruit
{
    public int Foo = 2;
}

有没有很漂亮的方法将Casket序列化为此?

{
    "Fruits": [
        "apple": {
            "Foo": 2
        },
        "orange: {
            "Bar": "asd"
        }
    ]
}
  1. TypeNameHandling由于其令人讨厌的“ $ type”前缀和值名称而无法使用,即使您使用TypeNameAssemblyFormatHandling也是如此,除非您证明我错了
  2. 创建匿名类的想法
  3. 创建自定义转换器可能是答案,因为我仍然没有尝试过,尽管如果有些东西不需要创建其他类,那就更好了

0 个答案:

没有答案
相关问题