在Kotlin中枚举注释
我有一个由Gson序列化/反序列化的枚举:
enum class PacketType { NONE; [SerializedName("request")] REQUEST; [SerializedName("response")] RESPONSE; [SerializedName("event")] EVENT; }
不幸的是,我注意到Gson忽略了SerializedName
注解,并使用枚举值的大写名称。 我决定找出为什么序列化不能按预期工作,并发现Kotlin删除枚举值的所有注释。 我怎样才能使这些注释出现在生成的字节码?
看起来像是一个bug。 请向问题跟踪器报告。
作为临时解决方法,您可以使用Java编写此类
现在这个问题已经修复,你的代码现在可以在Kotlin M9(0.9.66)中正常工作。 如果你升级到它将会像你期望的那样工作。
例如
应用程序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' }
顶级build.gradle
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.13.2' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.+' } }
我通过枚举枚举名称和序列化名称之间没有关系来确认这一点,并按预期工作。