im trying to serialzie a ` com.soywiz.klock.Year` ...
# serialization
t
im trying to serialzie a
com.soywiz.klock.Year
but doing so breakd code generation... Thats my serializer:
Copy code
class YearSerializer : KSerializer<Year> {

    override val descriptor: SerialDescriptor =
        StringDescriptor.withName("Klock-Year")

    override fun serialize(encoder: Encoder, obj: Year) {
        encoder.encodeInt(obj.year)
    }

    override fun deserialize(decoder: Decoder): Year {
        return Year(decoder.decodeInt())
    }
}
And thats how i use it (Month is an enum class i wrote):
Copy code
@Serializable
data class LocalDate(val dayOfMonth: Int,
                     val month: Month, @Serializable(with = YearSerializer::class) val year: Year) {
    init {
        require(month.isDayOfMonthValid(dayOfMonth, year))
    }
}
s
Is
Year
an inline class?
t
yes
s
Unfortunately, they're not supported
t
oh noes
thank you, i'll look for a workaround
r
@thana did you find any? I was hoping that custom serializer would help avoid this and stumbled upon similar stacktrace. It seems that even specifying custom serializer for field of inline class type is not working with the same error. I've removed
inline
modifier for now, but I'm curious if there is a workaround.
t
No, Not for the actual issue. In my case i could simply use an Int instead of Year. Generally a workaround could be to expect the inline class in the constructor, but store the wrapped value and than instatiate whenever you actually need the behavior of the inline class. As it's an inline class thus shouldn't even be a performance issue or something...
r
Sounds kind of reasonable, but looks like too much work for a temporary solution for me. Thanks for sharing anyway, maybe it would be handy for someone 🙂
👍 1