I have created a serialize_foo
function that can serialize the Foo
struct.
struct Foo(i32)
<- The Foo
struct looks like this.
The Foo
struct is used in another struct Bar
that looks like this:
struct Bar {
#[serde(serialize_with = "serialize_foo")]
pub x: Foo,
#[serde(serialize_with = "serialize_foo")]
pub y: Option<Foo>
}
The x
field can be serialized with the serialize_foo
function, but the y
field can't. It is because it is an Option
.
How should I serialize an Option
, should there be a new function serialize_foo_option
, but what would the function do if the value is None
.
Foo
struct? If so, you should makeserialize_foo
be the actualSerialize
implementation forFoo
, and then you'll get all of the composed types likeOption<Foo>
,Vec<Foo>
, etc. for free. – DrennanFoo
. – HennieFoo
struct in a wrapper type as shown in this playground (always from the former thread) – Spirochetetry_serialize_foo
. That really just contains amap
call on theOption
. – PadillaVec<Foo>
orBTreeMap<Foo, i32>
. The link explains how to use the existingserialize_foo
function for that. – Borek