Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add js support in kotlinx.serialization feature
Close #666
- Loading branch information
Showing
10 changed files
with
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
description = "Ktor client features" | ||
|
||
apply plugin: 'kotlinx-serialization' | ||
|
||
dependencies { | ||
compile project(":ktor-client:ktor-client-core:ktor-client-core-jvm") | ||
} |
3 changes: 2 additions & 1 deletion
3
ktor-client/ktor-client-features/ktor-client-json/ktor-client-json-ios/build.gradle
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
apply plugin: 'kotlinx-serialization' | ||
|
||
dependencies { | ||
expectedBy project(':ktor-client:ktor-client-features:ktor-client-json') | ||
implementation project(':ktor-client:ktor-client-core:ktor-client-core-ios') | ||
|
||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
ktor-client/ktor-client-features/ktor-client-json/ktor-client-json-js/build.gradle
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,8 @@ | ||
apply plugin: 'kotlinx-serialization' | ||
|
||
dependencies { | ||
expectedBy project(':ktor-client:ktor-client-features:ktor-client-json') | ||
|
||
compile project(':ktor-client:ktor-client-core:ktor-client-core-js') | ||
compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version" | ||
} |
13 changes: 13 additions & 0 deletions
13
...tor-client-json/ktor-client-json-js/src/io/ktor/client/features/json/defaultSerializer.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,13 @@ | ||
package io.ktor.client.features.json | ||
|
||
import io.ktor.client.features.json.serializer.* | ||
|
||
/** | ||
* Platform default serializer. | ||
* | ||
* Uses service loader on jvm. | ||
* Consider to add one of the following dependencies: | ||
* - ktor-client-gson | ||
* - ktor-client-json | ||
*/ | ||
actual fun defaultSerializer(): JsonSerializer = KotlinxSerializer() |
3 changes: 3 additions & 0 deletions
3
ktor-client/ktor-client-features/ktor-client-json/ktor-client-json-jvm/build.gradle
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
53 changes: 53 additions & 0 deletions
53
ktor-client/ktor-client-features/ktor-client-json/test/KotlinxSerializerTest.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,53 @@ | ||
package io.ktor.client.features.json | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.features.json.serializer.* | ||
import io.ktor.client.response.* | ||
import io.ktor.http.* | ||
import io.ktor.http.content.* | ||
import io.ktor.util.date.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.io.* | ||
import kotlinx.serialization.* | ||
import kotlin.coroutines.* | ||
import kotlin.jvm.* | ||
import kotlin.test.* | ||
|
||
@Serializable | ||
internal data class User(val id: Long, val login: String) | ||
|
||
class KotlinxSerializerTest { | ||
|
||
@Test | ||
fun registerCustomTest() { | ||
val serializer = KotlinxSerializer().apply { | ||
register(User::class.serializer()) | ||
} | ||
|
||
val user = User(1, "vasya") | ||
val actual = serializer.testWrite(user) | ||
assertEquals(" {\"id\":1,\"login\":\"vasya\"}", actual) | ||
// assertEquals(user, serializer.testRead(actual)) | ||
} | ||
|
||
private fun JsonSerializer.testWrite(data: Any): String = | ||
(write(data) as? TextContent)?.text ?: error("Failed to get serialized $data") | ||
|
||
private suspend inline fun <reified T : Any> JsonSerializer.testRead(data: String): T { | ||
val info = typeInfo<T>() | ||
|
||
val response = object : HttpResponse { | ||
override val call: HttpClientCall get() = TODO() | ||
override val status: HttpStatusCode get() = TODO() | ||
override val version: HttpProtocolVersion get() = TODO() | ||
override val requestTime: GMTDate get() = TODO() | ||
override val responseTime: GMTDate get() = TODO() | ||
override val headers: Headers get() = TODO() | ||
override val coroutineContext: CoroutineContext get() = TODO() | ||
|
||
override val content: ByteReadChannel = ByteReadChannel(data) | ||
} | ||
|
||
return read(info, response) as T | ||
} | ||
} |
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