Skip to content

Commit

Permalink
migrate from moshi to kotlinx serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
its-me-debk007 committed Sep 2, 2024
1 parent 8e0ccbb commit eea73bd
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 77 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/debk007/template/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/com/debk007/template/model/Dimensions.kt
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 10 additions & 10 deletions app/src/main/java/com/debk007/template/model/Meta.kt
Original file line number Diff line number Diff line change
@@ -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
)
82 changes: 41 additions & 41 deletions app/src/main/java/com/debk007/template/model/ProductDetailsDto.kt
Original file line number Diff line number Diff line change
@@ -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<String>,
@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<Review>,
@SerialName("shippingInformation")
val shippingInformation: String,
@SerialName("sku")
val sku: String,
@SerialName("stock")
val stock: Int,
@Json(name = "tags")
@SerialName("tags")
val tags: List<String>,
@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<Review>,
@Json(name = "returnPolicy")
val returnPolicy: String,
@Json(name = "minimumOrderQuantity")
val minimumOrderQuantity: Int,
@Json(name = "meta")
val meta: Meta,
@Json(name = "images")
val images: List<String>,
@Json(name = "thumbnail")
val thumbnail: String
@SerialName("weight")
val weight: Int
)
22 changes: 11 additions & 11 deletions app/src/main/java/com/debk007/template/model/Review.kt
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand All @@ -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" }
Expand All @@ -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]

0 comments on commit eea73bd

Please sign in to comment.