C# 使用ASP.NET Core 3,如何使json响应不序列化为复杂类型?
我有这个模型:C# 使用ASP.NET Core 3,如何使json响应不序列化为复杂类型?,c#,json,asp.net-mvc,asp.net-core,C#,Json,Asp.net Mvc,Asp.net Core,我有这个模型: public class Product { public Product() { this.Supplier = new Supplier(); } public int Id { get; set; } public string Name { get; set; } public double Price { get; set; }
public class Product
{
public Product()
{
this.Supplier = new Supplier();
}
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public int SupplierId { get; set; }
public ProductStatus Status { get; set; }
public Supplier Supplier { get; set; }
}
还有另一种模式:
public class Supplier
{
public Supplier()
{
this.Products = new List<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public ICollection<Product> Products { get; set; }
}
当尝试使用供应商模型发送响应时:
{
"id": 1,
"name": "HP",
"address": "Mckinley",
"products": [
{
"id": 1,
"name": "HP-ENVY 15",
"price": 800,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 12,
"name": "HP-PAVILION 14",
"price": 550,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 13,
"name": "HP-ENVY 17",
"price": 1200.7,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 14,
"name": "Printer xxx-2020",
"price": 300.5,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 15,
"name": "Compaq Presario",
"price": 500.8,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
}
]
}
两个响应都试图序列化其中的complext对象,是否可以:
1.)当发送产品模型的json响应时,它将仅显示产品及其供应商(不包括该供应商的产品属性)
2.)当发送供应商模型的json响应时,它将仅显示供应商及其产品(不会包含每个产品的供应商属性)
为了实现这一点,我需要在中间件上配置json选项吗?或者我应该创建DTO/类,其中产品模型中引用的供应商复杂对象没有其产品的属性,反之亦然(供应商模型没有产品属性)
注意:我知道最好使用viewmodel/dtos进行json响应,但在我的示例中,让我们只说产品和模型都不是域类,而是viewmodels,因为我的主要问题是如何防止json阻止序列化对象属性。json.net有
MaxDepth
属性用于反序列化到某一点
不过要小心,这会在一定程度上帮助你。它不会识别相同的对象循环依赖关系,只会在反序列化的第二级停止
对于相反的事情(序列化),如果我做对了,您可以使用扩展
JsonTextWriter
。您可以使用,例如,Select()
,简单地返回您想要的内容。是的,您应该为此使用DTO。使用实体类型进行序列化会暴露用户可能不应该知道的实现细节,而反序列化到实体类型是非常危险的,因为用户可以完全覆盖任何内容。@poke yep,我知道这就是为什么在我的问题中提到它的原因。我同意你所说的。@bolkay我实际上是这样做的,但稍后我将对其进行重构,并使用DTO/ViewModel对json数据进行整形。谢谢
{
"id": 1,
"name": "HP",
"address": "Mckinley",
"products": [
{
"id": 1,
"name": "HP-ENVY 15",
"price": 800,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 12,
"name": "HP-PAVILION 14",
"price": 550,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 13,
"name": "HP-ENVY 17",
"price": 1200.7,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 14,
"name": "Printer xxx-2020",
"price": 300.5,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
},
{
"id": 15,
"name": "Compaq Presario",
"price": 500.8,
"supplierId": 0,
"status": 0,
"supplier": {
"id": 0,
"name": null,
"address": null,
"products": []
}
}
]
}