Make json an optional feature (default off)
This commit is contained in:
committed by
Sean McArthur
parent
a787fc1b38
commit
24394364eb
@@ -3,6 +3,7 @@ use std::fmt;
|
||||
use base64::encode;
|
||||
use http::HttpTryFrom;
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "json")]
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
|
||||
@@ -388,6 +389,12 @@ impl RequestBuilder {
|
||||
/// Sets the body to the JSON serialization of the passed value, and
|
||||
/// also sets the `Content-Type: application/json` header.
|
||||
///
|
||||
/// # Optional
|
||||
///
|
||||
/// This requires the optional `json` feature enabled.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use reqwest::Error;
|
||||
/// # use std::collections::HashMap;
|
||||
@@ -408,6 +415,7 @@ impl RequestBuilder {
|
||||
///
|
||||
/// 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 {
|
||||
@@ -552,6 +560,7 @@ mod tests {
|
||||
use crate::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, HOST};
|
||||
use crate::Method;
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "json")]
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
@@ -776,6 +785,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "json")]
|
||||
fn add_json() {
|
||||
let client = Client::new();
|
||||
let some_url = "https://google.com/";
|
||||
@@ -796,6 +806,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "json")]
|
||||
fn add_json_fail() {
|
||||
use serde::ser::Error as _;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::time::Duration;
|
||||
|
||||
use http;
|
||||
use hyper::header::HeaderMap;
|
||||
#[cfg(feature = "json")]
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use super::client::KeepCoreThreadAlive;
|
||||
@@ -50,6 +51,7 @@ impl Response {
|
||||
/// Checking for general status class:
|
||||
///
|
||||
/// ```rust
|
||||
/// # #[cfg(feature = "json")]
|
||||
/// # fn run() -> Result<(), Box<std::error::Error>> {
|
||||
/// let resp = reqwest::blocking::get("http://httpbin.org/get")?;
|
||||
/// if resp.status().is_success() {
|
||||
@@ -181,6 +183,10 @@ impl Response {
|
||||
|
||||
/// Try and deserialize the response body as JSON using `serde`.
|
||||
///
|
||||
/// # Optional
|
||||
///
|
||||
/// This requires the optional `json` feature enabled.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
@@ -208,7 +214,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 fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
|
||||
wait::timeout(self.inner.json(), self.timeout).map_err(|e| match e {
|
||||
wait::Waited::TimedOut(e) => crate::error::decode(e),
|
||||
|
||||
Reference in New Issue
Block a user