You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a file format that serialized strings as &[u8]. After learning more this seems cumbersome and complicates things down the line. So I would like to change the format to instead contain (effectively) String but still be able to read older files which will contain byte sequences instead of strings.
Is it possible to use a field attribute to accomplish this?
The text was updated successfully, but these errors were encountered:
This is nearly what I had in mind - but having to discriminate String of Bytes is not what I wanted.
I did however find the docs on #[serde(untagged)] - is it possible to override the default deserializer for one field and do what untagged does here? That is deserialize the following field but trying out String (Arc<str>) and if it fails Vec<u8> (that is Box<[u8]>)? I should add, if the data is a u8 sequence, this is just a String in bytes form.
I think, that you just need to write your function for use with #[serde(deserialize_with)]. Implement a visitor that will accept visit_bytes & Co. and visit_str & Co. and produce Arc<str>, something like:
I have a file format that serialized strings as
&[u8]
. After learning more this seems cumbersome and complicates things down the line. So I would like to change the format to instead contain (effectively)String
but still be able to read older files which will contain byte sequences instead of strings.Is it possible to use a field attribute to accomplish this?
The text was updated successfully, but these errors were encountered: