79329063

Date: 2025-01-04 15:16:41
Score: 1
Natty:
Report link

Jackson < version 2.19

Did NOT work: JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN and SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN.

Below fix works:

ObjectMapper objectMapper = new ObjectMapper();
JsonNodeFactory customJsonNodeFactory = new JsonNodeFactory(true);
objectMapper.setNodeFactory(customJsonNodeFactory);

JsonNodeFactory has a constructor which sets the property big decimal exact.

public JsonNodeFactory(boolean bigDecimalExact)
{
    _cfgBigDecimalExact = bigDecimalExact;
}

Reason : Because if this _cfgBigDecimalExact property is false, then it calls BigDecimal.stripTrailingZeros() which converts it to exponent form, hence setting it to true solves our issue.

Jackson > version 2.19

A property was introduced STRIP_TRAILING_BIGDECIMAL_ZEROES. Setting this as false skips BigDecimal.stripTrailingZeros() call.

Reasons:
  • Blacklisted phrase (1): Did NOT work
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Pranav Jandu