8000 Fixed JSON.parse() failing on numbers that overflow float64 by bardrop · Pull Request #874 · dop251/goja · GitHub
[go: up one dir, main page]

Skip to content

Fixed JSON.parse() failing on numbers that overflow float64 - #874

Merged
dop251 merged 1 commit into
dop251:masterfrom
bardrop-oss:fix-json-parse-number-overflow
Nov 18, 2026
Merged

dop251 merged 1 commit into
dop251:masterfrom
bardrop-oss:fix-json-parse-number-overflow

Conversation

@bardrop
@bardrop bardrop commented Nov 15, 2026
Copy link
Copy Markdown
Contributor

Fixes #873.

JSON.parse('1e400') threw SyntaxError: json: cannot unmarshal number 1e400 into Go value of type float64 instead of returning Infinity. encoding/json's Decoder.Token() decodes numbers into float64 and errors out when the value overflows.

The decoder now uses UseNumber(), and the json.Number token is converted with strconv.ParseFloat. On overflow it returns ±Inf together with ErrRange, which is what JSON.parse needs, so ErrRange is ignored. Underflow (1e-400 → 0) and -0 behave as before.

Testing: added TestJSONParseNumberOverflow (fails on master, passes now). go test ./... (with test262), go vet and gofmt are clean.

encoding/json's Decoder.Token() returns numbers as float64 and fails
with an error for values like 1e400, which JSON.parse() must return as
Infinity. Read numbers as json.Number and convert them with
strconv.ParseFloat, which gives ±Inf on overflow.

Fixes #873.
@dop251
dop251 merged commit c834042 into dop251:master Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON.parse throws SyntaxError for numbers that overflow float64 (1e400)

2 participants

0