This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Adopt PublishedApi #36
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6532108
Add PublishedApi to BaseRetrofitHelper
Goooler 8503127
Add JsonUtil to JsonUtil
Goooler b093e57
Add JsonUtilTest
Goooler f998570
Add test cases
Goooler 7e63dbf
Rename
Goooler 4d6b3b1
Replace assert with Assert.assertTrue
Goooler 95b2fbf
Update unit-tests job
Goooler 932972b
kaptTest moshiCompiler
Goooler a55725e
Add @Language annotations
Goooler 5f7e50d
Fix tests
Goooler 47a0b02
Cleanup
Goooler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,6 @@ dependencies { | |
implementations(*Libs.coil) | ||
debugImplementations(Libs.chuckerDebug) | ||
releaseImplementations(Libs.chuckerRelease) | ||
|
||
kaptTests(Libs.moshiCompiler) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,47 +4,43 @@ package io.goooler.demoapp.common.util | |
|
||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import java.lang.reflect.Type | ||
import org.intellij.lang.annotations.Language | ||
|
||
object JsonUtil { | ||
@PublishedApi | ||
internal val moshi: Moshi = Moshi.Builder().build() | ||
|
||
fun <T> fromJson(json: String, clazz: Class<T>): T? = try { | ||
moshi.adapter(clazz).fromJson(json) | ||
inline fun <reified T> fromJson(@Language("JSON") string: String): T? = try { | ||
moshi.adapter(T::class.java).fromJson(string) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
|
||
fun <T> fromJson(json: String, typeOfT: Type): T? = try { | ||
moshi.adapter<T>(typeOfT).fromJson(json) | ||
inline fun <reified T> fromJson( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
@Language("JSON") string: String, | ||
rawType: Class<*>, | ||
vararg typeArguments: Class<*> | ||
): T? = try { | ||
moshi.adapter<T>(Types.newParameterizedType(rawType, *typeArguments)).fromJson(string) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
|
||
fun <T> fromJson(json: String, rawType: Class<*>, vararg typeArguments: Class<*>): T? = try { | ||
fromJson(json, Types.newParameterizedType(rawType, *typeArguments)) | ||
@Language("JSON") | ||
inline fun <reified T> toJson(value: T?): String? = try { | ||
moshi.adapter(T::class.java).toJson(value) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
|
||
fun <T> toJson(o: T?, clazz: Class<T>): String? = try { | ||
moshi.adapter(clazz).toJson(o) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
|
||
inline fun <reified T> toJson(o: T?): String? = toJson(o, T::class.java) | ||
} | ||
|
||
inline fun <reified T> String.fromJson(): T? = JsonUtil.fromJson(this, T::class.java) | ||
|
||
inline fun <reified T> String.fromJson(typeOfT: Type): T? = JsonUtil.fromJson(this, typeOfT) | ||
inline fun <reified T> String.fromJson(): T? = JsonUtil.fromJson(this) | ||
|
||
inline fun <reified T> String.fromJson(rawType: Class<*>, vararg typeArguments: Class<*>): T? = | ||
JsonUtil.fromJson(this, rawType, *typeArguments) | ||
|
||
@Language("JSON") | ||
fun Any?.toJson(): String? = JsonUtil.toJson(this) |
85 changes: 85 additions & 0 deletions
85
common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.goooler.demoapp.common | ||
|
||
import com.squareup.moshi.JsonClass | ||
import io.goooler.demoapp.base.util.secondOrNull | ||
import io.goooler.demoapp.common.util.JsonUtil | ||
import io.goooler.demoapp.common.util.fromJson | ||
import io.goooler.demoapp.common.util.toJson | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class JsonUtilTest { | ||
@Test | ||
fun `JsonUtil fromJson(String)`() { | ||
assertTrue(JsonUtil.fromJson<Repo>(firstStr) == firstBean) | ||
assertTrue(JsonUtil.fromJson<Repo>(secondStr) == secondBean) | ||
} | ||
|
||
@Test | ||
fun `JsonUtil fromJson(String, Class, Class)`() { | ||
val list: List<Repo> = JsonUtil.fromJson(strArray, List::class.java, Repo::class.java) | ||
?: throw Exception("Parse json error") | ||
assertTrue(list.firstOrNull() == firstBean) | ||
assertTrue(list.secondOrNull() == secondBean) | ||
} | ||
|
||
@Test | ||
fun `JsonUtil toJson(T)`() { | ||
assertTrue(JsonUtil.toJson(firstBean) == firstStr) | ||
assertTrue(JsonUtil.toJson(secondBean) == secondStr) | ||
} | ||
|
||
@Test | ||
fun `String fromJson()`() { | ||
assertTrue(firstStr.fromJson<Repo>() == firstBean) | ||
assertTrue(secondStr.fromJson<Repo>() == secondBean) | ||
} | ||
|
||
@Test | ||
fun `String fromJson(Class, Class)`() { | ||
val list: List<Repo> = strArray.fromJson(List::class.java, Repo::class.java) | ||
?: throw Exception("Parse json error") | ||
assertTrue(list.first() == firstBean) | ||
assertTrue(list[1] == secondBean) | ||
} | ||
|
||
@Test | ||
fun `Any toJson(T)`() { | ||
assertTrue(firstBean.toJson() == firstStr) | ||
assertTrue(secondBean.toJson() == secondStr) | ||
} | ||
|
||
@JsonClass(generateAdapter = true) | ||
internal data class Repo(val id: Long, val name: String, val owner: Owner) { | ||
@JsonClass(generateAdapter = true) | ||
data class Owner(val login: String) | ||
|
||
override fun equals(other: Any?): Boolean = if (other is Repo) { | ||
this.id == other.id && this.name == other.name && this.owner.login == other.owner.login | ||
} else { | ||
false | ||
} | ||
|
||
override fun hashCode(): Int = id.hashCode() | ||
} | ||
|
||
companion object { | ||
@Language("JSON") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
private val firstStr = """ | ||
{"id":126987864,"name":"1024_hosts","owner":{"login":"Goooler"}} | ||
""".trimIndent() | ||
|
||
@Language("JSON") | ||
private val secondStr = """ | ||
{"id":374913489,"name":"AndroidUiDemo","owner":{"login":"Goooler"}} | ||
""".trimIndent() | ||
|
||
// https://api.github.com/users/goooler/repos?&page=1&per_page=2 | ||
@Language("JSON") | ||
private val strArray = "[$firstStr,$secondStr]" | ||
|
||
private val firstBean = Repo(126987864, "1024_hosts", Repo.Owner("Goooler")) | ||
private val secondBean = Repo(374913489, "AndroidUiDemo", Repo.Owner("Goooler")) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.