commit | 549b9ccb8a16f53d4f5f2b87027acc15f542b0e4 | [log] [tgz] |
---|---|---|
author | Leonid Startsev <sandwwraith@users.noreply.github.com> | Mon Sep 21 14:05:05 2020 +0300 |
committer | GitHub <noreply@github.com> | Mon Sep 21 14:05:05 2020 +0300 |
tree | 4c81f3e0b4b77b2364d0163fa74c8856a40b98c7 | |
parent | c041c2c92b96c353958843be78084bfd3a4fc1de [diff] |
Prepare RC2 release (#1087) Update to Kotlin 1.4.10
Kotlin serialization consists of a compiler plugin, that generates visitor code for serializable classes, runtime library with core serialization API and support libraries with various serialization formats.
@Serializable
and standard collections.Here is a small example.
import kotlinx.serialization.* import kotlinx.serialization.json.* @Serializable data class Project(val name: String, val language: String) fun main() { // Serializing objects val data = Project("kotlinx.serialization", "Kotlin") val string = Json.encodeToString(data) println(string) // {"name":"kotlinx.serialization","language":"Kotlin"} // Deserializing back into objects val obj = Json.decodeFromString<Project>(string) println(obj) // Project(name=kotlinx.serialization, language=Kotlin) }
You can get the full code here.
Read the Kotlin Serialization Guide for all details.
Kotlin serialization plugin is shipped with the Kotlin compiler distribution, and the IDEA plugin is bundled into the Kotlin plugin.
Using Kotlin Serialization requires Kotlin compiler 1.4.0
or higher. Make sure you have the corresponding Kotlin plugin installed in the IDE, no additional plugins for IDE are required.
plugins
blockYou can set up the serialization plugin with the Kotlin plugin using Gradle plugins DSL:
Kotlin DSL:
plugins { kotlin("jvm") version "1.4.10" // or kotlin("multiplatform") or any other kotlin plugin kotlin("plugin.serialization") version "1.4.10" }
Groovy DSL:
plugins { id 'org.jetbrains.kotlin.multiplatform' version '1.4.10' id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10' }
Kotlin versions before are not supported by the stable release of Kotlin serialization
apply plugin
(the old way)First, you have to add the serialization plugin to your classpath as the other compiler plugins:
Kotlin DSL:
buildscript { repositories { jcenter() } dependencies { val kotlinVersion = "1.4.10" classpath(kotlin("gradle-plugin", version = kotlinVersion)) classpath(kotlin("serialization", version = kotlinVersion)) } }
Groovy DSL:
buildscript { ext.kotlin_version = '1.4.10' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" } }
Then you can apply plugin
(example in Groovy):
apply plugin: 'kotlin' // or 'kotlin-multiplatform' for multiplatform projects apply plugin: 'kotlinx-serialization'
After setting up the plugin one way or another, you have to add a dependency on the serialization library. Note that while the plugin has version the same as the compiler one, runtime library has different coordinates, repository and versioning.
Kotlin DSL:
repositories { // artifacts are published to JCenter jcenter() } dependencies { implementation(kotlin("stdlib", KotlinCompilerVersion.VERSION)) // or "stdlib-jdk8" implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0-RC2") // JVM dependency }
Groovy DSL:
repositories { jcenter() } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // or "kotlin-stdlib-jdk8" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0-RC2" // JVM dependency }
We also provide kotlinx-serialization-core artifact that contains all serialization API but does not have bundled serialization format with it
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.
Most of the modules are also available for Kotlin/JS and Kotlin/Native. You can add dependency to the required module right to the common source set:
commonMain { dependencies { implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version" implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$serialization_version" } }
The same artifact coordinates can be used to depend on platform-specific artifact in platform-specific source-set.
Ensure the proper version of Kotlin and serialization version:
<properties> <kotlin.version>1.4.10</kotlin.version> <serialization.version>1.0.0-RC2</serialization.version> </properties>
You can also use JCenter or https://kotlin.bintray.com/kotlinx
Bintray repository.
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.kotlin</groupId> <artifactId>kotlin-maven-serialization</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
Add dependency on serialization runtime library:
<dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-serialization-json</artifactId> <version>${serialization.version}</version> </dependency>