Pain point: a way to minify serial names automatically
See original GitHub issueWhat is your use-case and why do you need this feature? By default, kx.serialization uses full names for enum items and polymorphic types, for field names. There are two cons I see:
- The decoding time is slower because there is a need to compare long strings.
- The size of serialized content is huge.
So in my protocol I have SerialName
s specified almost everywhere to make names as compact as possible (you can take sample repo from #907):
@Serializable
enum class PaintType {
@SerialName("a")
DRAW,
@SerialName("b")
FILL,
}
@Serializable
sealed class ImageId {
@Serializable
@SerialName("a")
data class BufferedImageId(
@SerialName("a")
val identityHash: Int,
@SerialName("b")
val stateHash: Int
) : ImageId()
@Serializable
@SerialName("b")
data class PVolatileImageId(
@SerialName("a")
val id: Long
) : ImageId()
// ...
It’s not convenient to support such code. I can barely see signature of a class. We even have a special test which checks that all serial names in any context form alphabet.
Describe the solution you’d like
I believe a feature to minify serial names automatically should be available.
A parameter for @Serializable
annotation is a good temporary solution. Setting a mask for FQNs of classes that should be minified in Gradle script is even cooler.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
@SerVB Sequential serialization is where you don’t write any names at all. You just write the minimum needed (for polymorphism you still need a discriminator, unfortunately). Of course such serialization is not particularly robust (but neither is using arbitrary letters as names). Formats such as ProtoBuf, Json and XML are designed to be robust and allow for backwards (and forwards) compatibility. Btw. in terms of file/message size, repeating names compress very very well - the main issue would be parsing speed, but for something like that you would go to a binary format anyway.
See also: #33
We do not plan to introduce this feature for now, mainly because of the philosophy “what you see in definition of class in Kotlin code is what you get in the JSON output”. Although minification is an interesting use-case for naming policies, why don’t use binary formats (cbor, protobuf) if one want to minimize output size?