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
Currently, the serde_json::value::Serializer uses to_value() at various points. It would be very convenient to allow configuring this via Generics. Today, this Serializer cannot collect metadata or have special logic, which is applied on all nesting-levels. This pseudo code shows a solution to overcome this limitation:
pubtraitSerializeToValue{fnserialize_value<S:Serialize>(&mutself,key:&str,item:S) -> Result<serde_json::Value, serde_json::error::Error>;}#[derive(Default)]structDefaultSerializeToValue;implSerializeToValueforDefaultSerializeToValue{fnserialize_value<S:Serialize>(&mutself,key:&str,item:S) -> Result<serde_json::Value, serde_json::error::Error>{to_value(item)}}// In the next major version, this could be added:// type Serializer = GenericSerializer<DefaultSerializeToValue>;structGenericSerializer<T=DefaultToValue>{strategy:T}
Instead of to_value, we could then use self.strategy.serialize_value (e.g. in serialize_newtype_variant). The runtime performance should not change, but there might be some build-time regressions.
As you can see, the above solution doesn't work without breaking changes. Would you consider to mark Serializer deprecated and add a GenericSerializer<T>? For now, the serde_json::value::Serializer and serde_json::value::GenericSerializer could be generated with a macro to avoid Code-Duplication. An alternative would be to ship this Serializer in a separate crate and duplicate the serde_json::value::Serializer code.
The text was updated successfully, but these errors were encountered:
Feature request:
Currently, the serde_json::value::Serializer uses
to_value()
at various points. It would be very convenient to allow configuring this via Generics. Today, this Serializer cannot collect metadata or have special logic, which is applied on all nesting-levels. This pseudo code shows a solution to overcome this limitation:Instead of
to_value
, we could then useself.strategy.serialize_value
(e.g. inserialize_newtype_variant
). The runtime performance should not change, but there might be some build-time regressions.As you can see, the above solution doesn't work without breaking changes. Would you consider to mark Serializer deprecated and add a
GenericSerializer<T>
? For now, the serde_json::value::Serializer and serde_json::value::GenericSerializer could be generated with a macro to avoid Code-Duplication. An alternative would be to ship this Serializer in a separate crate and duplicate the serde_json::value::Serializer code.The text was updated successfully, but these errors were encountered: