JsonWriter: Write non-finite floating-point values as strings#4080
Merged
JsonWriter: Write non-finite floating-point values as strings#4080
Conversation
This change updates `JsonWriter` to serialize non-finite `float` and `double` values (`NaN`, `Infinity`, and `-Infinity`) as JSON strings. This behavior aligns with Jackson's `JsonWriteFeature#WRITE_NAN_AS_STRINGS`. Previously, these values were written without quotes, which produces **invalid JSON** according to the JSON specification.
vy
approved these changes
Mar 24, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Updates JsonWriter so that non-finite floating-point values (NaN, Infinity, -Infinity) are serialized as JSON strings, producing valid JSON output and aligning behavior with Jackson’s JsonWriteFeature#WRITE_NAN_AS_STRINGS.
Changes:
- Serialize non-finite
float/doublevalues as quoted strings inJsonWriter. - Add parameterized tests covering non-finite
float/doubleserialization. - Add a changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/changelog/.2.x.x/4080_jtl-nan.xml |
Adds changelog entry for the non-finite float/double JSON serialization fix. |
log4j-layout-template-json/.../JsonWriter.java |
Writes non-finite float/double values via writeString(...) instead of emitting invalid JSON numbers. |
log4j-layout-template-json-test/.../JsonWriterTest.java |
Adds parameterized tests asserting non-finite values are quoted. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...mplate-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonWriter.java
Outdated
Show resolved
Hide resolved
...mplate-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonWriter.java
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change updates
JsonWriterto serialize non-finitefloatanddoublevalues (NaN,Infinity, and-Infinity) as JSON strings.This behavior aligns with Jackson's
JsonWriteFeature#WRITE_NAN_AS_STRINGS.Previously, these values were written without quotes, which produces invalid JSON according to the JSON specification.