aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/de.rs9
-rw-r--r--src/ser.rs49
2 files changed, 32 insertions, 26 deletions
diff --git a/src/de.rs b/src/de.rs
index 1d43cc9..5e63cb4 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -6,6 +6,7 @@
use std::borrow::Cow;
use std::error;
+use std::f64;
use std::fmt;
use std::str;
use std::vec;
@@ -882,6 +883,14 @@ impl<'a> Deserializer<'a> {
}
_ => Err(self.error(at, ErrorKind::NumberInvalid)),
}
+ } else if s == "inf" {
+ Ok(Value { e: E::Float(f64::INFINITY), start: start, end: end })
+ } else if s == "-inf" {
+ Ok(Value { e: E::Float(f64::NEG_INFINITY), start: start, end: end })
+ } else if s == "nan" {
+ Ok(Value { e: E::Float(f64::NAN), start: start, end: end })
+ } else if s == "-nan" {
+ Ok(Value { e: E::Float(-f64::NAN), start: start, end: end })
} else {
self.integer(s).map(|f| Value { e: E::Integer(f), start: start, end: end })
}
diff --git a/src/ser.rs b/src/ser.rs
index 8f4e72b..9d989db 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -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> {