I have a data class in a module that is not depend...
# serialization
m
I have a data class in a module that is not dependent on kotlinx.serialization. Is there an easy way to make that class serializable in another module, without having to manually write the serializer?
d
Yes! Let me find you an example!
m
Cool, thanks 🙂 But for some reason I’m getting this error while compiling: “java.lang.IllegalStateException: Class ColourEffect have constructor parameters which are not properties and therefore it is not serializable automatically”. I did this:
Copy code
@Serializer(forClass = ColourEffect::class)
object ColourEffectSerializer {}

@Serializable
data class Wrapper(@Serializable(with = ColourEffectSerializer::class) val colourEffect: ColourEffect)
And ColourEffect looks like this:
Copy code
data class ColourEffect(
    val u: Int,
    val v: Int
) {

    init {
        require(u in 0..255)
        require(v in 0..255)
    }

}
Any idea why that happens?
d
Hmm, no idea.
m
Almost feels like a bug. I made a data class in the same module and created a serializer for it the same way, and that worked well.
n
I was also facing same issue. Try UseSerializer file annotation that's kinda solved the issue
m
@Nagarajan Oh, thanks. I’ll try that.