I am looking for a good way to serialize/deserialize the follwing JSON format. I know it's quite uncommon, but thats how it's given. Currently i am using json.net and I think it's quite good and customizable, but i haven't found any good solution. I'am aware of the CustomConverter and ContractResolver see post for example:
JSON.net ContractResolver vs. JsonConverter
But all my solutions don't feel right at the moment. Maybe you guys have a simple good solution
Here is the Json format: the main ressource here is the house (key1) for example api/house/1 get
the relations for this object are then given only with the fields typename and key, where as the main resource shows all "primitiv" types (as called in json.net : datetime, string, int..). The whole relation entity with their own fields are given in the "addition" section
{
"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"
}
}
}
}
]
}
For my class design i would prefer the following, but dont has to be this way
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; }
}
I don't want to hardcode it into the customconverter, because i have a lot of those entites and they might change. Do you guys have any help on this one? thanks