From 761f47fbce5fee9ba9160727862ca4709e4c304a Mon Sep 17 00:00:00 2001 From: lovasoa Date: Sat, 6 Apr 2024 23:12:04 +0200 Subject: [PATCH] str & char --- src/de.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/de.rs b/src/de.rs index 45c0b11..474aad1 100644 --- a/src/de.rs +++ b/src/de.rs @@ -246,6 +246,17 @@ impl Deserializer { t => Err(Error::UnexpectedType(t)), } } + + fn read_float(&mut self, header: Header) -> Result + where + for<'a> T: Deserialize<'a>, + { + match header.element_type { + ElementType::Float => self.read_json_compatible(header), + ElementType::Float5 => self.read_json5_compatible(header), + t => Err(Error::UnexpectedType(t)), + } + } } /// A reader wrapped that adds double quotes around the original text @@ -525,32 +536,40 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer { visitor.visit_unit() } - fn deserialize_f32(self, _visitor: V) -> Result + fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de>, { - todo!() + let header = self.read_header()?; + visitor.visit_f32(self.read_float(header)?) } - fn deserialize_f64(self, _visitor: V) -> Result + fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de>, { - todo!() + let header = self.read_header()?; + visitor.visit_f64(self.read_float(header)?) } - fn deserialize_char(self, _visitor: V) -> Result + fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de>, { - todo!() + let header = self.read_header()?; + let s = self.read_string(header)?; + if s.len() != 1 { + return Err(Error::Message("invalid string length for char".into())); + } + visitor.visit_char(s.chars().next().unwrap()) } - fn deserialize_str(self, _visitor: V) -> Result + fn deserialize_str(self, visitor: V) -> Result where V: Visitor<'de>, { - todo!("Borrowed string deserialization is not supported") + // Borrowed string deserialization is not supported + self.deserialize_string(visitor) } fn deserialize_string(self, visitor: V) -> Result