mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 20:17:02 +05:30
Replace Moshi with kotlinx.serialization
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
43295a58eb
commit
84f45f523a
10 changed files with 40 additions and 52 deletions
|
@ -4,6 +4,7 @@ plugins {
|
|||
id("com.android.application")
|
||||
kotlin("android")
|
||||
kotlin("kapt")
|
||||
kotlin("plugin.serialization") version "1.4.10"
|
||||
id("dagger.hilt.android.plugin")
|
||||
`lobsters-plugin`
|
||||
`core-library-desugaring`
|
||||
|
@ -90,9 +91,9 @@ dependencies {
|
|||
implementation(Dependencies.ThirdParty.Roomigrant.runtime)
|
||||
implementation(Dependencies.AndroidX.material)
|
||||
implementation(Dependencies.AndroidX.Hilt.dagger)
|
||||
implementation(Dependencies.ThirdParty.Moshi.lib)
|
||||
implementation(Dependencies.ThirdParty.accompanist)
|
||||
implementation(Dependencies.Kotlin.Coroutines.android)
|
||||
implementation(Dependencies.Kotlin.Serialization.json)
|
||||
implementation(Dependencies.ThirdParty.customtabs)
|
||||
androidTestImplementation(Dependencies.Testing.daggerHilt)
|
||||
testImplementation(Dependencies.Testing.junit)
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
package dev.msfjarvis.lobsters.data.source
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.squareup.moshi.Moshi
|
||||
import dev.msfjarvis.lobsters.model.Submitter
|
||||
import dev.msfjarvis.lobsters.model.SubmitterJsonAdapter
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
object LobstersApiTypeConverters {
|
||||
private val moshi = Moshi.Builder().build()
|
||||
private const val SEPARATOR = ","
|
||||
|
||||
@TypeConverter
|
||||
@JvmStatic
|
||||
fun toSubmitterUser(value: String?): Submitter? {
|
||||
return value?.let { SubmitterJsonAdapter(moshi).fromJson(value) }
|
||||
return value?.let { Json.decodeFromString(value) }
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
@JvmStatic
|
||||
fun fromSubmitterUser(value: Submitter?): String? {
|
||||
return value?.let { SubmitterJsonAdapter(moshi).toJson(value) }
|
||||
return value?.let { Json.encodeToString(value) }
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
|
|
|
@ -136,7 +136,7 @@ fun PreviewLobstersItem() {
|
|||
"sevan",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
emptyList(),
|
||||
),
|
||||
listOf("openbsd"),
|
||||
)
|
||||
|
|
|
@ -79,18 +79,10 @@ object Dependencies {
|
|||
const val customtabs = "saschpe.android:customtabs:3.0.2"
|
||||
const val retrofitSerialization = "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
|
||||
|
||||
object Moshi {
|
||||
|
||||
private const val version = "1.11.0"
|
||||
const val codegen = "com.squareup.moshi:moshi-kotlin-codegen:$version"
|
||||
const val lib = "com.squareup.moshi:moshi:$version"
|
||||
}
|
||||
|
||||
object Retrofit {
|
||||
|
||||
private const val version = "2.9.0"
|
||||
const val lib = "com.squareup.retrofit2:retrofit:$version"
|
||||
const val moshi = "com.squareup.retrofit2:converter-moshi:$version"
|
||||
}
|
||||
|
||||
object Roomigrant {
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
plugins {
|
||||
id("com.android.library")
|
||||
kotlin("android")
|
||||
kotlin("kapt")
|
||||
kotlin("plugin.serialization") version "1.4.10"
|
||||
`lobsters-plugin`
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":model"))
|
||||
implementation(Dependencies.ThirdParty.Retrofit.lib)
|
||||
implementation(Dependencies.ThirdParty.Retrofit.moshi)
|
||||
implementation(Dependencies.Kotlin.Serialization.json)
|
||||
implementation(Dependencies.ThirdParty.retrofitSerialization)
|
||||
kaptTest(Dependencies.ThirdParty.Moshi.codegen)
|
||||
testImplementation(Dependencies.Testing.junit)
|
||||
testImplementation(Dependencies.Kotlin.Coroutines.core)
|
||||
testImplementation(Dependencies.Testing.mockWebServer)
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package dev.msfjarvis.lobsters.api
|
||||
|
||||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.MediaType
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
|
||||
object ApiClient {
|
||||
inline fun <reified T> getClient(baseUrl: String): T {
|
||||
return Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(MoshiConverterFactory.create())
|
||||
.addConverterFactory(Json.asConverterFactory(MediaType.get("application/json")))
|
||||
.build()
|
||||
.create(T::class.java)
|
||||
}
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
plugins {
|
||||
id("com.android.library")
|
||||
kotlin("android")
|
||||
kotlin("kapt")
|
||||
kotlin("plugin.serialization") version "1.4.10"
|
||||
`lobsters-plugin`
|
||||
}
|
||||
|
||||
dependencies {
|
||||
kapt(Dependencies.ThirdParty.Moshi.codegen)
|
||||
implementation(Dependencies.ThirdParty.Moshi.lib)
|
||||
implementation(Dependencies.Kotlin.Serialization.json)
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package dev.msfjarvis.lobsters.model
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@Serializable
|
||||
class KeybaseSignature(
|
||||
@Json(name = "kb_username")
|
||||
@SerialName("kb_username")
|
||||
val kbUsername: String,
|
||||
@Json(name = "sig_hash")
|
||||
@SerialName("sig_hash")
|
||||
val sigHash: String
|
||||
)
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
package dev.msfjarvis.lobsters.model
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@Serializable
|
||||
class LobstersPost(
|
||||
@Json(name = "short_id")
|
||||
@SerialName("short_id")
|
||||
val shortId: String,
|
||||
@Json(name = "short_id_url")
|
||||
@SerialName("short_id_url")
|
||||
val shortIdUrl: String,
|
||||
@Json(name = "created_at")
|
||||
@SerialName("created_at")
|
||||
val createdAt: String,
|
||||
val title: String,
|
||||
val url: String,
|
||||
val score: Long,
|
||||
val flags: Long,
|
||||
@Json(name = "comment_count")
|
||||
@SerialName("comment_count")
|
||||
val commentCount: Long,
|
||||
val description: String,
|
||||
@Json(name = "comments_url")
|
||||
@SerialName("comments_url")
|
||||
val commentsUrl: String,
|
||||
@Json(name = "submitter_user")
|
||||
@SerialName("submitter_user")
|
||||
val submitterUser: Submitter,
|
||||
val tags: List<String>,
|
||||
)
|
||||
|
|
|
@ -1,27 +1,27 @@
|
|||
package dev.msfjarvis.lobsters.model
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@Serializable
|
||||
class Submitter(
|
||||
val username: String,
|
||||
@Json(name = "created_at")
|
||||
@SerialName("created_at")
|
||||
val createdAt: String,
|
||||
@Json(name = "is_admin")
|
||||
@SerialName("is_admin")
|
||||
val isAdmin: Boolean,
|
||||
val about: String,
|
||||
@Json(name = "is_moderator")
|
||||
@SerialName("is_moderator")
|
||||
val isModerator: Boolean,
|
||||
val karma: Long = 0,
|
||||
@Json(name = "avatar_url")
|
||||
@SerialName("avatar_url")
|
||||
val avatarUrl: String,
|
||||
@Json(name = "invited_by_user")
|
||||
@SerialName("invited_by_user")
|
||||
val invitedByUser: String,
|
||||
@Json(name = "github_username")
|
||||
val githubUsername: String?,
|
||||
@Json(name = "twitter_username")
|
||||
val twitterUsername: String?,
|
||||
@Json(name = "keybase_signatures")
|
||||
val keybaseSignatures: List<KeybaseSignature>?
|
||||
@SerialName("github_username")
|
||||
val githubUsername: String? = null,
|
||||
@SerialName("twitter_username")
|
||||
val twitterUsername: String? = null,
|
||||
@SerialName("keybase_signatures")
|
||||
val keybaseSignatures: List<KeybaseSignature> = emptyList()
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue