Created
June 18, 2017 23:00
-
-
Save ben0x539/9fd94365137f82fbb5320c75cb73e446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt; | |
use serde::Deserialize; | |
extern crate serde; | |
extern crate serde_json; | |
struct Foo; | |
impl<'de> Deserialize<'de> for Foo { | |
fn deserialize<D>(deserializer: D) -> Result<Foo, D::Error> | |
where D: serde::Deserializer<'de> { | |
deserializer.deserialize_map(MyVisitor) | |
} | |
} | |
struct MyVisitor; | |
impl<'de> serde::de::Visitor<'de> for MyVisitor { | |
type Value = Foo; | |
fn expecting(&self, _: &mut fmt::Formatter) -> fmt::Result { | |
unimplemented!() | |
} | |
fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<Foo, A::Error> { | |
loop { | |
match map.next_entry()? { | |
Some((key, value)) => saw_entry(key, value), | |
None => break, | |
} | |
} | |
Ok(Foo) | |
} | |
} | |
fn saw_entry(k: &str, v: i32) { | |
println!("saw key {}: {}", k, v); | |
} | |
fn main() { | |
let s = r#"{"foo": 42}"#; | |
println!("this works:"); | |
let _: Foo = serde_json::from_str(s).unwrap(); | |
println!("this breaks:"); | |
let _: Foo = serde_json::from_reader(s.as_bytes()).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment