How to convert a Data Class to ByteBuffer in Kotlin?
Asked Answered
C

4

5

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 )

Chrono answered 7/2, 2019 at 7:17 Comment(0)
C
1

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?,
Chrono answered 14/2, 2019 at 9:3 Comment(1)
easiest solutionFichtean
H
8

Check the below method

fun toByteArray(): ByteArray? {
val size: Int = 8 + 8 + string.Size

val byteBuffer = ByteBuffer.allocate(size)
        .put(long) //long veriable 
        .put(double) // double veriable 
        .put(string)


   return byteBuffer.array()
}

You can allocate the size based on dataType size like Int 4 bytes, Double and Long 8 bytes

for reading back to dataType

  val byteBuffer = ByteBuffer.wrap(byteArray)
        byteBuffer.get(Int) //Int variable
        byteBuffer.get(Double) //Double variable
        byteBuffer.get(nonce)
Hols answered 7/2, 2019 at 7:35 Comment(0)
S
3

You might want to have a look at kotlinx.serialization. It is an official Kotlin project and supports several formats out-of-the-box. You can use the output and wrap it in with ByteBuffer.wrap

Singlehanded answered 7/2, 2019 at 19:13 Comment(1)
Tried it, got error on LocalDateTime serialization.Chrono
C
1

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?,
Chrono answered 14/2, 2019 at 9:3 Comment(1)
easiest solutionFichtean
P
0

A simple solution with no additional libraries

Note: must be tailored for each data class

data class TimerConfig(val startTime: Long, val repeatCount: Int, val sequenceDuration: Int)

Converting the data class to a ByteArray

private fun TimerConfig.toByteArray(): ByteArray {
    val byteBuffer = ByteBuffer.allocate(Long.SIZE_BYTES + Int.SIZE_BYTES + Int.SIZE_BYTES)
    byteBuffer.putLong(this.startTime)
    byteBuffer.putInt(this.repeatCount)
    byteBuffer.putInt(this.sequenceDuration)
    return byteBuffer.array()
}

Recovering the data class from the received ByteArray

private fun ByteArray.toTimerConfig(): TimerConfig {
    val byteBuffer = ByteBuffer.wrap(this)
    return TimerConfig(byteBuffer.long, byteBuffer.int, byteBuffer.int)
}
Paulie answered 24/1, 2023 at 1:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.