I'm trying to deserialize the JSON response that I get back from the ARIN whois REST API. I'm new to JSON but I think they are returning two different schemas depending on the results and I'm having a hard time taking it apart.
Here is an abridged version of the JSON that's returned if there are multiple net objects:
{
"nets": {
"@xmlns":{"ns3":"http:\/\/www.arin.net\/whoisrws\/netref\/v2",
"net": [{
"customerRef": {
"@name": "Internet Customer",
},
}, {
"orgRef": {
"@name": "Internet Service Provider",
},
}
]
}
}
Note that nets is an array of net objects. When there is only one net object returned, the JSON looks like this:
{
"nets": {
"@xmlns":{"ns3":"http:\/\/www.arin.net\/whoisrws\/netref\/v2",
"net": {
"orgRef": {
"@name": "Internet Customer",
},
}
}
}
In this case nets is an object that contains a single net object. I can define my class with an array or with a single embedded object but deserializing with JsonConvert.DeserializeObject<ARINWhois>(response)
will throw an exception on results that don't agree with my schema.
I've considered two options:
- Deserialize it all by hand with
JsonTextReader
. - Define both object schemas and retry with the other one if an exception is thrown.
Is there is an easier and more elegant solution?