Skip to content

Commit

Permalink
Fix JSON stringification for fractional numbers (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeroman committed Oct 14, 2020
1 parent de7202d commit a48a2e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ fn json_stringify_no_args() {
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]
fn json_parse_array_with_reviver() {
let mut engine = Context::new();
Expand Down
6 changes: 3 additions & 3 deletions boa/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ impl Value {
Self::Boolean(b) => Ok(JSONValue::Bool(b)),
Self::Object(ref obj) => obj.to_json(interpreter),
Self::String(ref str) => Ok(JSONValue::String(str.to_string())),
Self::Rational(num) => Ok(JSONNumber::from_f64(num)
.map(JSONValue::Number)
.unwrap_or(JSONValue::Null)),
Self::Rational(num) => Ok(JSONValue::Number(
JSONNumber::from_str(&Number::to_native_string(num)).unwrap(),
)),
Self::Integer(val) => Ok(JSONValue::Number(JSONNumber::from(val))),
Self::BigInt(_) => {
Err(interpreter.construct_type_error("BigInt value can't be serialized in JSON"))
Expand Down

0 comments on commit a48a2e6

Please sign in to comment.