Browse Source

Fix JSON stringification for fractional numbers (#870)

pull/873/head
George Roman 4 years ago committed by GitHub
parent
commit
a48a2e6986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      boa/src/builtins/json/tests.rs
  2. 6
      boa/src/value/mod.rs

9
boa/src/builtins/json/tests.rs

@ -198,6 +198,15 @@ fn json_stringify_no_args() {
assert_eq!(actual_no_args, expected); assert_eq!(actual_no_args, expected);
} }
#[test]
fn json_stringify_fractional_numbers() {
let mut engine = Context::new();
let actual = forward(&mut engine, r#"JSON.stringify(Math.round(1.0))"#);
let expected = forward(&mut engine, r#""1""#);
assert_eq!(actual, expected);
}
#[test] #[test]
fn json_parse_array_with_reviver() { fn json_parse_array_with_reviver() {
let mut engine = Context::new(); let mut engine = Context::new();

6
boa/src/value/mod.rs

@ -227,9 +227,9 @@ impl Value {
Self::Boolean(b) => Ok(JSONValue::Bool(b)), Self::Boolean(b) => Ok(JSONValue::Bool(b)),
Self::Object(ref obj) => obj.to_json(interpreter), Self::Object(ref obj) => obj.to_json(interpreter),
Self::String(ref str) => Ok(JSONValue::String(str.to_string())), Self::String(ref str) => Ok(JSONValue::String(str.to_string())),
Self::Rational(num) => Ok(JSONNumber::from_f64(num) Self::Rational(num) => Ok(JSONValue::Number(
.map(JSONValue::Number) JSONNumber::from_str(&Number::to_native_string(num)).unwrap(),
.unwrap_or(JSONValue::Null)), )),
Self::Integer(val) => Ok(JSONValue::Number(JSONNumber::from(val))), Self::Integer(val) => Ok(JSONValue::Number(JSONNumber::from(val))),
Self::BigInt(_) => { Self::BigInt(_) => {
Err(interpreter.construct_type_error("BigInt value can't be serialized in JSON")) Err(interpreter.construct_type_error("BigInt value can't be serialized in JSON"))

Loading…
Cancel
Save