update doc examples

- Make examples runnable for doc tests
- Add error handling using `?` instead of `unwrap`
This commit is contained in:
James Kominick
2017-05-21 17:14:32 -04:00
parent e9d5774365
commit 812c220e61
4 changed files with 97 additions and 40 deletions

View File

@@ -87,27 +87,31 @@ impl Response {
}
}
/// Try and deserialize the response body as JSON.
/// Try and deserialize the response body as JSON using `serde`.
///
/// # Examples
///
/// ```rust,no_run
/// extern crate reqwest;
/// #[macro_use]
/// extern crate serde_derive;
///
/// ```rust
/// # extern crate reqwest;
/// # #[macro_use] extern crate serde_derive;
/// #
/// # use reqwest::Error;
/// #
/// #[derive(Deserialize)]
/// struct User {
/// name: String,
/// age: u8,
/// struct Response {
/// origin: String,
/// }
///
/// fn main() {
/// let user: User = reqwest::get("http://127.0.0.1/user.json")
/// .expect("network error")
/// .json()
/// .expect("malformed json");
/// }
/// # fn run() -> Result<(), Error> {
/// let resp: Response = reqwest::get("http://127.0.0.1/user.json")?.json()?;
/// # Ok(())
/// # }
/// #
/// # fn main() {
/// # if let Err(error) = run() {
/// # println!("Error: {:?}", error);
/// # }
/// # }
/// ```
#[inline]
pub fn json<T: DeserializeOwned>(&mut self) -> ::Result<T> {