I have the following HAL+JSON sample:
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"country": "DE",
"_embedded": {
"company": {
"name": "Apple",
"industrySector": "IT",
"owner": "Klaus Kleber",
"_embedded": {
"emailAddresses": [
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"value": "test2@consoto.com",
"type": "Business",
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
}
}
}
],
"phoneNumbers": [
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"value": "01670000000",
"type": "Business",
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
}
}
}
],
},
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"phoneNumbers": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"addresses": {
"href": "https://any-host.com/api/v1/customers/1234"
},
}
},
},
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"legalPerson": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"naturalPerson": {
"href": "https://any-host.com/api/v1/customers/1234"
}
}
}
And the following models:
public class Customer
{
public Guid Id { get; set; }
public string Country { get; set; }
public LegalPerson Company { get; set; }
}
public class LegalPerson
{
public string Name { get; set; }
public string IndustrySector { get; set; }
public string Owner { get; set; }
public ContactInfo[] EmailAddresses { get; set; }
public ContactInfo[] PhoneNumbers { get; set; }
}
public class ContactInfo
{
public Guid Id { get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
Now, because of the _embbeded
, I can't do an out-of-the-box serialization with Newtonsoft.Json
, because then Company
will be null
;
I was hoping to see a native hal+json support by Json.NET, but it only has one recommendation to use a custom JsonConverter
.
I started to create a custom one by myself, but feels like "reinventing the wheel" for me.
So, anyone knows a smart way to get out with this?
UPDATE:
- It's important to not change the models/classes. I can add attributes, but never change it's structures.