-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7620 Make Url class @serializable and JVM Serializable
In our project we had to define our own UrlSerializer. It would be much nicer to have this in the Ktor library itself, so it works out of the box (similar to how Cookie was recently extended). Also, types like Url and Cookie should be java.io.Serializable. Otherwise Android crashes when using those types as e.g. screen arguments. This happens very quickly when Url is used indirectly as part of a data class where we wanted type safety.
- Loading branch information
1 parent
5b086cc
commit 9057d9f
Showing
14 changed files
with
216 additions
and
10 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// ktlint-disable filename | ||
/* | ||
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.http | ||
|
||
import io.ktor.http.* | ||
import io.ktor.junit.* | ||
import kotlin.test.* | ||
|
||
class SerializableTest { | ||
@Test | ||
fun urlTest() { | ||
val url = Url("https://localhost/path?key=value#fragment") | ||
assertEquals(url, assertSerializable(url)) | ||
} | ||
|
||
@Test | ||
fun cookieTest() { | ||
val cookie = Cookie("key", "value") | ||
assertEquals(cookie, assertSerializable(cookie)) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -53,6 +53,11 @@ abstract interface <#A: kotlin/Any> io.ktor.utils.io.pool/ObjectPool : kotlin/Au | |
open fun close() // io.ktor.utils.io.pool/ObjectPool.close|close(){}[0] | ||
} | ||
|
||
abstract interface <#A: kotlin/Any?> io.ktor.utils.io/JvmSerializer : io.ktor.utils.io/JvmSerializable { // io.ktor.utils.io/JvmSerializer|null[0] | ||
abstract fun jvmDeserialize(kotlin/ByteArray): #A // io.ktor.utils.io/JvmSerializer.jvmDeserialize|jvmDeserialize(kotlin.ByteArray){}[0] | ||
abstract fun jvmSerialize(#A): kotlin/ByteArray // io.ktor.utils.io/JvmSerializer.jvmSerialize|jvmSerialize(1:0){}[0] | ||
} | ||
|
||
abstract interface io.ktor.utils.io.core/Closeable : kotlin/AutoCloseable { // io.ktor.utils.io.core/Closeable|null[0] | ||
abstract fun close() // io.ktor.utils.io.core/Closeable.close|close(){}[0] | ||
} | ||
|
@@ -97,6 +102,8 @@ abstract interface io.ktor.utils.io/ChannelJob { // io.ktor.utils.io/ChannelJob| | |
abstract fun <get-job>(): kotlinx.coroutines/Job // io.ktor.utils.io/ChannelJob.job.<get-job>|<get-job>(){}[0] | ||
} | ||
|
||
abstract interface io.ktor.utils.io/JvmSerializable // io.ktor.utils.io/JvmSerializable|null[0] | ||
|
||
abstract class <#A: kotlin/Any> io.ktor.utils.io.pool/DefaultPool : io.ktor.utils.io.pool/ObjectPool<#A> { // io.ktor.utils.io.pool/DefaultPool|null[0] | ||
constructor <init>(kotlin/Int) // io.ktor.utils.io.pool/DefaultPool.<init>|<init>(kotlin.Int){}[0] | ||
|
||
|
@@ -399,6 +406,7 @@ final fun (kotlinx.io/Source).io.ktor.utils.io.core/readTextExactCharacters(kotl | |
final fun (kotlinx.io/Source).io.ktor.utils.io.core/release() // io.ktor.utils.io.core/release|[email protected](){}[0] | ||
final fun (kotlinx.io/Source).io.ktor.utils.io.core/takeWhile(kotlin/Function1<kotlinx.io/Buffer, kotlin/Boolean>) // io.ktor.utils.io.core/takeWhile|[email protected](kotlin.Function1<kotlinx.io.Buffer,kotlin.Boolean>){}[0] | ||
final fun (kotlinx.io/Source).io.ktor.utils.io/readText(): kotlin/String // io.ktor.utils.io/readText|[email protected](){}[0] | ||
final fun <#A: kotlin/Any> io.ktor.utils.io/JvmSerializerReplacement(io.ktor.utils.io/JvmSerializer<#A>, #A): kotlin/Any // io.ktor.utils.io/JvmSerializerReplacement|JvmSerializerReplacement(io.ktor.utils.io.JvmSerializer<0:0>;0:0){0§<kotlin.Any>}[0] | ||
final fun <#A: kotlin/Any?> (kotlinx.io/Sink).io.ktor.utils.io.core/preview(kotlin/Function1<kotlinx.io/Source, #A>): #A // io.ktor.utils.io.core/preview|[email protected](kotlin.Function1<kotlinx.io.Source,0:0>){0§<kotlin.Any?>}[0] | ||
final fun <#A: kotlin/Any?> (kotlinx.io/Source).io.ktor.utils.io.core/preview(kotlin/Function1<kotlinx.io/Source, #A>): #A // io.ktor.utils.io.core/preview|[email protected](kotlin.Function1<kotlinx.io.Source,0:0>){0§<kotlin.Any?>}[0] | ||
final fun <#A: kotlin/Any?> io.ktor.utils.io.core/withMemory(kotlin/Int, kotlin/Function1<kotlin/ByteArray, #A>): #A // io.ktor.utils.io.core/withMemory|withMemory(kotlin.Int;kotlin.Function1<kotlin.ByteArray,0:0>){0§<kotlin.Any?>}[0] | ||
|
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,16 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.utils.io | ||
|
||
public expect interface JvmSerializable | ||
|
||
public interface JvmSerializer<T> : JvmSerializable { | ||
public fun jvmSerialize(value: T): ByteArray | ||
public fun jvmDeserialize(value: ByteArray): T | ||
} | ||
|
||
public expect fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any | ||
|
||
internal object DummyJvmSimpleSerializerReplacement |
11 changes: 11 additions & 0 deletions
11
ktor-io/jsAndWasmShared/src/io/ktor/utils/io/JvmSerializable.jsAndWasmShared.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,11 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.utils.io | ||
|
||
/** Alias for `java.io.Serializable` on JVM. Empty interface otherwise. */ | ||
public actual interface JvmSerializable | ||
|
||
public actual fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any = | ||
DummyJvmSimpleSerializerReplacement |
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,39 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.utils.io | ||
|
||
import java.io.* | ||
|
||
public actual typealias JvmSerializable = Serializable | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
public actual fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any = | ||
DefaultJvmSerializerReplacement(serializer, value) | ||
|
||
@PublishedApi // IMPORTANT: changing the class name would result in serialization incompatibility | ||
internal class DefaultJvmSerializerReplacement<T : Any>( | ||
private var serializer: JvmSerializer<T>?, | ||
private var value: T? | ||
) : Externalizable { | ||
constructor() : this(null, null) | ||
|
||
override fun writeExternal(out: ObjectOutput) { | ||
out.writeObject(serializer) | ||
out.writeObject(serializer!!.jvmSerialize(value!!)) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun readExternal(`in`: ObjectInput) { | ||
serializer = `in`.readObject() as JvmSerializer<T> | ||
value = serializer!!.jvmDeserialize(`in`.readObject() as ByteArray) | ||
} | ||
|
||
private fun readResolve(): Any = | ||
value!! | ||
|
||
companion object { | ||
private const val serialVersionUID: Long = 0L | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.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,11 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.utils.io | ||
|
||
/** Alias for `java.io.Serializable` on JVM. Empty interface otherwise. */ | ||
public actual interface JvmSerializable | ||
|
||
public actual fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any = | ||
DummyJvmSimpleSerializerReplacement |
Oops, something went wrong.