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

Enum serialization is not working #63

Closed
Galarzaa90 opened this issue Dec 16, 2021 · 1 comment · Fixed by #64
Closed

Enum serialization is not working #63

Galarzaa90 opened this issue Dec 16, 2021 · 1 comment · Fixed by #64
Labels
bug Something isn't working

Comments

@Galarzaa90
Copy link

Galarzaa90 commented Dec 16, 2021

Version

  • Library version: 0.4.2
  • Platform info: Kotlin 1.6.0

Describe the bug
Trying to serialize a object with enums fails with the following error:

SerializationException: Non-serializable class kotlin.Int is not supported by class com.ensarsarajcic.kotlinx.serialization.msgpack.internal.BasicMsgPackEncoder encoder

This is an example enum:

enum class Vocation(val properName: String) {
    NONE("None"),
    DRUID("Druid"),
    SORCERER("Sorcerer"),
    PALADIN("Paladin"),
    KNIGHT("Knight"),
    ELDER_DRUID("Elder Druid"),
    MASTER_SORCERER("Master Sorcerer"),
    ROYAL_PALADIN("Royal Paladin"),
    ELITE_KNIGHT("Elite Knight");

    companion object {
        fun fromProperName(properName: String): Vocation? {
            return values().firstOrNull { it.properName.equals(properName, true) }
        }
    }
}

EnumSerializer from kotlinx-serialization-core-jvm is being used by default. Following it on the debugger, it all goes well until the last line of EnumSerializer.serialize:

override fun serialize(encoder: Encoder, value: T) {
    val index = values.indexOf(value)
    if (index == -1) {
        throw SerializationException(
            "$value is not a valid enum ${descriptor.serialName}, " +
                    "must be one of ${values.contentToString()}"
        )
    }
    encoder.encodeEnum(descriptor, index) //here
}

Where encoder is an instance of BasicMsgPackEncoder. I see that encodeEnum is not implemented in BasicMsgPackEncoder, so that could be part of the issue?

Expected behavior
The enum value should be serialized by its value's name (as the JSON serializer does), e.g. DRUID, KNIGHT, etc.

The same object can be serialized without problems using: Json.encodeToString.

@Galarzaa90 Galarzaa90 added the bug Something isn't working label Dec 16, 2021
esensar added a commit that referenced this issue Dec 17, 2021
esensar added a commit that referenced this issue Dec 17, 2021
esensar added a commit that referenced this issue Dec 17, 2021
@esensar
Copy link
Owner

esensar commented Dec 18, 2021

This should be fixed in 0.4.3 which was just released.

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

Successfully merging a pull request may close this issue.

2 participants