Enum annotations in Kotlin

I have an enum that serializes / deserializes Gson:

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

Unfortunately, I noticed that Gson ignores SerializedName annotations and uses uppercase names for enum values. I decided to find out why serialization does not work as intended, and found out that Kotlin leaves all the annotations for the enumeration values. How can I make these annotations in the generated bytecode?

+6
source share
2 answers

Looks like a mistake. Please inform the questionnaire .

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

+2
source

Now the problem is fixed, now your code works fine in Kotlin M9 (0.9.66). If you switch to this, it will work as you expect.

eg.

app 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' } 

top level build.gradle

 buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.13.2' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.+' } } 

I confirmed this by doing an enumeration without any relationship between the enum names and the SerializedName names, and it worked as expected.

+2
source

Source: https://habr.com/ru/post/974151/


All Articles