我正在寻找一种序列化/反序列化后续JSON格式的好方法。我知道这是非常罕见的,但这就是它给出的方式。目前我正在使用json.net,我认为它非常好并可定制,但我还没有找到任何好的解决方案。我知道CustomConverter和ContractResolver见例如:
JSON.net ContractResolver vs. JsonConverter
但我现在所有的解决方案都感觉不对劲。也许你们有一个简单的好解决方案
这是Json格式: 这里的主要资源是房子(key1),例如api / house / 1 get
此对象的关系仅使用字段typename和key给出,其中主要资源显示所有“primitiv”类型(在json.net中调用:datetime,string,int ..)。具有各自字段的整个关系实体在“添加”部分中给出
{
"info": {
"typename": "house",
"key": "1",
"ownFields": {
"width": "10 meter",
"height": "12 meter"
},
"relation": {
"resident": {
"info": [
{
"typename": "people",
"key": "1"
},
{
"typename": "people",
"key": "3"
}
]
},
"homeowner": {
"info": {
"typename": "people",
"key": "4"
}
},
"neighbors": {
"info": [
{
"typename": "house",
"key": "2"
}
]
}
}
},
"addition": [
{
"typename": "people",
"key": "1",
"ownFields": {
"firstname": "Martin",
"lastname": "Keystone",
"phone": "1234"
},
"relation": {
"parent": {
"info": {
"typename": "people",
"key": "6"
}
}
}
},
{
"typename": "people",
"key": "2",
"ownFields": {
"firstname": "Sandra",
"lastname": "Keystone",
"phone": "1234"
},
"relation": {
"parent": {
"info": {
"typename": "people",
"key": "7"
}
}
}
}
]
}
对于我的班级设计,我更喜欢以下,但不一定是这样
public class entity
{
public string typename { get; set; } #maybe not needed since class name could be used
public string key { get; set; }
}
public class house : entity
{
public house() { }
public string width { get; set; }
public string height { get; set; }
public people homeowner { get; set; }
public List<people> resident { get; set; }
public List<house> neighbors { get; set; }
}
public class people : entity
{
public people() { }
public string firstname { get; set; }
public string lastname { get; set; }
public string phone { get; set; }
public people parent { get; set; }
}
我不想将其硬编码到customconverter中,因为我有很多这些参与者,他们可能会改变。你们对这个人有什么帮助吗? 感谢