-
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
3d71a28
commit 8db09e6
Showing
10 changed files
with
159 additions
and
3 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
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
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 |
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