I have a data format that needs to support encodin...
# serialization
z
I have a data format that needs to support encoding different types of strings. Some may be UTF8, Windows-1252. Some are prepended with the string length, some strings are null terminated, some have the length after the terminator. Would the best way to handle this be specifying different Serializers in the
with
property of the
@Serializable
annotation?
a
Is the encoding specific per property? So a data class can have one property with UTF-8, and another with Windows-1252? If so, I'd create a custom serializer for each encoding, and use them on encoding-specific typealiases or value classes.
If it's for the entire output (e.g. "I want to encode this data class to a UTF8 string in function X, but to a Win1252 in function Y"), then I'd probably create a specific SerialFormat (based on StringFormat) that has specific
encodeToUtf8()
and
decodeFromWin1252()
functions.
z
Thanks Adam, yes, more along the lines with the first. One string property could be UTF8, another in the same data class could be Windows-1252. I like the typealias strategy, but I'll probably end up using that with value classes, since there are also some other interesting rules in this format, like
01 != 1
, so parsing them as numbers ends up with the wrong values, so it's basically just a digit only string.