Skip to content

Commit

Permalink
add the ability to deserialize JSON from a response body directly
Browse files Browse the repository at this point in the history
  • Loading branch information
gsquire committed Nov 20, 2016
1 parent 6967f92 commit 2d10ecc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hyper::status::StatusCode;
use hyper::version::HttpVersion;
use hyper::{Url};

use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_urlencoded;

Expand Down Expand Up @@ -275,6 +275,11 @@ impl Response {
pub fn version(&self) -> &HttpVersion {
&self.inner.version
}

/// Try and deserialize the response body as JSON.
pub fn json<T: Deserialize>(&mut self) -> ::Result<T> {
serde_json::from_reader(self).map_err(::Error::from)
}
}

/// Read the body of the Response.
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@ impl From<::serde_urlencoded::ser::Error> for Error {
}
}

impl From<::serde_json::Error> for Error {
fn from(err: ::serde_json::Error) -> Error {
Error::Serialize(Box::new(err))
}
}

/// A `Result` alias where the `Err` case is `reqwest::Error`.
pub type Result<T> = ::std::result::Result<T, Error>;

0 comments on commit 2d10ecc

Please sign in to comment.