英文:
How to deserialize Option<T> where T has a custom deserializer?
问题
I want to create deserialize function for Option<T>
where I already have a custom deserializer for T
and I want to use that deserializer for T when deserializing Option<T>
:
pub fn deserialize(deserializer: D) -> Result<T, D::Error>
{
...
}
That is null
in JSON to be deserialized to None
and non-null to Some(deserialize(deserializer)?)
.
How to define:
pub fn deserialize(deserializer: D) -> Result<Option<T>, D::Error>
{
... // What here?
}
英文:
I want to create deserialize function for Option<T>
where I already have a custom deserializer for T
and I want to use that deserializer for T, when deserializing Option<T>
:
pub fn deserialize(deserializer: D) -> Result<T, D::Error>
{
...
}
That is null
in JSON to be deserialized to None
and non-null to Some(deserialize(deserializer)?)
.
How to define:
pub fn deserialize(deserializer: D) -> Result<Option<T>, D::Error>
{
... // What here?
}
答案1
得分: -2
创建类型 Wrap
:
struct Wrap(T);
为 Wrap
定义 Deserialize
。
现在我可以为 T
定义 deserialize
函数,将 Wrap
进行反序列化并解包 T
的值。
英文:
Create type Wrap
:
struct Wrap(T);
Define Deserialize
for Wrap
.
Now I can define the deserialize
function for T
as deserializing Wrap
and unwrapping the value of T
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论