如何使用多态性将派生 类 序列化为 JSON
How to serialize derived classes using polymorphism to JSON
如果我有以下类:
public class ParentClass
{
public int ParentProperty { get; set; } = 0;
}
public class ChildClass : ParentClass
{
public string ChildProperty { get; set; } = "Child property";
}
public class Container
{
public double ContainerCapacity { get; set; } = 0.2;
public List<ParentClass> ClassContainer { get; set; } = new List<ParentClass>();
}
然后如果我在 Program.cs
中创建以下对象:
// Objects
var container = new Container() { ContainerCapacity = 3.14 };
var parent = new ParentClass() { ParentProperty = 5 };
var child = new ChildClass() { ParentProperty = 10, ChildProperty = "value" };
container.ClassContainer.Add(parent);
container.ClassContainer.Add(child);
// Serialization
var serializerOptions = new JsonSerializerOptions() { WriteIndented = true };
var containerJson = JsonSerializer.Serialize(container, serializerOptions);
Console.WriteLine(containerJson);
预期输出:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ChildProperty": "value",
"ParentProperty": 10
}
]
}
实际输出:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ParentProperty": 10
}
]
}
如何确保 child
上的 属性 ChildProperty
也得到序列化?我将如何处理接口多态性?
我在网上看到有关此问题的信息,但似乎不太容易解决。我建议使用 Newtonsoft.Json
库来解析对象,因为它是一个成熟的库,可以完美地处理 child-parent 对象,而无需编写自定义设置。
为 Newtonsoft.Json
安装 nuget 包,然后像这样解析它:
var containerJson = JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented);
输出如下:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ChildProperty": "value",
"ParentProperty": 10
}
]
}
如果我有以下类:
public class ParentClass
{
public int ParentProperty { get; set; } = 0;
}
public class ChildClass : ParentClass
{
public string ChildProperty { get; set; } = "Child property";
}
public class Container
{
public double ContainerCapacity { get; set; } = 0.2;
public List<ParentClass> ClassContainer { get; set; } = new List<ParentClass>();
}
然后如果我在 Program.cs
中创建以下对象:
// Objects
var container = new Container() { ContainerCapacity = 3.14 };
var parent = new ParentClass() { ParentProperty = 5 };
var child = new ChildClass() { ParentProperty = 10, ChildProperty = "value" };
container.ClassContainer.Add(parent);
container.ClassContainer.Add(child);
// Serialization
var serializerOptions = new JsonSerializerOptions() { WriteIndented = true };
var containerJson = JsonSerializer.Serialize(container, serializerOptions);
Console.WriteLine(containerJson);
预期输出:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ChildProperty": "value",
"ParentProperty": 10
}
]
}
实际输出:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ParentProperty": 10
}
]
}
如何确保 child
上的 属性 ChildProperty
也得到序列化?我将如何处理接口多态性?
我在网上看到有关此问题的信息,但似乎不太容易解决。我建议使用 Newtonsoft.Json
库来解析对象,因为它是一个成熟的库,可以完美地处理 child-parent 对象,而无需编写自定义设置。
为 Newtonsoft.Json
安装 nuget 包,然后像这样解析它:
var containerJson = JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented);
输出如下:
{
"ContainerCapacity": 3.14,
"ClassContainer": [
{
"ParentProperty": 5
},
{
"ChildProperty": "value",
"ParentProperty": 10
}
]
}