Skip to content

Commit a46b32a

Browse files
committed
Auto merge of rust-num#61 - arcnmx:value-missing-field, r=oli-obk
fix(value): Missing field error when deserializing from Value The logic for treating missing fields differed between the utf8 deserializer and the `Value` one, and missing fields would be incorrectly reported as an "invalid type" error instead. This makes the two deserializers consistent. I believe the "deserialize from unit" logic was just left over from before `deserialize_option` was a thing, and therefore isn't necessary anymore? Fixes rust-num#50
2 parents fc9a5df + d6ec8db commit a46b32a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

json/src/value.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -995,29 +995,31 @@ impl<'a> de::MapVisitor for MapDeserializer<'a> {
995995
}
996996
}
997997

998-
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V, Error>
998+
fn missing_field<V>(&mut self, field: &'static str) -> Result<V, Error>
999999
where V: de::Deserialize,
10001000
{
1001-
// See if the type can deserialize from a unit.
1002-
struct UnitDeserializer;
1001+
struct MissingFieldDeserializer(&'static str);
10031002

1004-
impl de::Deserializer for UnitDeserializer {
1005-
type Error = Error;
1003+
impl de::Deserializer for MissingFieldDeserializer {
1004+
type Error = de::value::Error;
10061005

1007-
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
1006+
fn deserialize<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
10081007
where V: de::Visitor,
10091008
{
1010-
visitor.visit_unit()
1009+
let &mut MissingFieldDeserializer(field) = self;
1010+
Err(de::value::Error::MissingField(field))
10111011
}
10121012

1013-
fn deserialize_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
1013+
fn deserialize_option<V>(&mut self,
1014+
mut visitor: V) -> Result<V::Value, Self::Error>
10141015
where V: de::Visitor,
10151016
{
10161017
visitor.visit_none()
10171018
}
10181019
}
10191020

1020-
Ok(try!(de::Deserialize::deserialize(&mut UnitDeserializer)))
1021+
let mut de = MissingFieldDeserializer(field);
1022+
Ok(try!(de::Deserialize::deserialize(&mut de)))
10211023
}
10221024

10231025
fn size_hint(&self) -> (usize, Option<usize>) {

0 commit comments

Comments
 (0)