Encode ±INFINITY as ±1e5000

The V8 behavior of encoding infinity as null doesn't make sense to me.
Using ±1e5000 is better, because JSON.parse decodes it as INFINITY and
the information is preserved. This could be a breaking change for some
This commit is contained in:
Justine Tunney 2024-06-01 03:15:23 -07:00
parent 9b6718ac99
commit fae1c32267
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 7 additions and 12 deletions

View file

@ -22,7 +22,7 @@ assert(EncodeJson(0) == "0")
assert(EncodeJson(0.0) == "0")
assert(EncodeJson(3.14) == "3.14")
assert(EncodeJson(0/0) == "null")
assert(EncodeJson(math.huge) == "null")
assert(EncodeJson(math.huge) == "1e5000")
assert(EncodeJson(123.456e-789) == '0')
assert(EncodeJson(9223372036854775807) == '9223372036854775807')
assert(EncodeJson(-9223372036854775807 - 1) == '-9223372036854775808')

View file

@ -176,30 +176,25 @@ assert(DecodeJson(' [123e-10000000] '))
assert(EncodeJson(DecodeJson(' [123e-10000000] ')) == '[0]')
assert(EncodeLua(DecodeJson(' [123e-10000000] ')) == '{0.}')
-- [jart] consistent with v8 we encode Infinity as null (wut?)
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_real_pos_overflow.json
assert(DecodeJson(' [123123e100000] '))
assert(EncodeJson(DecodeJson(' [123123e100000] ')) == '[null]')
assert(EncodeJson(DecodeJson(' [123123e100000] ')) == '[1e5000]')
-- [jart] consistent with v8 we encode -Infinity as null (wut?)
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_real_neg_overflow.json
assert(DecodeJson(' [-123123e100000] '))
assert(EncodeJson(DecodeJson(' [-123123e100000] ')) == '[null]')
assert(EncodeJson(DecodeJson(' [-123123e100000] ')) == '[-1e5000]')
-- [jart] consistent with v8 we encode Infinity as null (wut?)
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_pos_double_huge_exp.json
assert(DecodeJson(' [1.5e+9999] '))
assert(EncodeJson(DecodeJson(' [1.5e+9999] ')) == '[null]')
assert(EncodeJson(DecodeJson(' [1.5e+9999] ')) == '[1e5000]')
-- [jart] consistent with v8 we encode -Infinity as null (wut?)
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_neg_int_huge_exp.json
assert(DecodeJson(' [-1e+9999] '))
assert(EncodeJson(DecodeJson(' [-1e+9999] ')) == '[null]')
assert(EncodeJson(DecodeJson(' [-1e+9999] ')) == '[-1e5000]')
-- [jart] consistent with v8 we encode Infinity as null (wut?)
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_huge_exp.json
assert(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] '))
assert(EncodeJson(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ')) == '[null]')
assert(EncodeJson(DecodeJson(' [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ')) == '[1e5000]')
-- [jart] consistent with v8 we encode underflow as 0
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/i_number_double_huge_neg_exp.json

View file

@ -38,7 +38,7 @@ char* DoubleToJson(char buf[128], double x) {
static const DoubleToStringConverter kDoubleToJson(
DoubleToStringConverter::UNIQUE_ZERO |
DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN,
"null", "null", 'e', -6, 21, 6, 0);
"1e5000", "null", 'e', -6, 21, 6, 0);
kDoubleToJson.ToShortest(x, &b);
b.Finalize();
if (READ32LE(buf) != READ32LE("-nul")) {