darkmoon_uk
12/30/2024, 6:34 AMkotlinx.serialization
is that, in many cases, we can use:
• 👁️ JSON in Debug builds for observability, then almost effortlessly switch to...
• ⚡ CBOR for Production, to gain the time/space optimisation afforded by that format
However; the StringFormat
and BinaryFormat
interfaces these conform to are disjoint - only the SerialFormat
interface is common, with no common encode/decode functions.
To some degree this is understandable - one works with Strings and the other Bytes, so we'd expect them to be optimised for each.
But; there's surely an opportunity to make the switch a bit more seamless, something like this; but out-of-the-box:
fun SerialFormat.encode(obj: Any): ByteArray = when (this) {
is BinaryFormat -> encodeToByteArray(obj)
is StringFormat -> encodeToString(obj).encodeToByteArray()
else -> throw IllegalArgumentException("Unsupported format: $this")
}
inline fun <reified T:Any> SerialFormat.decode(byteArray: ByteArray): T = when (this) {
is BinaryFormat -> decodeFromByteArray(byteArray)
is StringFormat -> decodeFromString(byteArray.decodeToString())
else -> throw IllegalArgumentException("Unsupported format: $this")
}
Thoughts?CLOVIS
12/30/2024, 8:58 AMBinaryFormat
and StringFormat
that implement itdarkmoon_uk
12/30/2024, 9:01 AMephemient
12/30/2024, 6:32 PMephemient
12/30/2024, 6:33 PM