From 2be66023daf2736c1946f5c753221a45dd3ec1d4 Mon Sep 17 00:00:00 2001 From: MJ Date: Sun, 11 Feb 2018 00:05:35 +0100 Subject: [PATCH] Added kotlinx-serialization support. --- build.gradle | 3 +++ client/build.gradle | 2 ++ .../czyzby/example/client/ClientTest.kt | 19 +++++++++++++++++++ common/build.gradle | 2 ++ .../github/czyzby/example/common/Common.kt | 12 ++++++++++++ gradle.properties | 2 +- server/build.gradle | 2 ++ .../czyzby/example/server/ServerTest.kt | 17 +++++++++++++++++ 8 files changed, 58 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 00b5e5f..c5b6174 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,13 @@ buildscript { repositories { jcenter() + maven { url "https://kotlin.bintray.com/kotlinx" } maven { url 'https://plugins.gradle.org/m2/' } mavenCentral() } dependencies { classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: kotlinVersion + classpath group: 'org.jetbrains.kotlinx', name: 'kotlinx-gradle-serialization-plugin', version: kotlinSerializationVersion classpath group: 'com.moowork.gradle', name: 'gradle-node-plugin', version: nodePluginVersion } } @@ -16,6 +18,7 @@ subprojects { repositories { mavenLocal() + maven { url "https://kotlin.bintray.com/kotlinx" } jcenter() } } diff --git a/client/build.gradle b/client/build.gradle index e029e3c..92ae205 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -1,11 +1,13 @@ apply plugin: 'kotlin-platform-js' apply plugin: 'kotlin-dce-js' +apply plugin: 'kotlinx-serialization' apply plugin: 'com.moowork.node' dependencies { expectedBy project(':common') compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-js', version: kotlinVersion + compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-serialization-runtime-js', version: kotlinSerializationVersion testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test-js', version: kotlinVersion } diff --git a/client/src/test/kotlin/com/github/czyzby/example/client/ClientTest.kt b/client/src/test/kotlin/com/github/czyzby/example/client/ClientTest.kt index 8b935fa..9afcbfe 100644 --- a/client/src/test/kotlin/com/github/czyzby/example/client/ClientTest.kt +++ b/client/src/test/kotlin/com/github/czyzby/example/client/ClientTest.kt @@ -1,6 +1,10 @@ +@file:Suppress("unused") + package com.github.czyzby.example.client +import com.github.czyzby.example.common.CommonData import com.github.czyzby.example.common.getAnswer +import kotlinx.serialization.protobuf.ProtoBuf import kotlin.test.Test import kotlin.test.asserter @@ -11,4 +15,19 @@ class ServerTest { fun the_answer_should_be_correct() { asserter.assertEquals("The answer must be 42.", 42, getAnswer()) } + + // kotlinx-serialization usage example: + + @Test + fun should_correctly_serialize_CommonData_in_JS() { + // Given: + val data = CommonData(id = 42, name = "John") + + // When: + val serialized = ProtoBuf.dump(data) + + // Then: + val deserialized = ProtoBuf.load(serialized) + asserter.assertEquals("Deserialized object should be equal to the original", data, deserialized) + } } diff --git a/common/build.gradle b/common/build.gradle index 15a339d..42e681b 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,7 +1,9 @@ apply plugin: 'kotlin-platform-common' +apply plugin: 'kotlinx-serialization' dependencies { compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-common', version: kotlinVersion + compileOnly group: 'org.jetbrains.kotlinx', name: 'kotlinx-serialization-runtime', version: kotlinSerializationVersion testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test-common', version: kotlinVersion testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test-annotations-common', version: kotlinVersion } diff --git a/common/src/main/kotlin/com/github/czyzby/example/common/Common.kt b/common/src/main/kotlin/com/github/czyzby/example/common/Common.kt index b720fef..4a9e390 100644 --- a/common/src/main/kotlin/com/github/czyzby/example/common/Common.kt +++ b/common/src/main/kotlin/com/github/czyzby/example/common/Common.kt @@ -1,8 +1,20 @@ package com.github.czyzby.example.common +import kotlinx.serialization.SerialId +import kotlinx.serialization.Serializable + /** * Must be implemented by all modules and return the one and only answer Answer to the Ultimate Question of Life, * the Universe, and Everything. This is not the best example of a multi-platform usage, since we expect it to * return the same number on all platforms, but you get the idea. */ expect fun getAnswer(): Int + + +// kotlinx-serialization usage example: + +@Serializable +data class CommonData( + @SerialId(1) val id: Int, + @SerialId(2) val name: String +) diff --git a/gradle.properties b/gradle.properties index c30140e..7130773 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ javaVersion=1.8 kotlinVersion=1.2.21 +kotlinSerializationVersion=0.4.1 junitVersion=4.12 nodePluginVersion=1.2.0 - diff --git a/server/build.gradle b/server/build.gradle index b54773c..db50bf1 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'kotlin-platform-jvm' +apply plugin: 'kotlinx-serialization' apply plugin: 'application' mainClassName = 'com.github.czyzby.example.server.Main' @@ -15,6 +16,7 @@ dependencies { expectedBy project(':common') compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: kotlinVersion + compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-serialization-runtime', version: kotlinSerializationVersion testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test', version: kotlinVersion testCompile group: 'org.jetbrains.kotlin', name: 'kotlin-test-junit', version: kotlinVersion diff --git a/server/src/test/kotlin/com/github/czyzby/example/server/ServerTest.kt b/server/src/test/kotlin/com/github/czyzby/example/server/ServerTest.kt index e2c2c04..0865108 100644 --- a/server/src/test/kotlin/com/github/czyzby/example/server/ServerTest.kt +++ b/server/src/test/kotlin/com/github/czyzby/example/server/ServerTest.kt @@ -1,6 +1,8 @@ package com.github.czyzby.example.server +import com.github.czyzby.example.common.CommonData import com.github.czyzby.example.common.getAnswer +import kotlinx.serialization.protobuf.ProtoBuf import org.junit.Assert.assertEquals import kotlin.test.Test @@ -12,4 +14,19 @@ class ServerTest { fun `just to be sure, the answer should be correct`() { assertEquals(42, getAnswer()) } + + // kotlinx-serialization usage example: + + @Test + fun `should correctly serialize CommonData on JVM`() { + // Given: + val data = CommonData(id = 42, name = "John") + + // When: + val serialized = ProtoBuf.dump(data) + + // Then: + val deserialized = ProtoBuf.load(serialized) + assertEquals(data, deserialized) + } }