Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enums Kotlin中的枚举注释_Enums_Annotations_Gson_Kotlin - Fatal编程技术网

Enums Kotlin中的枚举注释

Enums Kotlin中的枚举注释,enums,annotations,gson,kotlin,Enums,Annotations,Gson,Kotlin,我有一个由Gson序列化/反序列化的枚举: enum class PacketType { NONE; [SerializedName("request")] REQUEST; [SerializedName("response")] RESPONSE; [SerializedName("event")] EVENT; } 不幸的是,我注意到Gson忽略了SerializedName注释,并对枚举值使用大写名称。我决定找出序列化不能按预期

我有一个由Gson序列化/反序列化的枚举:

enum class PacketType {
    NONE;
    [SerializedName("request")]
    REQUEST;
    [SerializedName("response")]
    RESPONSE;
    [SerializedName("event")]
    EVENT;
}

不幸的是,我注意到Gson忽略了
SerializedName
注释,并对枚举值使用大写名称。我决定找出序列化不能按预期工作的原因,并发现Kotlin删除了枚举值的所有注释。如何使这些注释显示在生成的字节码中?

对我来说似乎是个bug。请向警察局报告


作为一种临时解决方法,您可以用Java编写此类

问题现在已经解决,您的代码现在可以在Kotlin M9(0.9.66)中正常工作。如果您升级到该级别,它将按您的预期工作

e、 g

应用程序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'
}
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.13.2'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.+'
 }
}
顶级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'
}
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.13.2'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.+'
 }
}

我通过创建一个在枚举名称和序列化名称之间没有关系的枚举来确认这一点,它按照预期工作。

如果您需要在改型中使用enum as@Query param,您可以重写toString():


请参阅下面的答案,这已在Kotlin的后续版本中得到解决,不再是里程碑9之后的问题