英文:
Serializing ReadItemAsync() response based on a property of the document
问题
我需要根据存储在Azure Cosmos DB中的文档属性对ReadItemAsync()
的响应进行序列化。
所以如果document.type = "dog"
,我需要序列化为ReadItemAsync<Dog>()
,依此类推。
在检索之前,我不知道类型是什么。
有什么好的解决方案吗?
英文:
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?
答案1
得分: 1
你可以将其反序列化为 JObject
并在将其映射到特定类之前读取类型属性。
var response = await container.ReadItemAsync<JObject>("myId", new("myPk"));
if (response.Resource.Value<string>("type") == "dog")
{
var dog = response.Resource.ToObject<Dog>();
}
英文:
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>();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论