diff --git a/build.gradle.kts b/build.gradle.kts index f48f1d1..f3bd642 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,13 +1,13 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.6.21" apply false - id("org.jmailen.kotlinter") version "3.10.0" apply false + kotlin("jvm") version "1.7.10" apply false + id("org.jmailen.kotlinter") version "3.11.1" apply false id("com.github.ben-manes.versions") version "0.42.0" } subprojects { group = "com.github.cs125-illinois" - version = "2022.4.0" + version = "2022.9.0" tasks.withType { kotlinOptions { jvmTarget = JavaVersion.VERSION_16.toString() diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa991fc..ae04661 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 04c4816..cb33851 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -7,14 +7,14 @@ plugins { dependencies { kaptTest("com.squareup.moshi:moshi-kotlin-codegen:1.13.0") - implementation("io.ktor:ktor-server-core:2.0.0") - implementation("io.ktor:ktor-server-content-negotiation:2.0.0") + implementation("io.ktor:ktor-server-core:2.1.0") + implementation("io.ktor:ktor-server-content-negotiation:2.1.0") implementation("com.squareup.moshi:moshi-kotlin:1.13.0") - implementation("com.squareup.okio:okio:3.1.0") + implementation("com.squareup.okio:okio:3.2.0") testImplementation(kotlin("test")) testImplementation("com.google.truth:truth:1.1.3") - testImplementation("io.ktor:ktor-server-test-host:2.0.0") + testImplementation("io.ktor:ktor-server-test-host:2.1.0") } tasks { val sourcesJar by creating(Jar::class) { diff --git a/library/src/main/kotlin/com/ryanharter/ktor/moshi/MoshiConverter.kt b/library/src/main/kotlin/com/ryanharter/ktor/moshi/MoshiConverter.kt index 29b1651..3af1b34 100644 --- a/library/src/main/kotlin/com/ryanharter/ktor/moshi/MoshiConverter.kt +++ b/library/src/main/kotlin/com/ryanharter/ktor/moshi/MoshiConverter.kt @@ -3,19 +3,19 @@ package com.ryanharter.ktor.moshi import com.squareup.moshi.Moshi +import io.ktor.content.TextContent import io.ktor.http.ContentType -import io.ktor.http.content.TextContent import io.ktor.http.withCharset import io.ktor.serialization.ContentConverter import io.ktor.server.plugins.contentnegotiation.ContentNegotiationConfig import io.ktor.util.reflect.TypeInfo import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.charsets.Charset import io.ktor.utils.io.jvm.javaio.toInputStream import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import okio.buffer import okio.source +import java.nio.charset.Charset class MoshiConverter(private val moshi: Moshi = Moshi.Builder().build()) : ContentConverter { override suspend fun deserialize(charset: Charset, typeInfo: TypeInfo, content: ByteReadChannel): Any? { @@ -24,12 +24,18 @@ class MoshiConverter(private val moshi: Moshi = Moshi.Builder().build()) : Conte } } - override suspend fun serialize( + override suspend fun serializeNullable( contentType: ContentType, charset: Charset, typeInfo: TypeInfo, - value: Any - ) = TextContent(moshi.adapter(value.javaClass).toJson(value), contentType.withCharset(charset)) + value: Any? + ) = TextContent( + moshi.adapter( + value?.javaClass + ?: Any::class.java + ).nullSafe().toJson(value), + contentType.withCharset(charset) + ) } /** diff --git a/sample/build.gradle.kts b/sample/build.gradle.kts index b277bae..e8e2d81 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -8,10 +8,10 @@ dependencies { kapt("com.squareup.moshi:moshi-kotlin-codegen:1.13.0") implementation(project(":library")) - implementation("io.ktor:ktor-server-netty:2.0.0") - implementation("io.ktor:ktor-server-content-negotiation:2.0.0") - implementation("io.ktor:ktor-server-call-logging:2.0.0") - implementation("ch.qos.logback:logback-classic:1.2.11") + implementation("io.ktor:ktor-server-netty:2.1.0") + implementation("io.ktor:ktor-server-content-negotiation:2.1.0") + implementation("io.ktor:ktor-server-call-logging:2.1.0") + implementation("ch.qos.logback:logback-classic:1.4.0") implementation("com.squareup.moshi:moshi-adapters:1.13.0") implementation("com.squareup.moshi:moshi:1.13.0") implementation("com.squareup.moshi:moshi-kotlin:1.13.0")