JsonDeserializer deserialize a bigdecimal returns exponential value

Viewed 17

I'm using Kotlin and I try to deserialize a bigdecimal to an Instant, so here is what I'm doing:

data class Offer(
    @JsonDeserialize(using = InstantTimestampDeserializer::class)
    val startDate: Instant?,
    @JsonDeserialize(using = InstantTimestampDeserializer::class)
    val endDate: Instant?,
)

object InstantTimestampDeserializer : JsonDeserializer<Instant>() {
    fun cleanRawTextValue(rawTextValue: String) = when (rawTextValue.contains(".")) {
        false -> rawTextValue
        true -> rawTextValue.dropLast(rawTextValue.length - rawTextValue.indexOfLast { it == '.' })
    }
    override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Instant {

        val node: JsonNode? = p?.codec?.readTree(p)
        val rawTextValue = node?.asText()

        return Instant.ofEpochMilli(cleanRawTextValue(rawTextValue.toString()).toLong())
    }
}

the value I want to deserialize is 1397458800000.000000000 but this JsonParser read it as 1.3974588E12, how can I let it read as 1397458800000.000000000? thanks!

0 Answers
Related