commit | da728dd3f323c80653687526f98fd5e3fe160dbc | [log] [tgz] |
---|---|---|
author | Leonid Startsev <sandwwraith@gmail.com> | Wed Mar 07 12:41:47 2018 +0300 |
committer | Leonid Startsev <sandwwraith@gmail.com> | Wed Mar 07 12:41:47 2018 +0300 |
tree | 0dfd1990bfca2767a1f39c43505bfa5f07e02d41 | |
parent | 5aed5c050f8259d174723b74e5d2e94ac833c649 [diff] |
Update runtime library version to match plugin version. Update examples to use latest version of compiler, plugin and runtime. Update Gradle to run on build agents with Java 9.
Kotlin serialization support consists of three parts: a gradle compiler plugin, which produces visitor/serializer code for objects, an IntelliJ plugin and a runtime library.
@Serializable
and standard collections.This project contains the runtime library. Runtime library provides:
KInput
, KOutput
).ElementValueInput/Output
, NamedValueInput/Output
, ElementValueTransformer
)You can open example projects for JVM or JS to get started playing with it.
import kotlinx.serialization.* import kotlinx.serialization.json.JSON @Serializable data class Data(val a: Int, @Optional val b: String = "42") fun main(args: Array<String>) { println(JSON.stringify(Data(42))) // {"a": 42, "b": "42"} val obj = JSON.parse<Data>("""{"a":42}""") // Data(a=42, b="42") }
To learn more about JSON usage and other formats, see usage. More examples of various kinds of Kotlin classes that can be serialized can be found here.
Using Kotlin Serialization requires Kotlin compiler 1.1.50
or higher, recommended version is 1.2.21
. Also, it's recommended to install additional IDEA plugin for better IDE experience. Otherwise, some valid code will be shown as red and builds will have to be launched from console or build system tasks panel. Example projects on JVM are available for Gradle and Maven.
Ensure the proper version of Kotlin and add dependencies on plugin in addition to Kotlin compiler:
buildscript { ext.kotlin_version = '1.2.10' ext.serialization_version = '0.4.1' repositories { jcenter() maven { url "https://kotlin.bintray.com/kotlinx" } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serialization_version" } }
Don't forget to apply the plugin:
apply plugin: 'kotlin' apply plugin: 'kotlinx-serialization'
Add serialization runtime library in addition to Kotlin standard library.
repositories { jcenter() maven { url "https://kotlin.bintray.com/kotlinx" } } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version" }
Library should work on Android "as is". If you're using proguard, you need to add this to your proguard-rules.pro
:
-keepattributes *Annotation*, InnerClasses -dontnote kotlinx.serialization.SerializationKt -keep,includedescriptorclasses class com.yourcompany.yourpackage.**$$serializer { *; } # <-- change package name to your app's -keepclassmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's *** Companion; } -keepclasseswithmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's kotlinx.serialization.KSerializer serializer(...); }
You may also want to keep all custom serializers you've defined.
Ensure the proper version of Kotlin and serialization version:
<properties> <kotlin.version>1.2.10</kotlin.version> <serialization.version>0.4.1</serialization.version> </properties>
Include bintray repository for both library and plugin:
<repositories> <repository> <id>bintray-kotlin-kotlinx</id> <name>bintray</name> <url>https://kotlin.bintray.com/kotlinx</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>bintray-kotlin-kotlinx</id> <name>bintray-plugins</name> <url>https://kotlin.bintray.com/kotlinx</url> </pluginRepository> </pluginRepositories>
Add serialization plugin to Kotlin compiler plugin:
<build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <compilerPlugins> <plugin>kotlinx-serialization</plugin> </compilerPlugins> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-maven-serialization-plugin</artifactId> <version>${serialization.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
Add dependency on serialization runtime library:
<dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-serialization-runtime</artifactId> <version>${serialization.version}</version> </dependency>
Replace dependency on kotlinx-serialization-runtime
with kotlinx-serialization-runtime-js
or kotlinx-serialization-runtime-common
to use it in JavaScript and common projects, respectively. JavaScript example is located at example-js
folder.
Instead of using Gradle or Maven, IntelliJ IDEA relies on its own build system when analyzing and running code from within IDE. Because serialization is still highly experimental, it is shipped as a separate artifact from "big" Kotlin IDEA plugin. You can download additional IDEA plugin for working with projects that uses serialization from its TeamCity build page. In IDEA, open Settings - Plugins - Install plugin from disk...
and select downloaded .zip or .jar file. This installation will allow you to run code/tests from IDEA.
In case of issues with IDE, try to use gradle for running builds: Settings - Build, Execution, Deployment - Build Tools - Gradle - Runner -
tick Delegate IDE build/run actions to gradle
; or launch builds from console.