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 tried really hard to create a generic custom serializer for structs T that are serialized as JSON Objects which encode/decode Option<T>::None as {} instead of null (because this behavior is required in the jupyter messaging protocol)
Work-around
I found a non-idiomatic work-around, but it basically requires that I re-implement a custom Option type like this
but has the desired property of being serialized to/from the JSON empty object {} instead of null
EmptyObjectOr::Object(T) ~~ Option::Some(T)
is serialized the same as serde_json::to_string(T), thanks to the flattening behaviour of serde(untagged)
Now I can define other structs like
#[derive(Debug,Deserialize,Serialize)]pubstructMessageParsed{/// parent_header is sometimes a json object (de-serialized by the struct Header),/// sometimes its an empty json object, de-serialized by EmptyObjectOrpubparent_header:EmptyObjectOr<Header>,// ...}
But the question is
I still really want to use the more idiomatic Option type like this:
I tried writing a custom serializer/de-serializer using #[serde(deserialize_with="de_option_or", serialize_with="ser_option_or")]
But the trouble is that the visitor cannot tell if an object is empty without consuming it, then there is no way to push it down to a lower level deserialiser in a generic way.
Click to see the closest I got
This was the closest I got, but it does not compile because map.size_hint() appears to not be able to detect there are zero keys in the object being deserialized (and probably other reasons, i forget what all the compilation errors were).
is there a way to make the deserialize_with/serialize_with method work?
Is there scope for some kind of future feature like #[serde(option_none_as_empty_object)]
Many thanks for your time :)
I decided to post here since I figured any discussion might be easier to find if others run into the same problem than if it were on discord.
The text was updated successfully, but these errors were encountered:
I tried really hard to create a generic custom serializer for structs
T
that are serialized as JSONObject
s which encode/decodeOption<T>::None
as{}
instead ofnull
(because this behavior is required in the jupyter messaging protocol)Work-around
I found a non-idiomatic work-around, but it basically requires that I re-implement a custom
Option
type like thisWhere
EmptyObjectOr::EmptyObject
is likeOption::None
{}
instead ofnull
EmptyObjectOr::Object(T)
~~Option::Some(T)
serde_json::to_string(T)
, thanks to the flattening behaviour ofserde(untagged)
Now I can define other structs like
But the question is
I still really want to use the more idiomatic
Option
type like this:I tried writing a custom serializer/de-serializer using
#[serde(deserialize_with="de_option_or", serialize_with="ser_option_or")]
But the trouble is that the visitor cannot tell if an object is empty without consuming it, then there is no way to push it down to a lower level deserialiser in a generic way.
Click to see the closest I got
This was the closest I got, but it does not compile because
map.size_hint()
appears to not be able to detect there are zero keys in the object being deserialized (and probably other reasons, i forget what all the compilation errors were).deserialize_with
/serialize_with
method work?#[serde(option_none_as_empty_object)]
Many thanks for your time :)
I decided to post here since I figured any discussion might be easier to find if others run into the same problem than if it were on discord.
The text was updated successfully, but these errors were encountered: