-
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 (#4421)
- Loading branch information
1 parent
6e6cfb2
commit 6e87ccf
Showing
14 changed files
with
232 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,22 @@ | ||
// 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() { | ||
assertSerializable(Url("https://localhost/path?key=value#fragment")) | ||
} | ||
|
||
@Test | ||
fun cookieTest() { | ||
assertSerializable(Cookie("key", "value")) | ||
} | ||
} |
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,20 @@ | ||
/* | ||
* 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. */ | ||
@InternalAPI | ||
public expect interface JvmSerializable | ||
|
||
@InternalAPI | ||
public interface JvmSerializer<T> : JvmSerializable { | ||
public fun jvmSerialize(value: T): ByteArray | ||
public fun jvmDeserialize(value: ByteArray): T | ||
} | ||
|
||
@InternalAPI | ||
public expect fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any | ||
|
||
internal object DummyJvmSimpleSerializerReplacement |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
/* | ||
* 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 | ||
|
||
@InternalAPI | ||
public actual interface JvmSerializable | ||
|
||
@InternalAPI | ||
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,42 @@ | ||
/* | ||
* 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.* | ||
|
||
@InternalAPI | ||
public actual typealias JvmSerializable = Serializable | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
@InternalAPI | ||
public actual fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any = | ||
DefaultJvmSerializerReplacement(serializer, value) | ||
|
||
@OptIn(InternalAPI::class) | ||
@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 | ||
} | ||
} |
Oops, something went wrong.