kotlinaws-sdkbytebufferamazon-kinesis-kpl

How to convert a Data Class to ByteBuffer in Kotlin?


I am trying to use Kinesis, which expects data in byte buffer format. All the examples I have seen so far are in Java and pass simple strings. Can anybody give an idea of how to convert a kotlin data class to bytebuffer?

e.g. data class abc ( var a: Long, var b: String, var c: Double )


Solution

  • Thanks for all the suggestions.

    Solved the problem using ObjectMapper() of Jackson library (jackson-databind) and annotations. Following code used for serialization:

    val objectMapper = ObjectMapper()
    objectMapper.registerModule(JavaTimeModule())
    val buf = ByteBuffer.wrap(objectMapper.writeValueAsString(className).toByteArray(Charsets.UTF_8))
    

    code for deserialization:

    val objectMapper = ObjectMapper()
    objectMapper.registerModule(JavaTimeModule())
    val obj = objectMapper.readValue(Charsets.UTF_8.decode(record.data()).toString(), ClassName::class.java)
    

    Apart from this, I had to add constructors of all the data classes and had to add the following annotation to all the LocalDateTime attributes:

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @JsonFormat(pattern = "YYYY-MM-dd HH:mm")
    var edd: LocalDateTime?,