-
I have implemented custom (de-)serialize functions for a remote type, e.g. However, I also have structs that contain e.g. Are free-standing (de-)serializers not yet supported or am I missing something? In any case I think the documentation could use some work here - I don't think I can implement (De-)Serialize for |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @Lehona, I agree that documentation can be improved by a lot. So far this crate doesn't have many users, so I don't know where the hurdles are which need documenting. I don't know if the usage can ever be quite as simple as writing a module with a Coming back to your question though. I think there is no problem in supporting a foreign type, only a bit of boilerplate. I just focus on the You write an implementation like this for impl SerializeAs<RemoteType> for LocalType {/*...*/} Then you can use it like: #[serde_as]
#[derive(...)]
struct Data {
#[serde_as(as = "Vec<LocalType>")]
vec: Vec<RemoteType>,
} In your case The Lines 88 to 98 in 2651ea1 I hope this helps you. Feel free to keep asking questions if you have trouble implementing it :) |
Beta Was this translation helpful? Give feedback.
-
Implementing SerializeAs was straightforward after your comment. I encountered some problems with DeserializeAs because I relied on the DeserializeOwned trait from Serde, which is no longer tied to a lifetime. At some point the lifetime errors just vanished, though, and I can now (de-)serialize
Thank you very much! I suggest explicitly mentioning in your docs that serde_with will only work with types and that any module or function-based (de-)serialization will require an implementation of the *As-Traits on e.g. a unit struct. |
Beta Was this translation helpful? Give feedback.
-
Glad to hear that it works :) I leave this open for now and try to document how to use |
Beta Was this translation helpful? Give feedback.
Hi @Lehona,
I agree that documentation can be improved by a lot. So far this crate doesn't have many users, so I don't know where the hurdles are which need documenting. I don't know if the usage can ever be quite as simple as writing a module with a
serialize
and adeserialize
function, sinceserde_as
works by building a specific type "tree" and uses that to guide the de-/serialization. I see how I can document the interaction better though.Coming back to your question though. I think there is no problem in supporting a foreign type, only a bit of boilerplate. I just focus on the
SerializeAs
trait for now, but the same applies toDeserializeAs
. The trait works in reverse, which allows y…