0

from a server I get an array of bytes, but Kotlinx serializable doesen't seem to like it as it can't parse it to the native Kotlin datatype ByteArray and logs the error Unexpected JSON token at offset 902: Failed to parse 'byte':

JSON = "my_bytes": "[[22, 124, 78, etc...], [233, 89, 112, etc...], [etc...]]" and I wanna serialize that into a ByteArray so I can then write a file with that ByteArray with the File.writeBytes(my_byte_array)

My code by the way:

import kotlinx.serialization.Serializable

@Serializable
data class ServerResponse(
    val audio: List<ByteArray>
)
WiseEye
  • 133
  • 9
  • Can you provide a complete example? For example a main function `fun main() {}` that we can run. – aSemy Oct 22 '22 at 08:22
  • I'm confused because the JSON you've posted isn't an array (it's wrapped in quotes, so it's a string), the property name doesn't match (`my_bytes` vs `audio`), and the JSON contains non-byte values (233 is too large for a byte, the max value is 127). Also, do you have control over how the value is serialized? If so, you might want to [base64 encode the bytes](https://stackoverflow.com/q/64486317/4161471) as a string instead of sending them in an array. – aSemy Oct 22 '22 at 08:35

0 Answers0