I need to serialize the response from ReadItemAsync()
based on a property of the document stored in Azure Cosmos DB
.
So if document.type = "dog"
, I need to serialize as ReadItemAsync<Dog>()
and so on.
I don't know the type before retrieving it.
What's a good solution for this?
You could deserialize it to a JObject
and read the type property before mapping it to a specific class.
var response = await container.ReadItemAsync<JObject>("myId", new("myPk"));
if (response.Resource.Value<string>("type") == "dog")
{
var dog = response.Resource.ToObject<Dog>();
}