kotlinx.serialization

Introduction: Kotlin cross-platform / multi-format serialization
More: Author   ReportBugs   
Tags:

JetBrains incubator project GitHub license TeamCity build Download

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.

  • Supports Kotlin classes marked as @Serializable and standard collections.
  • Supports JSON, CBOR, and Protobuf formats out-of-the-box.
  • The same code works on Kotlin/JVM and Kotlin/JS.

Runtime overview

This project contains the runtime library. Runtime library provides:

  • Interfaces which are called by compiler-generated code (KInput, KOutput).
  • Basic skeleton implementations of these interfaces in which you should override some methods if you want to implement custom data format (ElementValueInput/Output, NamedValueInput/Output, ElementValueTransformer)
  • Some internal classes like built-ins and collections serializers.
  • Ready-to-use serialization formats.
  • Other useful classes that benefit from serialization framework (e.g. object-to-Map transformer)

You can open example projects for JVM or JS to get started playing with it.

Table of contents

Quick example


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.

Setup

Using Kotlin Serialization requires Kotlin compiler 1.1.50 or higher, recommended version is 1.2.0. Example projects on JVM are available for Gradle and Maven.

Gradle/JVM

Ensure the proper version of Kotlin and add dependencies on plugin in addition to Kotlin compiler:

buildscript {
    ext.kotlin_version = '1.2.0'
    ext.serialization_version = '0.3'
    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 and reflection (optional). For now, library requires small amount of reflection on runtime to find corresponding serializer for root-level type. In the future, we plan to move all resolving to separate module so the runtime library itself would not contain dependency on kotlin-reflect.

repositories {
    jcenter()
    maven { url "https://kotlin.bintray.com/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
}

Maven/JVM

Ensure the proper version of Kotlin and serialization version:

<properties>
    <kotlin.version>1.2.0</kotlin.version>
    <serialization.version>0.3</serialization.version>
</properties>

Include bintray repository:

<repositories>
    <repository>
        <id>bintray-kotlin-kotlinx</id>
        <name>bintray</name>
        <url>https://kotlin.bintray.com/kotlinx</url>
    </repository>
</repositories>

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>

JavaScript and common

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.

Working in IntelliJ IDEA

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.

Android 开发经验分享
随手点击下面广告支持本站吧
 
Android 开发经验分享