Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Added kotlinx-serialization support.
Browse files Browse the repository at this point in the history
  • Loading branch information
czyzby committed Feb 10, 2018
1 parent 9466b12 commit 2be6602
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
}
Expand All @@ -16,6 +18,7 @@ subprojects {

repositories {
mavenLocal()
maven { url "https://kotlin.bintray.com/kotlinx" }
jcenter()
}
}
Expand Down
2 changes: 2 additions & 0 deletions client/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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<CommonData>(serialized)
asserter.assertEquals("Deserialized object should be equal to the original", data, deserialized)
}
}
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions common/src/main/kotlin/com/github/czyzby/example/common/Common.kt
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
javaVersion=1.8
kotlinVersion=1.2.21
kotlinSerializationVersion=0.4.1
junitVersion=4.12
nodePluginVersion=1.2.0

2 changes: 2 additions & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'kotlin-platform-jvm'
apply plugin: 'kotlinx-serialization'
apply plugin: 'application'

mainClassName = 'com.github.czyzby.example.server.Main'
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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<CommonData>(serialized)
assertEquals(data, deserialized)
}
}

0 comments on commit 2be6602

Please sign in to comment.