Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primitive serialization not working? #300

Open
Kadeluxe opened this issue Jun 29, 2022 · 8 comments
Open

Primitive serialization not working? #300

Kadeluxe opened this issue Jun 29, 2022 · 8 comments
Labels
bug Something isn't working frozen help wanted Extra attention is needed

Comments

@Kadeluxe
Copy link

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

@charleskorn
Copy link
Owner

This seems to be a recurring issue (see #30 (comment)), 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 charleskorn added bug Something isn't working help wanted Extra attention is needed labels Jul 5, 2022
@stale
Copy link

stale bot commented Sep 3, 2022

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 stale bot added the stale label Sep 3, 2022
@stale
Copy link

stale bot commented Sep 10, 2022

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

@stale stale bot closed this as completed Sep 10, 2022
@russellbanks
Copy link
Contributor

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))
    }
}

@charleskorn
Copy link
Owner

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 charleskorn reopened this Mar 15, 2023
@stale stale bot removed the stale label Mar 15, 2023
@stale
Copy link

stale bot commented May 15, 2023

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 stale bot added the stale label May 15, 2023
@russellbanks
Copy link
Contributor

@charleskorn Can you freeze this

@stale stale bot removed the stale label May 15, 2023
@OptimumCode
Copy link
Contributor

Hi, @russellbanks. This issue might have been resolved when #568 was closed. Could you please check version 0.61.0 and let us know if you still have the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working frozen help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants