Differing behaviour of serializer.serialize_bool()
when using #[serde(serialize_with = "...")]
#2277
Labels
serializer.serialize_bool()
when using #[serde(serialize_with = "...")]
#2277
I have an enum which I am serializing using
serde
andserde_json
so that some of the variants are ofString
type, and others arebool
:I can manually implement
Serialize
like this, which works for my use case:However, I have lots of these sorts of enums and I don't want to pay the cost of manually serializing all of the
String
cases every time I want one or two variants to bebool
. So, I can up with this:The problem now is that
Filter::True
serializes (in json) toObject({"true": true})
, whereas with the manual impl, it serializes to just a plaintrue
.I tried adding
#[serde(untagged)]
to the enum, but whilst that fixes thebool
case, theString
cases now get serialized tonull
!Is this expected behaviour that calling
serializer.serialize_bool()
has a different output when called from aserialize_with
context versus a manual impl context?If this is better in the
serde_json
issue tracker, let me know...The text was updated successfully, but these errors were encountered: