I have a Kotlin object that I need converted into a byte array (byte[]). I understand how to convert a String and a series of other kinds of variables into byte[], but I can't find anything on doing this with an object.
Here is what I've tried:
override fun activateQuestion(instructorUserName: String, host: String, port: Int, questionToActivate: MultipleChoiceQuestion) { val socket = DatagramSocket() //This is the problem -- `.toByteArray(...)` only works for Strings val questionToActivateAsByteArray = questionToActivate.toByteArray(Charsets.UTF_8) //send byte[] data val packet = DatagramPacket(questionToActivateAsByteArray, questionToActivateAsByteArray.size, InetAddress.getByName(host), port) socket.send(packet) }
ObjectMapperand convert your object to a string before sending it as bytes.