Skip to content

Inconsistent serialisation of enums between Json and Properties parsers #818

@francos

Description

@francos

Describe the bug

I'm trying to deserialize a Map to an object (in JVM). The Map comes from a Firestore DB which I populated from Kotlin/JS using kotlinx.serialization.json.Json.

The data inside the DB contains enums, which by default are serialised as Strings in Json, but the kotlinx.serialization.Properties deserialiser seems to expect them as Integers because it throws the error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

I'm not using any custom KSerializers for enums, just the defaults in each case.

To Reproduce

fun main(args: Array<String>) {
    val values = mapOf("id" to "asfqweertsdsdg", "enumValue" to "VAL_1")

    val result = Properties.loadNullable(TestClass.serializer(), values)
}

@Serializable
data class TestClass(
    val id: String,
    val enumValue: TestEnum
)

enum class TestEnum { VAL_1, VAL_2 }

I also tried making TestEnum explicitly serializable:

@Serializable
enum class TestEnum {
    @SerialName("VAL_1")
    VAL_1,
   @SerialName("VAL_2")
    VAL_2
}

but that also failed.

If I change the map to:

mapOf("id" to "asfqweertsdsdg", "enumValue" to 0)

then the example above works fine.

Expected behavior

Json and Properties should use the same default way to serialise and deserialise enums.

Environment

  • Kotlin version: 1.3.72
  • Library version: 0.20.0
  • Kotlin platforms: JVM and JS
  • Gradle version: 5.6.4

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions