Skip to content

Commit

Permalink
Replace gson converter with kotlinx serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Atick authored and atick-faisal committed Oct 12, 2024
1 parent 0aa9871 commit c6f48c4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ okhttp-bom = { group = "com.squareup.okhttp3", name = "okhttp-bom", version.ref
okhttp-core = { group = "com.squareup.okhttp3", name = "okhttp" }
okhttp-logging = { group = "com.squareup.okhttp3", name = "logging-interceptor" }
retrofit-core = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
retrofit-converter-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofit" }
retrofit-converter-kotlinx-serialization = { group = "com.squareup.retrofit2", name = "converter-kotlinx-serialization", version.ref = "retrofit" }
coil-kt = { group = "io.coil-kt", name = "coil", version.ref = "coil" }
coil-kt-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
coil-kt-svg = { group = "io.coil-kt", name = "coil-svg", version.ref = "coil" }
Expand Down
2 changes: 1 addition & 1 deletion network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {

// ... Retrofit
implementation(libs.retrofit.core)
implementation(libs.retrofit.converter.gson)
implementation(libs.retrofit.converter.kotlinx.serialization)

// ... Coil
implementation(libs.coil.kt)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.atick.network.di.retrofit

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Converter
import retrofit2.converter.kotlinx.serialization.asConverterFactory
import javax.inject.Singleton

/**
* Module for providing [Converter.Factory].
*/
@Module
@InstallIn(SingletonComponent::class)
object ConverterModule {
/**
* Provides kotlinx.serialization [Converter.Factory].
*
* @return [Converter.Factory].
*/
@Provides
@Singleton
fun provideConverter(): Converter.Factory {
val json = Json {
ignoreUnknownKeys = true
}
return json.asConverterFactory(
"application/json; charset=UTF8".toMediaType(),
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import dagger.hilt.components.SingletonComponent
import dev.atick.network.BuildConfig
import dev.atick.network.di.okhttp.OkHttpClientModule
import okhttp3.OkHttpClient
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

/**
Expand All @@ -41,14 +41,14 @@ object RetrofitModule {
/**
* Provides [Retrofit].
*
* @param converterFactory [GsonConverterFactory].
* @param converterFactory [Converter.Factory].
* @param okHttpClient [OkHttpClient].
* @return [Retrofit].
*/
@Singleton
@Provides
fun provideRetrofitClient(
converterFactory: GsonConverterFactory,
converterFactory: Converter.Factory,
okHttpClient: OkHttpClient,
): Retrofit {
return Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.atick.network.models

import kotlinx.serialization.Serializable

/**
* Data class representing a network post retrieved from a remote source.
*
Expand All @@ -24,6 +26,7 @@ package dev.atick.network.models
* @property url The URL associated with the network post.
* @property thumbnailUrl The URL of the thumbnail image associated with the network post.
*/
@Serializable
data class NetworkPost(
val id: Int,
val title: String,
Expand Down

0 comments on commit c6f48c4

Please sign in to comment.