Skip to content

Commit

Permalink
Merge pull request 591 from erickt/value-into-deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 4, 2019
2 parents f7a4da0 + 4ae13a5 commit 9f13db7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ impl<'de> EnumAccess<'de> for EnumDeserializer {
}
}

impl<'de> IntoDeserializer<'de, Error> for Value {
type Deserializer = Self;

fn into_deserializer(self) -> Self::Deserializer {
self
}
}

struct VariantDeserializer {
value: Option<Value>,
}
Expand Down
21 changes: 20 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{f32, f64};
use std::{i16, i32, i64, i8};
use std::{u16, u32, u64, u8};

use serde::de::{self, Deserialize, IgnoredAny};
use serde::de::{self, Deserialize, IgnoredAny, IntoDeserializer};
use serde::ser::{self, Serialize, Serializer};

use serde_bytes::{ByteBuf, Bytes};
Expand Down Expand Up @@ -2179,3 +2179,22 @@ fn test_borrow_in_map_key() {
let value = json!({ "map": { "1": null } });
Outer::deserialize(&value).unwrap();
}

#[test]
fn test_value_into_deserializer() {
#[derive(Deserialize)]
struct Outer {
inner: Inner,
}

#[derive(Deserialize)]
struct Inner {
string: String,
}

let mut map = BTreeMap::new();
map.insert("inner", json!({ "string": "Hello World" }));

let outer = Outer::deserialize(map.into_deserializer()).unwrap();
assert_eq!(outer.inner.string, "Hello World");
}

0 comments on commit 9f13db7

Please sign in to comment.