Migrate from gson to moshi (#969)

This commit is contained in:
Mikołaj Pich 2020-09-27 20:59:27 +02:00 committed by GitHub
parent 2e05416fb5
commit a0f9c70036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 23 deletions

View File

@ -119,6 +119,7 @@ ext {
room = "2.2.5"
chucker = "3.2.0"
mockk = "1.10.0"
moshi = "1.9.3"
}
configurations.all {
@ -126,7 +127,7 @@ configurations.all {
}
dependencies {
implementation "io.github.wulkanowy:sdk:0.20.5"
implementation "io.github.wulkanowy:sdk:9364c2e"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
@ -170,7 +171,9 @@ dependencies {
implementation "com.ncapdevi:frag-nav:3.3.0"
implementation "com.github.YarikSOffice:lingver:1.2.2"
implementation "com.google.code.gson:gson:2.8.6"
implementation "com.squareup.moshi:moshi:$moshi"
implementation "com.squareup.moshi:moshi-adapters:$moshi"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi"
implementation "com.jakewharton.timber:timber:4.7.1"
implementation "at.favre.lib:slf4j-timber:1.0.1"
implementation "fr.bipi.treessence:treessence:0.3.2"

View File

@ -1,8 +1,8 @@
package io.github.wulkanowy.data.db
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
@ -12,6 +12,16 @@ import java.util.Date
class Converters {
private val moshi by lazy { Moshi.Builder().build() }
private val integerListAdapter by lazy {
moshi.adapter<List<Int>>(Types.newParameterizedType(List::class.java, Integer::class.java))
}
private val stringMapAdapter by lazy {
moshi.adapter<Map<String, String>>(Types.newParameterizedType(MutableMap::class.java, String::class.java, String::class.java))
}
@TypeConverter
fun timestampToDate(value: Long?): LocalDate? = value?.run {
Date(value).toInstant().atZone(ZoneOffset.UTC).toLocalDate()
@ -39,22 +49,22 @@ class Converters {
fun intToMonth(value: Int?) = value?.let { Month.of(it) }
@TypeConverter
fun intListToGson(list: List<Int>): String {
return Gson().toJson(list)
fun intListToJson(list: List<Int>): String {
return integerListAdapter.toJson(list)
}
@TypeConverter
fun gsonToIntList(value: String): List<Int> {
return Gson().fromJson(value, object : TypeToken<List<Int>>() {}.type)
fun jsonToIntList(value: String): List<Int> {
return integerListAdapter.fromJson(value).orEmpty()
}
@TypeConverter
fun stringPairListToGson(list: List<Pair<String, String>>): String {
return Gson().toJson(list)
fun stringPairListToJson(list: List<Pair<String, String>>): String {
return stringMapAdapter.toJson(list.toMap())
}
@TypeConverter
fun gsonToStringPairList(value: String): List<Pair<String, String>> {
return Gson().fromJson(value, object : TypeToken<List<Pair<String, String>>>() {}.type)
fun jsonToStringPairList(value: String): List<Pair<String, String>> {
return stringMapAdapter.fromJson(value).orEmpty().toList()
}
}

View File

@ -1,3 +1,9 @@
package io.github.wulkanowy.data.pojos
class Contributor(val displayName: String, val githubUsername: String)
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
class Contributor(
val displayName: String,
val githubUsername: String
)

View File

@ -1,7 +1,8 @@
package io.github.wulkanowy.data.repositories.appcreator
import android.content.res.AssetManager
import com.google.gson.Gson
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import io.github.wulkanowy.data.pojos.Contributor
import io.github.wulkanowy.utils.DispatchersProvider
import kotlinx.coroutines.withContext
@ -15,9 +16,9 @@ class AppCreatorRepository @Inject constructor(
) {
suspend fun getAppCreators() = withContext(dispatchers.backgroundThread) {
Gson().fromJson(
assets.open("contributors.json").bufferedReader().use { it.readText() },
Array<Contributor>::class.java
).toList()
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(List::class.java, Contributor::class.java)
val adapter = moshi.adapter<List<Contributor>>(type)
adapter.fromJson(assets.open("contributors.json").bufferedReader().use { it.readText() })
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -20,17 +21,18 @@
android:id="@+id/creatorRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android:layout_weight="1"
tools:listitem="@layout/item_contributor" />
<com.google.android.material.button.MaterialButton
android:id="@+id/creatorSeeMore"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="8dp"
android:text="@string/contributor_see_more" />
</LinearLayout>
</FrameLayout>

View File

@ -15,7 +15,8 @@
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/contributor_avatar_description" />
android:contentDescription="@string/contributor_avatar_description"
tools:src="@tools:sample/avatars" />
<TextView
android:id="@+id/creatorItemName"