Skip to content

Fix JsonDecoder parsing of large floats with e+ scientific notation#49

Merged
streamich merged 4 commits intomasterfrom
copilot/fix-48
Aug 8, 2025
Merged

Fix JsonDecoder parsing of large floats with e+ scientific notation#49
streamich merged 4 commits intomasterfrom
copilot/fix-48

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 8, 2025

The JsonDecoder.readNum() method had inconsistent character validation logic that prevented parsing of large floating point numbers with explicit plus signs in scientific notation (e.g., 1.7976931348623157e+308).

Problem

The issue occurred because the method's unrolled character-by-character parsing loop had different validation conditions at different positions:

  • Positions 1-8: Correctly allowed + signs with condition c !== 43 (where 43 is the ASCII code for +)
  • Positions 9+: Missing the c !== 43 check, causing numbers with + signs appearing after position 8 to fail parsing

This meant that 1.7976931348623157e+308 (where + appears at position 19) would throw "Invalid JSON" error, while 1.7976931348623157e308 (without explicit +) worked correctly.

Solution

Added the missing c !== 43 checks to all character validation conditions from position 9 onwards in the readNum() method. This ensures consistent handling of plus signs throughout the entire number parsing process.

Testing

  • Added comprehensive test cases covering various scientific notation formats including Number.MAX_VALUE
  • Created new global fixture file with edge case floating point numbers
  • All existing tests continue to pass, confirming no regression
  • Verified parsing of edge cases like infinity (2e+308) and very small numbers (5e-324)

The fix now correctly handles all standard JSON number formats including large floats that reach JavaScript's numeric limits.

Fixes #48.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 8, 2025 22:19
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
…float tests

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] JsonDecoder hardening Fix JsonDecoder parsing of large floats with e+ scientific notation Aug 8, 2025
Copilot AI requested a review from streamich August 8, 2025 22:31
@streamich streamich marked this pull request as ready for review August 8, 2025 22:31
Copy link
Copy Markdown
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run yarn prettier.

* Large floating point numbers and edge cases that should be handled correctly
* by JSON decoders, especially those with scientific notation (e+/e-).
*/
export default {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use these fixtures in JsonDecoder tests.

@streamich streamich merged commit 2878642 into master Aug 8, 2025
4 checks passed
@streamich streamich deleted the copilot/fix-48 branch August 8, 2025 23:12
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 8, 2025

🎉 This PR is included in version 1.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonDecoder hardening

2 participants