Make Response::into_json deserialize into a serde DeserializeOwned

This removes the necessity to take the result of Response::into_json and
having to convert it into a struct by using serde_json::from_value

This adds no new dependencies since serde_json already depends on serde.
Users of ureq will have to include `serde_derive` either by importing it
directly or by using serde with the `derive` feature, unless they want to
manually implement `Deserialize` on their structs.
This commit is contained in:
Paolo Barbolini
2020-05-02 17:32:33 +02:00
committed by Martin Algesten
parent 8f2d094cef
commit 0b69c595b6
5 changed files with 44 additions and 17 deletions

View File

@@ -61,6 +61,13 @@ fn body_as_text() {
#[test]
#[cfg(feature = "json")]
fn body_as_json() {
use serde::Deserialize;
#[derive(Deserialize)]
struct Hello {
hello: String,
}
test::set_handler("/body_as_json", |_unit| {
test::make_response(
200,
@@ -70,8 +77,8 @@ fn body_as_json() {
)
});
let resp = get("test://host/body_as_json").call();
let json = resp.into_json().unwrap();
assert_eq!(json["hello"], "world");
let json = resp.into_json::<Hello>().unwrap();
assert_eq!(json.hello, "world");
}
#[test]