Skip to content

Fix #696: write non-finite TOML floats as nan/inf/-inf - #697

Merged
yawkat merged 1 commit into
FasterXML:2.xfrom
seonwooj0810:fix/issue-696-toml-nonfinite-float
Jul 2, 2026
Merged

yawkat merged 1 commit into
FasterXML:2.xfrom
seonwooj0810:fix/issue-696-toml-nonfinite-float

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes #696

Root cause

TomlGenerator.writeNumber(double) and writeNumber(float) render the value with String.valueOf(...). For non-finite values that yields Java's textual forms NaN, Infinity, and -Infinity, which are not valid TOML floats. As a result the writer produces output it cannot read back — TomlMapper.readTree(...) on the writer's own output throws TomlStreamReadException: Unknown token.

Change

Map non-finite values to the TOML float tokens nan / inf / -inf (a small shared helper), leaving finite values formatted exactly as before. This is a writer-only change: the parser already accepts nan, inf, +inf, and -inf (Parser line ~389), so written non-finite values now round-trip with no reader change.

Tests

Added to TomlGeneratorTest:

  • nonFiniteDoubles / nonFiniteFloats — assert exact output abc = nan|inf|-inf
  • nonFiniteRoundTrip — writes NaN/±Infinity and reads them back through the same TomlMapper

All three fail before the change (two assertion failures plus a Unknown token error — the exact symptom in the issue) and pass after.

Verification done: ./mvnw -pl toml -am test -Dtest=TomlGeneratorTest — 23 tests pass with the fix; reverting only the TomlGenerator change reproduces 2 failures + 1 Unknown token error.

TomlGenerator wrote non-finite doubles/floats via String.valueOf(), emitting
Java's NaN/Infinity/-Infinity, which are not valid TOML and cannot be parsed
back by the reader (fails with 'Unknown token'). Emit the TOML float tokens
nan/inf/-inf instead so writer output round-trips.
@cowtowncoder

Copy link
Copy Markdown
Member

Happy to merge if and when @yawkat approves.

@cowtowncoder cowtowncoder added the cla-received PR already covered by CLA (optional label) label Jul 2, 2026
@yawkat
yawkat merged commit 8345d3f into FasterXML:2.x Jul 2, 2026
4 checks passed
@yawkat

yawkat commented Jul 2, 2026

Copy link
Copy Markdown
Member

Thanks!

@cowtowncoder

Copy link
Copy Markdown
Member

@yawkat @seonwooj0810 Going forward, we should update release-notes/VERSION-2.x (and maybe CREDITS-2.x) as part of pr.

@yawkat

yawkat commented Jul 2, 2026

Copy link
Copy Markdown
Member

Sorry, keep forgetting 😅

@cowtowncoder

Copy link
Copy Markdown
Member

@yawkat that happens no sweat. I can fix when I get home and can commit changes.

@yawkat

yawkat commented Jul 2, 2026

Copy link
Copy Markdown
Member

Added follow-up release-notes/credits PR: #698

@cowtowncoder cowtowncoder added this to the 2.23.0 milestone Jul 2, 2026
@cowtowncoder cowtowncoder added the TOML Issue related to TOML format backend label Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-received PR already covered by CLA (optional label) TOML Issue related to TOML format backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants