From eea73bdd4d163c4772bfdf7669c6391e589817c3 Mon Sep 17 00:00:00 2001 From: Debashish Kundu Date: Mon, 2 Sep 2024 20:33:06 +0530 Subject: [PATCH] migrate from moshi to kotlinx serialization --- app/build.gradle.kts | 5 +- .../com/debk007/template/di/NetworkModule.kt | 8 +- .../com/debk007/template/model/Dimensions.kt | 16 ++-- .../java/com/debk007/template/model/Meta.kt | 20 ++--- .../template/model/ProductDetailsDto.kt | 82 +++++++++---------- .../java/com/debk007/template/model/Review.kt | 22 ++--- build.gradle.kts | 1 + gradle/libs.versions.toml | 7 +- 8 files changed, 84 insertions(+), 77 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0ff7cb8..75a97c0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -6,6 +6,7 @@ plugins { alias(libs.plugins.dagger.hilt) alias(libs.plugins.ksp) alias(libs.plugins.compose.compiler) + alias(libs.plugins.kotlinx.serialization) } android { @@ -88,8 +89,8 @@ dependencies { debugImplementation(libs.ui.tooling) debugImplementation(libs.ui.test.manifest) implementation(libs.retrofit) - implementation(libs.moshi.converter) - ksp(libs.moshi.kotlin.codegen) + implementation(libs.kotlinx.serialization.converter) + implementation(libs.kotlinx.serialization.json) implementation(libs.glide) implementation(libs.coroutines.android) implementation(libs.coroutines.core) diff --git a/app/src/main/java/com/debk007/template/di/NetworkModule.kt b/app/src/main/java/com/debk007/template/di/NetworkModule.kt index 388eac6..cb476b2 100644 --- a/app/src/main/java/com/debk007/template/di/NetworkModule.kt +++ b/app/src/main/java/com/debk007/template/di/NetworkModule.kt @@ -13,7 +13,9 @@ import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.components.SingletonComponent import okhttp3.OkHttpClient import retrofit2.Retrofit -import retrofit2.converter.moshi.MoshiConverterFactory +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaType +import retrofit2.converter.kotlinx.serialization.asConverterFactory import javax.inject.Singleton @Module @@ -30,7 +32,9 @@ object NetworkModule { @Singleton fun providesRetrofit(okHttpClient: OkHttpClient): ApiService = Retrofit.Builder() .baseUrl(BuildConfig.BASE_URL) - .addConverterFactory(MoshiConverterFactory.create()) + .addConverterFactory( + Json.asConverterFactory( + "application/json; charset=UTF8".toMediaType())) .client(okHttpClient) .build() .create(ApiService::class.java) diff --git a/app/src/main/java/com/debk007/template/model/Dimensions.kt b/app/src/main/java/com/debk007/template/model/Dimensions.kt index 426bb2d..d88297e 100644 --- a/app/src/main/java/com/debk007/template/model/Dimensions.kt +++ b/app/src/main/java/com/debk007/template/model/Dimensions.kt @@ -1,15 +1,15 @@ package com.debk007.template.model -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable -@JsonClass(generateAdapter = true) +@Serializable data class Dimensions( - @Json(name = "width") - val width: Double, - @Json(name = "height") + @SerialName("depth") + val depth: Double, + @SerialName("height") val height: Double, - @Json(name = "depth") - val depth: Double + @SerialName("width") + val width: Double ) \ No newline at end of file diff --git a/app/src/main/java/com/debk007/template/model/Meta.kt b/app/src/main/java/com/debk007/template/model/Meta.kt index f5754e8..4cf9aec 100644 --- a/app/src/main/java/com/debk007/template/model/Meta.kt +++ b/app/src/main/java/com/debk007/template/model/Meta.kt @@ -1,17 +1,17 @@ package com.debk007.template.model -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable -@JsonClass(generateAdapter = true) +@Serializable data class Meta( - @Json(name = "createdAt") - val createdAt: String, - @Json(name = "updatedAt") - val updatedAt: String, - @Json(name = "barcode") + @SerialName("barcode") val barcode: String, - @Json(name = "qrCode") - val qrCode: String + @SerialName("createdAt") + val createdAt: String, + @SerialName("qrCode") + val qrCode: String, + @SerialName("updatedAt") + val updatedAt: String ) \ No newline at end of file diff --git a/app/src/main/java/com/debk007/template/model/ProductDetailsDto.kt b/app/src/main/java/com/debk007/template/model/ProductDetailsDto.kt index 893ca36..4a27e6f 100644 --- a/app/src/main/java/com/debk007/template/model/ProductDetailsDto.kt +++ b/app/src/main/java/com/debk007/template/model/ProductDetailsDto.kt @@ -1,53 +1,53 @@ package com.debk007.template.model -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable -@JsonClass(generateAdapter = true) +@Serializable data class ProductDetailsDto( - @Json(name = "id") - val id: Int, - @Json(name = "title") - val title: String, - @Json(name = "description") - val description: String, - @Json(name = "category") + @SerialName("availabilityStatus") + val availabilityStatus: String, + @SerialName("brand") + val brand: String, + @SerialName("category") val category: String, - @Json(name = "price") - val price: Double, - @Json(name = "discountPercentage") + @SerialName("description") + val description: String, + @SerialName("dimensions") + val dimensions: Dimensions, + @SerialName("discountPercentage") val discountPercentage: Double, - @Json(name = "rating") + @SerialName("id") + val id: Int, + @SerialName("images") + val images: List, + @SerialName("meta") + val meta: Meta, + @SerialName("minimumOrderQuantity") + val minimumOrderQuantity: Int, + @SerialName("price") + val price: Double, + @SerialName("rating") val rating: Double, - @Json(name = "stock") + @SerialName("returnPolicy") + val returnPolicy: String, + @SerialName("reviews") + val reviews: List, + @SerialName("shippingInformation") + val shippingInformation: String, + @SerialName("sku") + val sku: String, + @SerialName("stock") val stock: Int, - @Json(name = "tags") + @SerialName("tags") val tags: List, - @Json(name = "brand") - val brand: String, - @Json(name = "sku") - val sku: String, - @Json(name = "weight") - val weight: Int, - @Json(name = "dimensions") - val dimensions: Dimensions, - @Json(name = "warrantyInformation") + @SerialName("thumbnail") + val thumbnail: String, + @SerialName("title") + val title: String, + @SerialName("warrantyInformation") val warrantyInformation: String, - @Json(name = "shippingInformation") - val shippingInformation: String, - @Json(name = "availabilityStatus") - val availabilityStatus: String, - @Json(name = "reviews") - val reviews: List, - @Json(name = "returnPolicy") - val returnPolicy: String, - @Json(name = "minimumOrderQuantity") - val minimumOrderQuantity: Int, - @Json(name = "meta") - val meta: Meta, - @Json(name = "images") - val images: List, - @Json(name = "thumbnail") - val thumbnail: String + @SerialName("weight") + val weight: Int ) \ No newline at end of file diff --git a/app/src/main/java/com/debk007/template/model/Review.kt b/app/src/main/java/com/debk007/template/model/Review.kt index 43523f5..b89a2a5 100644 --- a/app/src/main/java/com/debk007/template/model/Review.kt +++ b/app/src/main/java/com/debk007/template/model/Review.kt @@ -1,19 +1,19 @@ package com.debk007.template.model -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable -@JsonClass(generateAdapter = true) +@Serializable data class Review( - @Json(name = "rating") - val rating: Int, - @Json(name = "comment") + @SerialName("comment") val comment: String, - @Json(name = "date") + @SerialName("date") val date: String, - @Json(name = "reviewerName") - val reviewerName: String, - @Json(name = "reviewerEmail") - val reviewerEmail: String + @SerialName("rating") + val rating: Int, + @SerialName("reviewerEmail") + val reviewerEmail: String, + @SerialName("reviewerName") + val reviewerName: String ) \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 990cfe7..1d295b4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,4 +5,5 @@ plugins { alias(libs.plugins.dagger.hilt) apply false alias(libs.plugins.ksp) apply false alias(libs.plugins.compose.compiler) apply false + alias(libs.plugins.kotlinx.serialization) apply false } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 28a019a..4b27a5d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ espresso-core = "3.6.1" lifecycle-runtime-ktx = "2.8.4" activity-compose = "1.9.1" compose-bom = "2024.08.00" -moshi-kotlin-codegen = "1.15.1" +kotlinx-serialization-json = "1.7.2" retrofit = "2.11.0" coroutines = "1.8.1" hilt-navigation-compose = "1.2.0" @@ -26,7 +26,7 @@ espresso-core = { group = "androidx.test.espresso", name = "espresso-core", vers lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle-runtime-ktx" } activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" } compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" } -moshi-kotlin-codegen = { module = "com.squareup.moshi:moshi-kotlin-codegen", version.ref = "moshi-kotlin-codegen" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" } ui = { group = "androidx.compose.ui", name = "ui" } ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } @@ -41,7 +41,7 @@ coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines- # Retrofit retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" } -moshi-converter = { group = "com.squareup.retrofit2", name = "converter-moshi", version.ref = "retrofit" } +kotlinx-serialization-converter = { group = "com.squareup.retrofit2", name = "converter-kotlinx-serialization", version.ref = "retrofit" } # Dagger Hilt hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "dagger-hilt" } @@ -64,6 +64,7 @@ org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.re dagger-hilt = { id = "com.google.dagger.hilt.android", version.ref = "dagger-hilt" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } +kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } [bundles]