Thread
#serialization
    m

    marstran

    2 years ago
    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?
    Dominaezzz

    Dominaezzz

    2 years ago
    Yes! Let me find you an example!
    m

    marstran

    2 years ago
    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:
    @Serializer(forClass = ColourEffect::class)
    object ColourEffectSerializer {}
    
    @Serializable
    data class Wrapper(@Serializable(with = ColourEffectSerializer::class) val colourEffect: ColourEffect)
    And ColourEffect looks like this:
    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?
    Dominaezzz

    Dominaezzz

    2 years ago
    Hmm, no idea.
    m

    marstran

    2 years ago
    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

    Nagarajan

    2 years ago
    I was also facing same issue. Try UseSerializer file annotation that's kinda solved the issue
    m

    marstran

    2 years ago
    @Nagarajan Oh, thanks. I’ll try that.