I have a .NET REST web-service that sends JSON data to various mobile apps.
I am testing a new method which returns objects that could be one of several subclasses, for example in the following pseudo-code it could return:
wallet: {
cards: [ { name: 'mastercard', ...},
{ name: 'visacard', ...},
{ name: 'photocard', ...}
]
}
where each card is a SubClass of Card, e.g.
public abstract class Card
{
...
}
public class CreditCard : Card
{
...
}
public class ConcessionCard : Card
{
...
}
public string webServiceMethodGetWallet()
{
myWallet = new Wallet();
CreditCard visaCard = new CreditCard();
CreditCard masterCard = new CreditCard();
ConcessionCard photoCard = new ConcessionCard();
myWallet.cards.AddAll(visaCard, masterCard, photoCard);
return JSON.Serialize(myWallet);
}
I am deserializing this data on multiple platforms (iOS, Android and Windows Phone).
What is a good strategy to let the client apps know which class to use for each of the objects when deserializing? e.g. in the above example, how can the client app tell whether to create classes of type CreditCard
or ConcessionCard
without inspecting all the fields?
Bear in mind, I need to deserialize in Objective C, Java and .NET.
Is there a 'normal' way of doing this?