Project

General

Profile

Bug #6921 ยป json-nan.patch

Simen Lybekk, 04/05/2024 10:33 AM

View differences:

rust/src/jsonbuilder.rs
return Err(JsonError::InvalidState);
}
}
self.push_str(&val.to_string())?;
if val.is_nan() {
self.push_str("\"NaN\"")?;
} else {
self.push_str(&val.to_string())?;
}
Ok(self)
}
......
self.push('"')?;
self.push_str(key)?;
self.push_str("\":")?;
self.push_str(&val.to_string())?;
if val.is_nan() {
self.push_str("\"NaN\"")?;
} else {
self.push_str(&val.to_string())?;
}
Ok(self)
}
......
let mut jb = JsonBuilder::try_new_object().unwrap();
jb.set_float("one", 1.1).unwrap();
jb.set_float("two", 2.2).unwrap();
jb.set_float("three", f64::NAN).unwrap();
jb.close().unwrap();
assert_eq!(jb.buf, r#"{"one":1.1,"two":2.2}"#);
assert_eq!(jb.buf, r#"{"one":1.1,"two":2.2,"three":"NaN"}"#);
}
#[test]
......
let mut jb = JsonBuilder::try_new_array().unwrap();
jb.append_float(1.1).unwrap();
jb.append_float(2.2).unwrap();
jb.append_float(f64::NAN).unwrap();
jb.close().unwrap();
assert_eq!(jb.buf, r#"[1.1,2.2]"#);
assert_eq!(jb.buf, r#"[1.1,2.2,"NaN"]"#);
}
}
    (1-1/1)