Make json an optional feature (default off)

This commit is contained in:
Constantin Nickel
2019-09-27 16:09:25 +02:00
committed by Sean McArthur
parent a787fc1b38
commit 24394364eb
10 changed files with 59 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ use std::future::Future;
use base64::encode;
use serde::Serialize;
#[cfg(feature = "json")]
use serde_json;
use serde_urlencoded;
@@ -287,10 +288,15 @@ impl RequestBuilder {
/// Send a JSON body.
///
/// # Optional
///
/// This requires the optional `json` feature enabled.
///
/// # Errors
///
/// Serialization can fail if `T`'s implementation of `Serialize` decides to
/// fail, or if `T` contains a map with non-string keys.
#[cfg(feature = "json")]
pub fn json<T: Serialize + ?Sized>(mut self, json: &T) -> RequestBuilder {
let mut error = None;
if let Ok(ref mut req) = self.request {

View File

@@ -11,7 +11,9 @@ use hyper::header::CONTENT_LENGTH;
use hyper::{HeaderMap, StatusCode, Version};
use log::debug;
use mime::Mime;
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
#[cfg(feature = "json")]
use serde_json;
use tokio::timer::Delay;
use url::Url;
@@ -197,6 +199,10 @@ impl Response {
/// Try to deserialize the response body as JSON.
///
/// # Optional
///
/// This requires the optional `json` feature enabled.
///
/// # Examples
///
/// ```
@@ -229,7 +235,9 @@ impl Response {
/// This method fails whenever the response body is not in JSON format
/// or it cannot be properly deserialized to target type `T`. For more
/// details please see [`serde_json::from_reader`].
///
/// [`serde_json::from_reader`]: https://docs.serde.rs/serde_json/fn.from_reader.html
#[cfg(feature = "json")]
pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
let full = self.bytes().await?;