vert.x

Vert.x JsonObject how to make BigDecimal stripTrailingZeros and toPlainString format in toBuffer and toString?


I use Vert.x JsonObject to store BigDecimal object. But I find it does not call BigDecimal's .stripTrailingZeros() and .toPlainString() method by default.

new JsonObject().put("bd", new BigDecimal("11.110")).toString()
// output: {"bd":11.110}

new JsonObject().put("bd", new BigDecimal("9.9E3")).toString()
// output: {"bd":9.9E+3}

I want to get {"bd":11.11} and {"bd":9900}, like BigDecimal .stripTrailingZeros() and .toPlainString().

How ?

Thanks.


Solution

  • First you must retrieve the object mappers used internally by Vert.x.

    Then configure each of them like this:

    objectMapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);