Skip to content

Commit 3f40c75

Browse files
committed
Using kotlinx.serialization instead of jackson
1 parent d99d4bb commit 3f40c75

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

build.gradle

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'org.jetbrains.kotlin.jvm' version '1.4.20'
3+
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.20'
34
}
45

56
group 'intro-coroutines'
@@ -12,22 +13,22 @@ repositories {
1213
dependencies {
1314
implementation "org.jetbrains.kotlin:kotlin-stdlib"
1415
implementation "org.jetbrains.kotlin:kotlin-reflect"
16+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")
1517

1618
def coroutines_version = '1.4.2'
1719
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
1820
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-swing:$coroutines_version"
1921
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutines_version"
2022
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:$coroutines_version"
2123
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version"
22-
23-
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7"
24+
2425
implementation 'ch.qos.logback:logback-classic:1.0.13'
2526

2627
def retrofit_version = '2.9.0'
2728
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
28-
implementation "com.squareup.retrofit2:converter-jackson:$retrofit_version"
29-
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
3029
implementation "com.squareup.retrofit2:retrofit-mock:$retrofit_version"
30+
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
31+
implementation 'com.squareup.okhttp3:okhttp:4.3.1'
3132

3233
implementation 'io.reactivex.rxjava2:rxjava:2.2.20'
3334
implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
@@ -38,15 +39,15 @@ dependencies {
3839
}
3940

4041
compileKotlin {
41-
kotlinOptions.jvmTarget = "1.8"
42-
}
43-
44-
compileTestKotlin {
45-
kotlinOptions.jvmTarget = "1.8"
42+
kotlinOptions {
43+
jvmTarget = "1.8"
44+
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
45+
}
4646
}
4747

4848
compileTestKotlin {
4949
kotlinOptions {
50+
jvmTarget = "1.8"
5051
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
5152
}
5253
}

src/contributors/GitHubService.kt

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package contributors
22

3-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
4-
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5-
import io.reactivex.Flowable
6-
import io.reactivex.Single
7-
import io.reactivex.Observable
8-
//import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
3+
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
4+
import kotlinx.serialization.ExperimentalSerializationApi
5+
import kotlinx.serialization.Serializable
6+
import kotlinx.serialization.json.Json
7+
import okhttp3.MediaType.Companion.toMediaType
98
import okhttp3.OkHttpClient
109
import retrofit2.Call
1110
import retrofit2.Response
1211
import retrofit2.Retrofit
1312
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
14-
import retrofit2.converter.jackson.JacksonConverterFactory
1513
import retrofit2.http.GET
1614
import retrofit2.http.Path
17-
import java.util.*
15+
import java.util.Base64
1816

1917
interface GitHubService {
2018
@GET("orgs/{org}/repos?per_page=100")
@@ -29,24 +27,26 @@ interface GitHubService {
2927
): Call<List<User>>
3028
}
3129

32-
@JsonIgnoreProperties(ignoreUnknown = true)
30+
@Serializable
3331
data class Repo(
3432
val id: Long,
3533
val name: String
3634
)
3735

38-
@JsonIgnoreProperties(ignoreUnknown = true)
36+
@Serializable
3937
data class User(
4038
val login: String,
4139
val contributions: Int
4240
)
4341

42+
@Serializable
4443
data class RequestData(
4544
val username: String,
4645
val password: String,
4746
val org: String
4847
)
4948

49+
@OptIn(ExperimentalSerializationApi::class)
5050
fun createGitHubService(username: String, password: String): GitHubService {
5151
val authToken = "Basic " + Base64.getEncoder().encode("$username:$password".toByteArray()).toString(Charsets.UTF_8)
5252
val httpClient = OkHttpClient.Builder()
@@ -60,9 +60,10 @@ fun createGitHubService(username: String, password: String): GitHubService {
6060
}
6161
.build()
6262

63+
val contentType = "application/json".toMediaType()
6364
val retrofit = Retrofit.Builder()
6465
.baseUrl("https://api.github.com")
65-
.addConverterFactory(JacksonConverterFactory.create(jacksonObjectMapper()))
66+
.addConverterFactory(Json { ignoreUnknownKeys = true }.asConverterFactory(contentType))
6667
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
6768
.client(httpClient)
6869
.build()

0 commit comments

Comments
 (0)