Skip to content

Pretty printing and beneathPath change how JSON numbers are documented - #1078

Open
chuuminggg wants to merge 1 commit into
spring-projects:4.0.xfrom
chuuminggg:pretty-print-preserves-numbers
Open

chuuminggg wants to merge 1 commit into
spring-projects:4.0.xfrom
chuuminggg:pretty-print-preserves-numbers

Conversation

@chuuminggg

Copy link
Copy Markdown
Contributor

prettyPrint() and beneathPath(…) read JSON with an ObjectMapper that uses double for floating-point numbers. When the JSON is written back out, some numbers are documented with a different value or representation from the one in the actual request or response:

{"price":10.50,"rate":1e3,"exact":0.1000000000000000055511151231257827,"big":12345678901234567890.123}

becomes:

{
  "price" : 10.5,
  "rate" : 1000.0,
  "exact" : 0.1,
  "big" : 1.2345678901234567E19
}

The same happens to the subsection that's extracted by beneathPath(…), which is used for the body and fields snippets. Trailing zeros are common in monetary amounts (10.50), so the documented payload can easily differ from what the API actually returns. High-precision and large decimal numbers lose precision.

This PR enables DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS on the ObjectMapper that's used for reading in PrettyPrintingContentModifier and FieldPathPayloadSubsectionExtractor, so the numbers are kept as BigDecimal. All of the values above are then written exactly as they were in the input. The only remaining differences are in notation, which doesn't change the value:

  • A number in exponent notation is written as BigDecimal.toString() would write it, for example 1E+3 rather than 1e3. Previously, it became 1000.0.
  • -0.0 is written as 0.0.

Integers aren't affected, as they were already read as int, long, or BigInteger.

Previously, floating-point numbers in JSON content were read as doubles
so pretty printing and beneathPath changed how they were documented.
For example, 10.50 became 10.5 and values that cannot be represented
exactly as a double lost precision. Such numbers are now read as
BigDecimal so that their original value is retained.

Signed-off-by: chuuminggg <rorrxr@naver.com>
@chuuminggg
chuuminggg force-pushed the pretty-print-preserves-numbers branch from 80ae193 to 265b0d5 Compare October 16, 2026 10:37
@wilkinsona wilkinsona added type: bug A bug and removed status: waiting-for-triage Untriaged issue labels Oct 19, 2026
@wilkinsona wilkinsona added this to the 4.0.x milestone Oct 19, 2026

This branch has not been deployed

No deployments
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.

3 participants