Closed ASalvail closed 6 years ago
Yes, this is a known problem and AFAIR cannot be fixed without breaking backwards compatibility.
For now you can simply use the Serde
newtype wrapper, e.g.:
let structure: Option<Serde<Person>> = bob.try_into().unwrap();
Alright, that's a nice workaround. For anyone that might fall on this thread, here's the fixed version:
#[macro_use]
extern crate stdweb;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use stdweb::unstable::TryInto;
use stdweb::serde::Serde;
#[derive(Deserialize, Debug)]
struct Person {
name: String,
age: i32
}
js_deserializable!( Person );
fn main() {
let bob = js! {
return {
name: "Bob",
age: 33
};
};
let structure: Option<Serde<Person>> = bob.try_into().unwrap();
let opt_pers: Option<Person> = structure.map(|x| x.0);
}
Thanks @koute
Deserialization, using
.try_into()
doesn't seem to work for types wrapped into theOption
enum because of a mismatch between the conversion errors.This minimal example, based on the example from
js_deserializable!
illustrates the problem:leads to