I'm consuming a REST API from an adventurous team. They're providing two endpoints where both return a similiar but not equal response. I'm deserializing the responses using the DataContractJsonSerializer.
Endpoint A response:
{
"message": "Hello World."
}
Endpoint B response:
{
"message": [
"Hello World.",
"Hello StackOverflow."
]
}
As you can see endpoint A provides a single string in the message property while endpoint B provides a string array.
I really really want to use the same DataContract but is there a way to make this happen?
[DataContract]
public class Response
{
[DataMember(Name = "message")]
public string Message { get; set; } // Changing this to string[] fails as well.
}
Of course I'm getting an error:
There was an error deserializing the object of type Response. End element 'message' from namespace '' expected. Found element 'item' from namespace ''.
For the sake of completion here's the code:
string jsonPayload = "{ 'Random': 'Payload' }";
HttpClient myHttpClient = getHttpClient();
HttpResponseMessage responseMsg = await myHttpClient.PostAsync("myApiPath", new StringContent(jsonPayload));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Response));
string rspJson = await responseMsg.Content.ReadAsStringAsync();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(rspJson));
Response rsp = (Response)serializer.ReadObject(ms);
DataContractJsonSerializerhas built-in support for polymorphic primitives, and arrays of primitives, so if you declare yourMessagesproperty as anobjectyou will be able to deserialize either JSON:Demo fiddle #1 here.
This model doesn't really capture the fact that
Messageshould be a string, or an array of strings, so you may instead prefer to use some surrogate property for serialization like so:Do note that, according to the docs
Demo fiddle #2 here.