Created
April 30, 2021 16:09
-
-
Save cicerohellmann/dd021ff350094bd19cbfea6a408ae3e6 to your computer and use it in GitHub Desktop.
A serialization test between our serialization libraries
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
@Serializable | |
data class User( | |
val name: String, | |
val email: String, | |
val age: Int = 13, | |
val isAlive: Boolean = false, | |
val role: Role = Role.Viewer, | |
val bodyType: BodyType = BodyType(Body.NotSkinny), | |
) | |
@Serializable | |
data class BodyType( | |
val type: Body? = null | |
) | |
enum class Body { Skinny, NotSkinny } | |
enum class Role { Viewer, Editor, Owner } | |
class SerializeTest { | |
private val jsonString = """ | |
{ | |
"name" : "John Doe", | |
"email" : "john.doe@email.com" | |
} | |
""".trimIndent() | |
@Test | |
fun gsonTest() { | |
val user = Gson().fromJson(jsonString, User::class.java) | |
assertEquals("John Doe", user.name) | |
assertEquals(Role.Viewer, user.role) | |
assertEquals(13, user.age) | |
assertEquals(false, user.isAlive) | |
assertEquals(BodyType(Body.NotSkinny), user.bodyType) | |
} | |
@Test | |
fun jsonTest() { | |
val user = Json.decodeFromString<User>(jsonString) | |
assertEquals("John Doe", user.name) | |
assertEquals(Role.Viewer, user.role) | |
assertEquals(13, user.age) | |
assertEquals(false, user.isAlive) | |
assertEquals(BodyType(Body.NotSkinny), user.bodyType) | |
} | |
@Test | |
fun moshiTest() { | |
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() | |
val jsonAdapter = moshi.adapter(User::class.java) | |
val user = jsonAdapter.fromJson(jsonString) | |
assertEquals("John Doe", user?.name) | |
assertEquals(Role.Viewer, user?.role) | |
assertEquals(13, user?.age) | |
assertEquals(false, user?.isAlive) | |
assertEquals(BodyType(Body.NotSkinny), user?.bodyType) | |
} | |
} | |
//for gradle(:app) | |
dependencies { | |
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0" | |
implementation("com.squareup.moshi:moshi-kotlin:1.12.0") | |
implementation 'com.google.code.gson:gson:2.8.6' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment