From 750aced064b45f70093ed29fb4c7bf0a02c717cb Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Thu, 24 Aug 2023 18:50:35 +0300 Subject: [PATCH] Remove serde_derive Had to tweak the JSON examples a bit, but I think they're still readable and clear. Hopefully this fixes the MSRV tests! --- Cargo.toml | 2 -- examples/json.rs | 13 +++++-------- src/native_tls/mod.rs | 2 ++ src/response.rs | 15 ++++----------- tests/main.rs | 22 ++++++---------------- 5 files changed, 17 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fb97a1b..5596e43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,13 +39,11 @@ native-tls = { version = "0.2", optional = true } [dev-dependencies] tiny_http = "0.8.2" -serde = { version = "=1.0.156", features = ["derive"] } [package.metadata.docs.rs] features = ["json-using-serde", "proxy", "https", "punycode"] [features] -default = ["https"] https = ["https-rustls"] https-rustls = ["rustls", "once_cell", "webpki-roots", "rustls-webpki"] https-rustls-probe = ["https-rustls", "rustls-native-certs"] diff --git a/examples/json.rs b/examples/json.rs index 531ce9e..338d5a1 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -1,16 +1,13 @@ /// This example demonstrates the `json-using-serde` feature. -#[derive(serde::Deserialize)] -struct Response { - /// The field in which `http://httpbin.org/anything` returns the body. - data: String, -} - fn main() -> Result<(), minreq::Error> { let response = minreq::get("http://httpbin.org/anything") .with_body("Hello, world!") .send()?; - let json: Response = response.json()?; - println!("Hello, world! == {}", &json.data); + + // httpbin.org/anything returns the body in the json field "data": + let json: serde_json::Value = response.json()?; + println!("\"Hello, world!\" == {}", json["data"]); + Ok(()) } diff --git a/src/native_tls/mod.rs b/src/native_tls/mod.rs index 44b2858..15146e0 100644 --- a/src/native_tls/mod.rs +++ b/src/native_tls/mod.rs @@ -1,5 +1,7 @@ // Derived from https://lib.rs/crates/native-tls. +#![allow(warnings)] + use std::any::Any; use std::error; use std::fmt; diff --git a/src/response.rs b/src/response.rs index da3d065..00c6832 100644 --- a/src/response.rs +++ b/src/response.rs @@ -143,20 +143,13 @@ impl Response { /// In case compiler cannot figure out return type you might need to declare it explicitly: /// /// ```no_run - /// use serde::Deserialize; - /// - /// #[derive(Deserialize)] - /// struct User { - /// name: String, - /// email: String, - /// } + /// use serde_json::Value; /// /// # fn main() -> Result<(), minreq::Error> { /// # let url_to_json_resource = "http://example.org/resource.json"; - /// let user_name = minreq::get(url_to_json_resource).send()? - /// .json::()? // explicitly declared type `User` - /// .name; - /// println!("User name is '{}'", &user_name); + /// // Value could be any type that implements Deserialize! + /// let user = minreq::get(url_to_json_resource).send()?.json::()?; + /// println!("User name is '{}'", user["name"]); /// # Ok(()) /// # } /// ``` diff --git a/tests/main.rs b/tests/main.rs index 71f3abf..9e23f6b 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,9 +1,6 @@ extern crate minreq; mod setup; -#[cfg(feature = "json-using-serde")] -use serde::{Deserialize, Serialize}; - use self::setup::*; use std::io; @@ -17,28 +14,21 @@ fn test_https() { ); } -#[cfg(feature = "json-using-serde")] -#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] -struct Json<'a> { - str: &'a str, - num: u32, -} - #[test] #[cfg(feature = "json-using-serde")] fn test_json_using_serde() { - let original_json = Json { - str: "Json test", - num: 42, - }; + const JSON_SRC: &str = r#"{ + "str": "Json test", + "num": 42 + }"#; + let original_json: serde_json::Value = serde_json::from_str(JSON_SRC).unwrap(); let response = minreq::post(url("/echo")) .with_json(&original_json) .unwrap() .send() .unwrap(); - let actual_json: Json = response.json().unwrap(); - + let actual_json: serde_json::Value = response.json().unwrap(); assert_eq!(&actual_json, &original_json); }