diff options
| author | Eric Huss <eric@huss.org> | 2018-07-10 16:27:58 -0700 | 
|---|---|---|
| committer | Eric Huss <eric@huss.org> | 2018-07-11 00:50:04 -0700 | 
| commit | 69b4571c6bafcd7d9f675d3eb49d4c088d372eea (patch) | |
| tree | 3a1715fa18ba7640c12fea20ffaa762206c95c62 /src/ser.rs | |
| parent | 2be7ce9079fa02d9dd42958410ba1c40cef9811c (diff) | |
| download | milf-rs-69b4571c6bafcd7d9f675d3eb49d4c088d372eea.tar.gz milf-rs-69b4571c6bafcd7d9f675d3eb49d4c088d372eea.zip  | |
0.5: Support floats nan, inf, and +/-0.0.
cc #224
Diffstat (limited to 'src/ser.rs')
| -rw-r--r-- | src/ser.rs | 49 | 
1 files changed, 23 insertions, 26 deletions
@@ -737,6 +737,27 @@ impl<'a> Serializer<'a> {      }  } +macro_rules! serialize_float { +    ($this:expr, $v:expr) => {{ +        $this.emit_key("float")?; +        if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() { +            drop(write!($this.dst, "-")); +        } +        if $v.is_nan() { +            drop(write!($this.dst, "nan")); +        } else { +            drop(write!($this.dst, "{}", $v)); +        } +        if $v % 1.0 == 0.0 { +            drop(write!($this.dst, ".0")); +        } +        if let State::Table { .. } = $this.state { +            $this.dst.push_str("\n"); +        } +        return Ok(()); +    }}; +} +  impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {      type Ok = ();      type Error = Error; @@ -785,35 +806,11 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {      }      fn serialize_f32(self, v: f32) -> Result<(), Self::Error> { -        if !v.is_finite() { -            return Err(Error::NumberInvalid); -        } - -        self.emit_key("float")?; -        drop(write!(self.dst, "{}", v)); -        if v % 1.0 == 0.0 { -            drop(write!(self.dst, ".0")); -        } -        if let State::Table { .. } = self.state { -            self.dst.push_str("\n"); -        } -        Ok(()) +        serialize_float!(self, v)      }      fn serialize_f64(self, v: f64) -> Result<(), Self::Error> { -        if !v.is_finite() { -            return Err(Error::NumberInvalid); -        } - -        self.emit_key("float")?; -        drop(write!(self.dst, "{}", v)); -        if v % 1.0 == 0.0 { -            drop(write!(self.dst, ".0")); -        } -        if let State::Table { .. } = self.state { -            self.dst.push_str("\n"); -        } -        Ok(()) +        serialize_float!(self, v)      }      fn serialize_char(self, v: char) -> Result<(), Self::Error> {  |