[BUG][Kotlin] Add support for collection of generic classes (Jackson / Gson / Moshi) (#9918)

* Cherry picked commits from add-support-for-collection-of-generic-classes

* Regenerated samples

* Missing work by @adamsz-lume

* Try to fix maven plugin error

Co-authored-by: Bruno Coelho <4brunu@gmail.com>
This commit is contained in:
Robin Trietsch 2021-07-24 17:04:14 +02:00 committed by GitHub
parent 888f9cd84a
commit 8eba70dd3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 61 additions and 15 deletions

View File

@ -238,7 +238,7 @@
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<kotlin.version>1.5.10</kotlin.version> <kotlin.version>1.5.10</kotlin.version>
<kotlinJvmTarget>1.8</kotlinJvmTarget> <kotlinJvmTarget>1.8</kotlinJvmTarget>
<moshi-kotlin.version>1.8.0</moshi-kotlin.version> <moshi-kotlin.version>1.12.0</moshi-kotlin.version>
<migbase64.version>2.2</migbase64.version> <migbase64.version>2.2</migbase64.version>
</properties> </properties>
</project> </project>

View File

@ -62,9 +62,11 @@ dependencies {
{{^moshiCodeGen}} {{^moshiCodeGen}}
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
{{/moshiCodeGen}} {{/moshiCodeGen}}
{{#moshiCodeGen}} {{#moshiCodeGen}}
implementation "com.squareup.moshi:moshi:1.12.0" implementation "com.squareup.moshi:moshi:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0" kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0"
{{/moshiCodeGen}} {{/moshiCodeGen}}
{{/moshi}} {{/moshi}}
@ -126,4 +128,4 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
freeCompilerArgs += "-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi" freeCompilerArgs += "-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi"
} }
} }
{{/kotlinx_serialization}} {{/kotlinx_serialization}}

View File

@ -55,6 +55,15 @@ import org.threeten.bp.LocalTime
import org.threeten.bp.OffsetDateTime import org.threeten.bp.OffsetDateTime
import org.threeten.bp.OffsetTime import org.threeten.bp.OffsetTime
{{/threetenbp}} {{/threetenbp}}
{{#gson}}
import com.google.gson.reflect.TypeToken
{{/gson}}
{{#jackson}}
import com.fasterxml.jackson.core.type.TypeReference
{{/jackson}}
{{#moshi}}
import com.squareup.moshi.adapter
{{/moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}open class ApiClient(val baseUrl: String) { {{#nonPublicApi}}internal {{/nonPublicApi}}open class ApiClient(val baseUrl: String) {
{{#nonPublicApi}}internal {{/nonPublicApi}}companion object { {{#nonPublicApi}}internal {{/nonPublicApi}}companion object {
@ -173,6 +182,9 @@ import org.threeten.bp.OffsetTime
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
{{#moshi}}
@OptIn(ExperimentalStdlibApi::class)
{{/moshi}}
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -201,7 +213,7 @@ import org.threeten.bp.OffsetTime
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).fromJson(bodyContent){{/moshi}}{{#gson}}Serializer.gson.fromJson(bodyContent, T::class.java){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, T::class.java){{/jackson}}{{#kotlinx_serialization}}Serializer.jvmJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}} JsonMediaType -> {{#moshi}}Serializer.moshi.adapter<T>().fromJson(bodyContent){{/moshi}}{{#gson}}Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType()){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T>() {}){{/jackson}}{{#kotlinx_serialization}}Serializer.jvmJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}}
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.google.gson.reflect.TypeToken
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -124,7 +125,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.gson.fromJson(bodyContent, T::class.java) JsonMediaType -> Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.fasterxml.jackson.core.type.TypeReference
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -124,7 +125,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.jacksonObjectMapper.readValue(bodyContent, T::class.java) JsonMediaType -> Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference<T>() {})
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -32,6 +32,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -23,6 +23,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -107,6 +108,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -130,7 +132,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.google.gson.reflect.TypeToken
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -124,7 +125,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.gson.fromJson(bodyContent, T::class.java) JsonMediaType -> Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ test {
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "com.squareup.moshi:moshi:1.12.0" implementation "com.squareup.moshi:moshi:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0" kapt "com.squareup.moshi:moshi-kotlin-codegen:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
internal open class ApiClient(val baseUrl: String) { internal open class ApiClient(val baseUrl: String) {
internal companion object { internal companion object {
@ -106,6 +107,7 @@ internal open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ internal open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:3.12.13" implementation "com.squareup.okhttp3:okhttp:3.12.13"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -20,6 +20,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -104,6 +105,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -122,7 +124,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -33,6 +33,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.2" implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.2"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1" implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "io.reactivex.rxjava3:rxjava:$rxJava3Version" implementation "io.reactivex.rxjava3:rxjava:$rxJava3Version"

View File

@ -32,6 +32,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.2" implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.2"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1" implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion" implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
implementation "org.threeten:threetenbp:1.5.1" implementation "org.threeten:threetenbp:1.5.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"

View File

@ -22,6 +22,7 @@ import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime import org.threeten.bp.LocalTime
import org.threeten.bp.OffsetDateTime import org.threeten.bp.OffsetDateTime
import org.threeten.bp.OffsetTime import org.threeten.bp.OffsetTime
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }

View File

@ -31,6 +31,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1" implementation "com.squareup.okhttp3:okhttp:4.9.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2" testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
} }

View File

@ -22,6 +22,7 @@ import java.time.OffsetDateTime
import java.time.OffsetTime import java.time.OffsetTime
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import com.squareup.moshi.adapter
open class ApiClient(val baseUrl: String) { open class ApiClient(val baseUrl: String) {
companion object { companion object {
@ -106,6 +107,7 @@ open class ApiClient(val baseUrl: String) {
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
} }
@OptIn(ExperimentalStdlibApi::class)
protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { protected inline fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? {
if(body == null) { if(body == null) {
return null return null
@ -124,7 +126,7 @@ open class ApiClient(val baseUrl: String) {
return f as T return f as T
} }
return when(mediaType) { return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent) JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
} }
} }