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" room = "2.2.5"
chucker = "3.2.0" chucker = "3.2.0"
mockk = "1.10.0" mockk = "1.10.0"
moshi = "1.9.3"
} }
configurations.all { configurations.all {
@ -126,7 +127,7 @@ configurations.all {
} }
dependencies { 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' 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.ncapdevi:frag-nav:3.3.0"
implementation "com.github.YarikSOffice:lingver:1.2.2" 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 "com.jakewharton.timber:timber:4.7.1"
implementation "at.favre.lib:slf4j-timber:1.0.1" implementation "at.favre.lib:slf4j-timber:1.0.1"
implementation "fr.bipi.treessence:treessence:0.3.2" implementation "fr.bipi.treessence:treessence:0.3.2"

View File

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

View File

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

View File

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

View File

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