fix deserialize error with invalid map type

This commit is contained in:
appflowy 2021-09-22 15:02:55 +08:00
parent 79560dd161
commit 47f3a187c4

View File

@ -71,7 +71,6 @@ impl<'de> Deserialize<'de> for Attributes {
{ {
let mut attributes = Attributes::new(); let mut attributes = Attributes::new();
while let Some(key) = map.next_key::<AttributeKey>()? { while let Some(key) = map.next_key::<AttributeKey>()? {
log::warn!("{:?}", key);
let value = map.next_value::<AttributeValue>()?; let value = map.next_value::<AttributeValue>()?;
attributes.add_kv(key, value); attributes.add_kv(key, value);
} }
@ -103,10 +102,7 @@ impl<'de> Deserialize<'de> for AttributeValue {
struct AttributeValueVisitor; struct AttributeValueVisitor;
impl<'de> Visitor<'de> for AttributeValueVisitor { impl<'de> Visitor<'de> for AttributeValueVisitor {
type Value = AttributeValue; type Value = AttributeValue;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("bool, usize or string") }
//
formatter.write_str("bool, usize or string")
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E> fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
where where
E: de::Error, E: de::Error,
@ -190,6 +186,16 @@ impl<'de> Deserialize<'de> for AttributeValue {
{ {
Ok(AttributeValue(None)) Ok(AttributeValue(None))
} }
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
// https://github.com/serde-rs/json/issues/505
let mut map = map;
let value = map.next_value::<AttributeValue>()?;
Ok(value)
}
} }
deserializer.deserialize_any(AttributeValueVisitor) deserializer.deserialize_any(AttributeValueVisitor)