Enum annotations in Kotlin

后端 未结 2 1006
有刺的猬
有刺的猬 2021-01-12 17:27

I have an enumeration that is serialized/deserialized by Gson:

enum class PacketType {
    NONE;
    [SerializedName(\"request\")]
    REQUEST;
    [Serializ         


        
相关标签:
2条回答
  • 2021-01-12 17:53

    Looks like a bug to me. Please report to the issue tracker.

    As a temporary workaround, you can write this class in Java

    0 讨论(0)
  • 2021-01-12 18:03

    The issue is now fixed, your code now works fine in Kotlin M9 (0.9.66). If you upgrade to that it'll work as you expect.

    e.g.

    app build.gradle

    dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'org.jetbrains.kotlin:kotlin-stdlib:0.9.66'
     compile 'com.google.code.gson:gson:2.3'
    }
    

    top-level build.gradle

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.13.2'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.+'
     }
    }
    

    I confirmed this by making an enum with no relation between the enum names and SerializedName names, and it worked as expected.

    0 讨论(0)
提交回复
热议问题