I'm doing what I assume to be a very common thing and have value-type structs that will be serialized to JSON and deserialized from JSON. Is there a way to do that with #[derive()]
?
For a struct Foo
:
#[derive(Serialize, Deserialize/*, JsonSerialize??, JsonDeserialize??*/)
struct Foo {
a: String,
b: i64,
c: Vec<u8>,
d: HashMap<String, String>
e: MyEnum
}
It should generate something like:
impl TryFrom<Value> for Foo {
fn try_from(v: Value) -> Result<Foo, Error> {
serde_json::from_value(v)
}
}
impl Into<Value> for Foo {
fn into(self) -> Value {
serde_json::to_value(self)
}
}
impl TryFrom<String> for Foo {
fn try_from(v: String) -> Result<Foo, Error> {
Foo::try_from(serde_json::to_value(v))
}
}
impl Into<String> for Foo {
fn into(self) -> String {
serde_json::to_string(self)
}
}
Is there any need for try_from::<String>()
and into::<String>()
?