Skip to content

Commit

Permalink
Add js support in kotlinx.serialization feature
Browse files Browse the repository at this point in the history
    Close #666
  • Loading branch information
e5l committed Oct 29, 2018
1 parent 5c1295a commit 993c868
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -21,6 +21,7 @@ buildscript {
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$kotlin_native_version"
classpath "org.jetbrains.kotlinx:atomicfu-gradle-plugin:$atomic_fu_version"
classpath "com.moowork.gradle:gradle-node-plugin:$gradle_node_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}

Expand Down
3 changes: 2 additions & 1 deletion gradle/js.gradle
Expand Up @@ -100,10 +100,12 @@ prepareMochaChrome.doLast {
<script src="$projectDir/build/web/kotlinx-coroutines-core.js"></script>
<script src="$projectDir/build/web/kotlinx-io-js.js"></script>
<script src="$projectDir/build/web/kotlinx-coroutines-io-js.js"></script>
<script src="$projectDir/build/web/kotlinx-serialization-runtime-js.js"></script>
<script src="$projectDir/build/web/ktor-utils-js.js"></script>
<script src="$projectDir/build/web/ktor-http-js.js"></script>
<script src="$projectDir/build/web/ktor-client-core-js.js"></script>
<script src="$projectDir/build/web/ktor-client-js.js"></script>
<script src="$projectDir/build/web/ktor-client-json-js.js"></script>
<script src="$compileTestKotlin2Js.outputFile"></script>
<script>mocha.run();</script>
</body>
Expand All @@ -119,4 +121,3 @@ task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) {
}

//test.dependsOn testMochaChrome

3 changes: 2 additions & 1 deletion gradle/platform.gradle
Expand Up @@ -18,7 +18,8 @@ def jsModules = [
'ktor-client-js',
'ktor-client-core-js',
'ktor-http-js',
'ktor-utils-js'
'ktor-utils-js',
'ktor-client-json-js'
]

ext.platformOf = { Project project ->
Expand Down
2 changes: 2 additions & 0 deletions ktor-client/ktor-client-features/build.gradle
@@ -1,5 +1,7 @@
description = "Ktor client features"

apply plugin: 'kotlinx-serialization'

dependencies {
compile project(":ktor-client:ktor-client-core:ktor-client-core-jvm")
}
@@ -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"
}
}
@@ -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"
}
@@ -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()
@@ -1,3 +1,6 @@

apply plugin: 'kotlinx-serialization'

dependencies {
expectedBy project(':ktor-client:ktor-client-features:ktor-client-json')

Expand Down
@@ -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
}
}
1 change: 1 addition & 0 deletions settings.gradle
Expand Up @@ -45,6 +45,7 @@ includeEx ':ktor-client:ktor-client-features:ktor-client-json'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-json-tests'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-json-ios'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-json-jvm'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-json-js'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-gson'
includeEx ':ktor-client:ktor-client-features:ktor-client-json:ktor-client-jackson'
includeEx ':ktor-client:ktor-client-features:ktor-client-auth-basic'
Expand Down

0 comments on commit 993c868

Please sign in to comment.