Skip to content

JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES not working with ObjectMapper.valueToTree() #5819

@pankajxeine

Description

@pankajxeine

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I have migrated code from jackson2 to jackson 3 and tried to trail BigDeciaml with zeroes using jackson 3x but it is not working with below configuration as suggested in document .
Jackson 2 Code (Working fine with jackson 2)

@Bean
public Jackson2ObjectMapperBuilderCustomizer customMapper () {
return builder -> builder.postConfigurer(objectMapper -> 
objectMapper.setNodeFactory(JsonNodeFactory.withExactBigdecimals(true)))
}

Jackson-databind 3.0.4 Code (output is different)

@Bean 
JsonMapperBuilderCustomizer customMapper () {
return builder-> builder
.enable(JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROES)
.enable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN);

BigDecimalWrapper body = new BigDecimalWrapper(new BigDecimal("1.000"), new BigDecimal(2.000), new BigDecimal(3000l));

ResponseEntity.ok(objectMapper.valueToTree(body);
Expected :
output : {bd1: 1, bd2: 2, bd3: 3)

Actual
output: {bd1: 1.000, bd2:2, bd3: 3}

I have tried with simple java code as well but still getting same result.

public class MainApp {

public static void main(String[] args) throws JacksonException {
ObjectMapper mapper = JsonMapper.builder()
.enable((JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROES)
.enable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN)
.build();
BigDecimalWrapper body = new BigDecimalWrapper(new BigDecimal("1.000"), new BigDecimal(2.000), new BigDecimal(3000l));

System.out.println(mapper.valueToTree(body)); // output:  {bd1: 1.000, bd2:2, bd3: 3}

}

Version Information

3.0.4

Reproduction

<-- Any of the following

  1. Configure ObjectMapper with enable(JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROES)
  2. call mapper.valueToTree(body)
  3. (JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROES does not take effect with jackson 3x.
  4. I observed this issue only occurred when we create BigDecimal with String literal e.g. new BigDecimal("1.000")
    -->
public class MainApp {

public static void main(String[] args) throws JacksonException {
ObjectMapper mapper = JsonMapper.builder()
.enable((JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROES)
.enable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN)
.build();
BigDecimalWrapper body = new BigDecimalWrapper(new BigDecimal("1.000"), new BigDecimal(2.000), new BigDecimal(3000l));

System.out.println(mapper.valueToTree(body)); // output:  {bd1: 1.000, bd2:2, bd3: 3}

}

Class BigDecimalWrapper  {
BigDecimal bd1;
BigDecimal bd2;
BigDecimal bd3;

BigDecimalWrapper (BigDecimal bd1, BigDecimal bd2, BigDecimal bd3) {
this.bd1 = bd1;
this.bd2 = bd2;
this.bd3 = bd3;
}
}

Expected behavior

Expected behavior is Bigdecimal 0 should be trailed if JsonNodeFeatue.STRIP_TRAILING_BIGDECIMAL_ZEROE enabled as default it is disabled.

output: {bd1: 1.000, bd2:2, bd3: 3}

Additional context

Nothing

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssues to be only tackled for Jackson 3.x, not 2.x

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions