kaml icon indicating copy to clipboard operation
kaml copied to clipboard

Primitive serialization not working?

Open Kadeluxe opened this issue 2 years ago • 7 comments

Describe the bug

Primitive deserialization is not working.

Reproduction repo

This serializer class works for JSON but not for YAML.

@Serializer(forClass = Duration::class)
object DurationSerializer : KSerializer<Duration> {
  override val descriptor = PrimitiveSerialDescriptor("Duration", PrimitiveKind.STRING)
  override fun serialize(encoder: Encoder, value: Duration) = encoder.encodeString(value.toString())
  override fun deserialize(decoder: Decoder): Duration = Duration.parse(decoder.decodeString())
}

Steps to reproduce

Create simple primitive serializer as described in docs and try to deserialize a value.

Expected behaviour

Same behaviour as in JSON.

It works if I change deserializer to this:

  override fun deserialize(decoder: Decoder): Duration {
    val t = (decoder.beginStructure(descriptor) as AbstractDecoder)
    return Duration.parse(t.decodeString().also { t.endStructure(descriptor) })
  }

but this way is not compatible with JSON and doesn't seem clean in general.

Actual behaviour

java.lang.IllegalStateException: Must call beginStructure() and use returned Decoder
        at com.charleskorn.kaml.YamlContextualInput.decodeValue(YamlContextualInput.kt:29) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeString(AbstractDecoder.kt:34) ~[?:?]
        at com.charleskorn.kaml.YamlMapLikeInputBase$decodeString$1.invoke(YamlMapLikeInputBase.kt:39) ~[?:?]
        at com.charleskorn.kaml.YamlMapLikeInputBase$decodeString$1.invoke(YamlMapLikeInputBase.kt:39) ~[?:?]
        at com.charleskorn.kaml.YamlMapLikeInputBase.fromCurrentValue(YamlMapLikeInputBase.kt:52) ~[?:?]
        at com.charleskorn.kaml.YamlMapLikeInputBase.decodeString(YamlMapLikeInputBase.kt:39) ~[?:?]
        ...
        at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:260) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
        at com.charleskorn.kaml.YamlInput.decodeSerializableValue(YamlInput.kt:103) ~[?:?]
        at kotlinx.serialization.ContextualSerializer.deserialize(ContextualSerializer.kt:67) ~[?:?]
        at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:260) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
        at com.charleskorn.kaml.YamlInput.decodeSerializableValue(YamlInput.kt:103) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70) ~[?:?]
        ...
        at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:260) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
        at com.charleskorn.kaml.YamlInput.decodeSerializableValue(YamlInput.kt:103) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70) ~[?:?]
        ...
        at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:260) ~[?:?]
        at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16) ~[?:?]
        at com.charleskorn.kaml.YamlInput.decodeSerializableValue(YamlInput.kt:103) ~[?:?]
        at com.charleskorn.kaml.Yaml.decodeFromReader(Yaml.kt:55) ~[?:?]
        at com.charleskorn.kaml.Yaml.decodeFromStream(Yaml.kt:48) ~[?:?]

Version information

0.46.0

Any other information

No response

Kadeluxe avatar Jun 29 '22 16:06 Kadeluxe

This seems to be a recurring issue (see https://github.com/charleskorn/kaml/issues/30#issuecomment-932688945), so I'd love to fix this.

Unfortunately, I don't have the time to do this myself right now. If you're interested in submitting a PR to fix the issue, I'd be more than happy to talk through your ideas, provide early feedback etc. to help you.

charleskorn avatar Jul 05 '22 09:07 charleskorn

This issue has been automatically marked as stale because it has not had any activity in the last 60 days. It will automatically be closed if no further activity occurs in the next seven days to enable maintainers to focus on the most important issues. If this issue is still affecting you, please comment below within the next seven days. Thank you for your contributions.

stale[bot] avatar Sep 03 '22 10:09 stale[bot]

This issue has been automatically closed because it has not had any recent activity.

stale[bot] avatar Sep 10 '22 10:09 stale[bot]

Could this be re-opened @charleskorn? I currently have to have twice the serializers as I should do in my project as I have to have separate ones for Json compared to Yaml:

Json:

object JsonLocalDateSerializer : KSerializer<LocalDate> {
    override val descriptor = PrimitiveSerialDescriptor(LocalDate::class.simpleName as String, PrimitiveKind.STRING)

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

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

Yaml:

object YamlLocalDateSerializer : KSerializer<LocalDate> {
    override val descriptor = PrimitiveSerialDescriptor(LocalDate::class.simpleName as String, PrimitiveKind.STRING)

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

    override fun deserialize(decoder: Decoder): LocalDate {
        return LocalDate.parse(decoder.beginStructure(descriptor).decodeStringElement(descriptor, 0))
    }
}

russellbanks avatar Mar 14 '23 23:03 russellbanks

Happy to reopen this, but I do not have time to work on this myself. I'd happily review a PR if you (or anyone else) is keen to see this happen.

charleskorn avatar Mar 15 '23 08:03 charleskorn

This issue has been automatically marked as stale because it has not had any activity in the last 60 days. It will automatically be closed if no further activity occurs in the next seven days to enable maintainers to focus on the most important issues. If this issue is still affecting you, please comment below within the next seven days. Thank you for your contributions.

stale[bot] avatar May 15 '23 10:05 stale[bot]

@charleskorn Can you freeze this

russellbanks avatar May 15 '23 14:05 russellbanks