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?
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.
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.
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.
Looks like a bug to me. Please report to the issue tracker.
As a temporary workaround, you can write this class in Java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With