Skip to content

JsonWriter writes NaN and infinite values as invalid JSON - #52477

Merged
wilkinsona merged 2 commits into
spring-projects:4.0.xfrom
b84330808:json-writer-non-finite-numbers
Oct 29, 2026
Merged

wilkinsona merged 2 commits into
spring-projects:4.0.xfrom
b84330808:json-writer-non-finite-numbers

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

JsonValueWriter writes every Number with toString(), so NaN and infinite Double/Float values come out as bare NaN, Infinity and -Infinity, which aren't valid JSON. With structured logging this makes the whole line unparseable. For example, with logging.structured.format.console=ecs:

logger.atInfo().addKeyValue("ratio", Double.NaN).log("Hello");

produces ..."ratio":NaN,....

This writes non-finite Double and Float values as JSON strings ("NaN", "Infinity", "-Infinity"), which is also what Jackson does by default. Other numbers are written as before. I picked strings over null so the value isn't lost, but I can switch to null if you'd prefer.

JsonValueWriterTests.writeWhenNonFiniteNumber fails without the change and passes with it.

I used Claude Code to write this change.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Oct 28, 2026
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Oct 28, 2026
@philwebb philwebb added this to the 4.0.x milestone Oct 28, 2026
b84330808 and others added 2 commits October 29, 2026 09:15
Previously, JsonValueWriter wrote every Number using toString(), so NaN
and infinite Double and Float values were written as bare NaN, Infinity
and -Infinity tokens. These are not valid JSON and, for example, made a
structured log line unparseable when a key-value pair held such a value.

This commit writes non-finite Double and Float values as JSON strings,
matching Jackson's default behavior.

Signed-off-by: Wei-Lun Li <[email protected]>

See spring-projectsgh-52477
@wilkinsona wilkinsona self-assigned this Oct 29, 2026
@wilkinsona
wilkinsona force-pushed the json-writer-non-finite-numbers branch from 9c5f8f3 to 0bce5d0 Compare October 29, 2026 09:17
@wilkinsona wilkinsona modified the milestones: 4.0.x, 4.0.10 Oct 29, 2026
@wilkinsona
wilkinsona merged commit 873231a into spring-projects:4.0.x Oct 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants