-
Say struct MyType;
impl Display for MyType {};
impl FromStr for MyType {};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct MyStruct {
#[serde_as(as = "DisplayFromStr")] // How to make this line works?
field: Vec<MyType>
} Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, as written, the code tries to use the #[serde_with::serde_as] // You also need to add this line in front of each struct/enum where you use serde_as
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct MyStruct {
#[serde_as(as = "Vec<DisplayFromStr>")]
field: Vec<MyType>,
} Other container types (e.g., If you work with a type you defined (like in your example), you might also be interested in the two derive macros |
Beta Was this translation helpful? Give feedback.
Hi, as written, the code tries to use the
Display
anFromStr
implementations ofVec
to perform the serialization. These do not exist, which is why you see the error. What you want is to apply theDisplayFromStr
conversion on each element of theVec
. You write this as:Other container types (e.g.,
Option
,BTreeMap
,HashSet
) work the same way, by using the collection type for the field and insideserde_as
.If you work with a type you defi…