kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

fileSerializer not properly recognized by IntelliJ

Open Ademord opened this issue 3 years ago • 1 comments

I have a class that looks like this

@file:UseSerializers(BigDecimalSerializer::class, LocalDateSerializer::class)

@Serializable 
data class MyRecordKeeper(val records: Map<LocalDate, BigDecimal>)

with

// FIXME
// compiler gives error when this class is not in business
// other serializers (BigDecimalSerializer) can stay in technical
// https://youtrack.jetbrains.com/issue/KT-44417

object LocalDateSerializer : KSerializer<LocalDate> {
    override val descriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): LocalDate {
        return LocalDate.parse(decoder.decodeString())
    }

    override fun serialize(encoder: Encoder, value: LocalDate) {
        encoder.encodeString(value.toString())
    }
}

and

object BigDecimalSerializer : KSerializer<BigDecimal> {
    override val descriptor = PrimitiveSerialDescriptor("BigDecimal", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): BigDecimal {
        return decoder.decodeString().toBigDecimal()
    }

    override fun serialize(encoder: Encoder, value: BigDecimal) {
        encoder.encodeString(value.toPlainString())
    }
}

for some reason I get the error: image

Side notes & Qs:

  • the serializers are in a different modules (imagine module1 "business", module2 "technical")
  • to add to my confusion IntelliJ is giving me an error only for LocalDate and not for BigDecimal. But when I move the LocalDateSerializer to the business module, the error is gone.
  • conclusion: building the project works but its the editor that shows there is an error only for this one serializer. what can one do so that LocalDate is not underlined as error and future developers dont think there is something wrong?

Ademord avatar May 11 '22 11:05 Ademord

Does error go away if you annotate LocalDate explicitly? Like Map<@Serializable(LocalDateSerializer::class) LocalDate, BigDecimal>?

sandwwraith avatar May 16 '22 12:05 sandwwraith