Kotlin 中的枚举注释 [英] Enum annotations in Kotlin

查看:27
本文介绍了Kotlin 中的枚举注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

Unfortunately, I noticed that Gson ignores SerializedName annotations and uses upper case names for enum values. I decided to find out why serialization doesn't work as intended and found out that Kotlin drops all annotations for enum values. How can I make these annotations appear in generated bytecode?

推荐答案

在我看来像是一个错误.请向问题跟踪器报告.

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

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

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

这篇关于Kotlin 中的枚举注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆