From 4ec3b5c35bf7732ecfd9e3d3da6edd5cda95d611 Mon Sep 17 00:00:00 2001 From: Vadim Shchenev Date: Wed, 4 Sep 2024 21:11:16 +0200 Subject: [PATCH] KotlinX support --- CHANGELOG.md | 3 +++ .../robopojogenerator/models/ControlsModel.kt | 5 +++++ .../postrocessing/common/JavaPostProcessor.kt | 2 ++ .../common/KotlinDataClassPostProcessor.kt | 9 +++++++++ .../properties/annotations/KotlinAnnotations.kt | 5 +++++ .../properties/templates/ImportsTemplate.kt | 5 +++++ .../robopojogenerator/view/PropertiesFactory.kt | 9 ++++++++- main/src/main/resources/META-INF/plugin.xml | 10 +++++----- 8 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab693a..24a3296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.6.0 +- Kotlin Serialisation. + ## 2.5.1 - Minor improvements with dependencies update. diff --git a/core/src/main/kotlin/com/robohorse/robopojogenerator/models/ControlsModel.kt b/core/src/main/kotlin/com/robohorse/robopojogenerator/models/ControlsModel.kt index fe78cf4..80fa388 100644 --- a/core/src/main/kotlin/com/robohorse/robopojogenerator/models/ControlsModel.kt +++ b/core/src/main/kotlin/com/robohorse/robopojogenerator/models/ControlsModel.kt @@ -111,6 +111,10 @@ sealed class FrameworkVW( properties: List = emptyList() ) : FrameworkVW(JAKATRA, properties) + class KotlinX( + properties: List = emptyList() + ) : FrameworkVW(KOTLIN_X, properties) + class JakatraJavaRecords( properties: List = emptyList() ) : FrameworkVW(JAKATRA_JAVA_RECORDS, properties), JavaRecords @@ -131,6 +135,7 @@ sealed class FrameworkVW( const val FAST_JSON = "FastJson" const val FAST_JSON_JAVA_RECORDS = "FastJson (records)" const val JAKATRA = "Jakarta JSON Binding" + const val KOTLIN_X = "KotlinX" const val JAKATRA_JAVA_RECORDS = "Jakarta JSON Binding (records)" } diff --git a/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/JavaPostProcessor.kt b/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/JavaPostProcessor.kt index 606a570..da16378 100644 --- a/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/JavaPostProcessor.kt +++ b/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/JavaPostProcessor.kt @@ -1,5 +1,6 @@ package com.robohorse.robopojogenerator.postrocessing.common +import com.robohorse.robopojogenerator.models.FrameworkVW.KotlinX import com.robohorse.robopojogenerator.models.FrameworkVW.AutoValue import com.robohorse.robopojogenerator.models.FrameworkVW.FastJson import com.robohorse.robopojogenerator.models.FrameworkVW.FastJsonJavaRecords @@ -114,6 +115,7 @@ internal abstract class JavaPostProcessor( } is None, + is KotlinX, is NoneJavaRecords -> { // NO OP } } diff --git a/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/KotlinDataClassPostProcessor.kt b/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/KotlinDataClassPostProcessor.kt index ea9a244..de5162b 100644 --- a/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/KotlinDataClassPostProcessor.kt +++ b/generator/src/main/kotlin/com/robohorse/robopojogenerator/postrocessing/common/KotlinDataClassPostProcessor.kt @@ -84,6 +84,15 @@ internal class KotlinDataClassPostProcessor( ) } + is KotlinX -> { + generateHelper.setAnnotations( + classItem, + KotlinAnnotations.KOTLIN_X.classAnnotation, + KotlinAnnotations.KOTLIN_X.annotation, + ImportsTemplate.KOTLIN_X.imports + ) + } + is LoganSquare -> { generateHelper.setAnnotations( classItem, diff --git a/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/annotations/KotlinAnnotations.kt b/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/annotations/KotlinAnnotations.kt index ef966de..9c83987 100644 --- a/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/annotations/KotlinAnnotations.kt +++ b/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/annotations/KotlinAnnotations.kt @@ -8,6 +8,11 @@ internal sealed class KotlinAnnotations( annotation = "@field:SerializedName(\"%1\$s\")" ) + object KOTLIN_X : KotlinAnnotations( + classAnnotation = "@Serializable", + annotation = "@SerialName(\"%1\$s\")" + ) + object LOGAN_SQUARE : KotlinAnnotations( classAnnotation = "@JsonObject", annotation = "@field:JsonField(name = [\"%1\$s\"])" diff --git a/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/templates/ImportsTemplate.kt b/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/templates/ImportsTemplate.kt index 9021152..e1fef53 100644 --- a/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/templates/ImportsTemplate.kt +++ b/generator/src/main/kotlin/com/robohorse/robopojogenerator/properties/templates/ImportsTemplate.kt @@ -5,6 +5,8 @@ internal sealed class ImportsTemplate( ) { object GSON : ImportsTemplate(arrayOf(SERIALIZED_NAME)) + object KOTLIN_X : ImportsTemplate(arrayOf(KOTLIN_X_SERIALIZABLE, KOTLIN_X_SERIAL_NAME)) + object LOGAN_SQUARE : ImportsTemplate(arrayOf(JSON_OBJECT, JSON_FIELD)) object JACKSON : ImportsTemplate(arrayOf(JSON_PROPERTY)) @@ -49,3 +51,6 @@ internal const val GSON_IMPORT = "import com.google.gson.Gson;" internal const val PARCELABLE_ANDROID = "import android.os.Parcelable" internal const val PARCELIZE_KOTLINX = "import kotlinx.parcelize.Parcelize" + +internal const val KOTLIN_X_SERIALIZABLE = "import kotlinx.serialization.Serializable" +internal const val KOTLIN_X_SERIAL_NAME = "import kotlinx.serialization.SerialName" diff --git a/main/src/main/kotlin/com/robohorse/robopojogenerator/view/PropertiesFactory.kt b/main/src/main/kotlin/com/robohorse/robopojogenerator/view/PropertiesFactory.kt index ec54152..b208947 100644 --- a/main/src/main/kotlin/com/robohorse/robopojogenerator/view/PropertiesFactory.kt +++ b/main/src/main/kotlin/com/robohorse/robopojogenerator/view/PropertiesFactory.kt @@ -11,7 +11,6 @@ import com.robohorse.robopojogenerator.models.AdditionalPropertiesVM.UseMoshiAda import com.robohorse.robopojogenerator.models.AdditionalPropertiesVM.UseSetters import com.robohorse.robopojogenerator.models.AdditionalPropertiesVM.UseToString import com.robohorse.robopojogenerator.models.ControlsModel -import com.robohorse.robopojogenerator.models.FrameworkVW import com.robohorse.robopojogenerator.models.FrameworkVW.Jakatra import com.robohorse.robopojogenerator.models.FrameworkVW.JakatraJavaRecords import com.robohorse.robopojogenerator.models.FrameworkVW.AutoValue @@ -21,6 +20,7 @@ import com.robohorse.robopojogenerator.models.FrameworkVW.Gson import com.robohorse.robopojogenerator.models.FrameworkVW.GsonJavaRecords import com.robohorse.robopojogenerator.models.FrameworkVW.Jackson import com.robohorse.robopojogenerator.models.FrameworkVW.JacksonJavaRecords +import com.robohorse.robopojogenerator.models.FrameworkVW.KotlinX import com.robohorse.robopojogenerator.models.FrameworkVW.LoganSquare import com.robohorse.robopojogenerator.models.FrameworkVW.LoganSquareJavaRecords import com.robohorse.robopojogenerator.models.FrameworkVW.Moshi @@ -165,6 +165,13 @@ internal class PropertiesFactory { UseKotlinParcelable() ) ), + KotlinX( + properties = listOf( + UseKotlinDataClasses(selected = true), + UseKotlinSingleDataClass(selected = true), + UseKotlinNullableFields(selected = true) + ) + ), Gson( properties = listOf( UseKotlinDataClasses(selected = true), diff --git a/main/src/main/resources/META-INF/plugin.xml b/main/src/main/resources/META-INF/plugin.xml index 009f30a..9fc7117 100644 --- a/main/src/main/resources/META-INF/plugin.xml +++ b/main/src/main/resources/META-INF/plugin.xml @@ -1,9 +1,9 @@ com.robohorse.robopojogenerator RoboPOJOGenerator - 2.5.1 - - Minor improvements with dependencies update + 2.6.0 + + Kotlin Serialisation ROBOHORSE @@ -14,8 +14,8 @@ Intellij Idea and Android Studio plugin for JSON to POJO transformation. - Generates Java, Java Records and Kotlin POJO files from JSON: GSON, FastJSON, AutoValue (GSON), Logan Square, Jackson, Lombok, Jakarta JSON Binding, empty annotations' template. Supports: primitive types, multiple inner JSONArrays. - Keywords: JsonToPojo, Json2Pojo, Kotlin, GSON, FastJSON, AutoValue, Jackson, LoganSquare, Moshi, Parcelable, Lombok, Jakarta JSON Binding, Java records. + Generates Java, Java Records and Kotlin POJO files from JSON: GSON, FastJSON, AutoValue (GSON), Logan Square, Jackson, Lombok, Jakarta JSON Binding, KotlinX, empty annotations' template. Supports: primitive types, multiple inner JSONArrays. + Keywords: JsonToPojo, Json2Pojo, Kotlin, GSON, FastJSON, AutoValue, Jackson, LoganSquare, Moshi, Parcelable, Lombok, Jakarta JSON Binding, Java records, KotlinX.