Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum annotations in Kotlin

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

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

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?

like image 904
Michael Avatar asked Aug 21 '14 10:08

Michael


People also ask

How do I get Kotlin enum value?

To get an enum constant by its string value, you can use the function valueOf() on the Enum class. It helps us to get enum from a String value in Kotlin.

What are annotations used for Kotlin?

Annotations are used to attach metadata to classes, interface, parameters, and so on at compile time. Annotation can be used by compiler which reflects at runtime. We can change the meaning of the data or program according to annotation values.

What are enums in Kotlin?

Kotlin enums are classes, which means that they can have one or more constructors. Thus, you can initialize enum constants by passing the values required to one of the valid constructors. This is possible because enum constants are nothing other than instances of the enum class itself.


1 Answers

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

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

like image 188
Andrey Breslav Avatar answered Dec 17 '22 08:12

Andrey Breslav