Fix JSON output for non-finite numbers.

Fixes #6015