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
This trait was ripped directly from rustc_serialize::json::ToJson but the point was lost by providing an impl ToJson for T where T: Serialize. The rustc_serialize trait is infallible and there are impls for standard library types that can be infallibly converted to Json - not for all types that implement Encodable.
I don't think the trait as it currently exists serves a purpose. The fallible use case is covered by serde_json::to_value and the literal use case is covered by the json! macro.
I would be open to seeing an infallible ToJson trait explored in its own crate - ideally complete with its own custom derive which is distinct from serde's Serialize.
#[derive(ToJson)]structS{a:u8,b:u8,}// The generated code:implToJsonforS{fnto_json(&self) -> serde_json::Value{json!({"a":ToJson::to_json(&self.a),"b":ToJson::to_json(&self.b),})}}
The text was updated successfully, but these errors were encountered:
D'oh. I was using this trait for a type that I wanted to be able to convert to JSON but which I didn't want to make generically serializable. The reason being, I have a specific JSON format I need to match, so my ToJson implementation is written for that format. But that format isn't the "natural" format for my data structure. In fact, I have a separate XML format too that I emit (but I don't use Serde at all for that, I use a custom XML serializer), which definitely doesn't match the JSON format. I'm also not even sure if it's possible to get the JSON structure I need using Serializable.
The main fallout here is I won't be able to use my type with json! anymore. I can certainly work around that limitation, but it's annoying, and surely I'm not the only person who has a type that needs precise control over its JSON representation.
This trait was ripped directly from
rustc_serialize::json::ToJson
but the point was lost by providing an impl ToJson for T where T: Serialize. The rustc_serialize trait is infallible and there are impls for standard library types that can be infallibly converted to Json - not for all types that implement Encodable.I don't think the trait as it currently exists serves a purpose. The fallible use case is covered by serde_json::to_value and the literal use case is covered by the
json!
macro.I would be open to seeing an infallible ToJson trait explored in its own crate - ideally complete with its own custom derive which is distinct from serde's Serialize.
The text was updated successfully, but these errors were encountered: