Skip to content

Commit 5459c10

Browse files
elizarovqwwdfsad
andauthored
Docs: Kotlin Serialization Guide (#933)
Documentation from the docs folder is incorporated into a common story that spans six chapters and proceeds from the basic serialization to the experimental alternative and custom formats. The example files are automatically generated from the text with the Knit tool (guide/example folder) and are verified with the automatically generated tests (guide/test folder). The entry point to the new is docs/serialization-guide.md that contains the overall table of contents and links to the specific chapters. The old docs that are being incorporated: * cbor_byte_string.md -> formats.md * custom_serializers.md -> serializers.md * examples.md -> basic-serialization.md * json_transformations.md -> json.md * old12.md (removed completely as obsolete) * runtime_usage.md -> various chapters Additional changes include: * Top-level README.md file is cleaned up from legacy information and also contains a modern knitted example. * More consistent error messages in various exceptions. * Minor corrections to KDocs and APIs. * Better style.css for the documentation that is generated by Dokka. * Improved update_docs.sh to put temporary files inside the build folder. Co-authored-by: Vsevolod Tolstopyatov <[email protected]>
1 parent 65fdbc7 commit 5459c10

File tree

152 files changed

+8924
-1625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+8924
-1625
lines changed

README.md

+58-100
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,100 @@
11
# Kotlin multiplatform / multi-format reflectionless serialization
22

3-
[![JetBrains incubator project](https://jb.gg/badges/incubator.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
3+
[![official JetBrains project](https://jb.gg/badges/official.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
44
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
55
[![TeamCity build](https://img.shields.io/teamcity/http/teamcity.jetbrains.com/s/KotlinTools_KotlinxSerialization_Ko.svg)](https://teamcity.jetbrains.com/viewType.html?buildTypeId=KotlinTools_KotlinxSerialization_Ko&guest=1)
6-
[![Download](https://api.bintray.com/packages/kotlin/kotlinx/kotlinx.serialization.runtime/images/download.svg) ](https://bintray.com/kotlin/kotlinx/kotlinx.serialization.runtime/_latestVersion)
6+
[![Download](https://api.bintray.com/packages/kotlin/kotlinx/kotlinx.serialization.runtime/images/download.svg?version=0.20.0) ](https://bintray.com/kotlin/kotlinx/kotlinx.serialization.runtime/0.20.0)
77

88
Kotlin serialization consists of a compiler plugin, that generates visitor code for serializable classes,
9-
runtime library with core serialization API and JSON format, and support libraries with ProtoBuf, CBOR and properties formats.
9+
runtime libraries with core serialization API and JSON format, and support libraries with ProtoBuf, CBOR and properties formats.
1010

1111
* Supports Kotlin classes marked as `@Serializable` and standard collections.
12-
* Provides JSON, [CBOR](formats/README.md#CBOR), and [Protobuf](formats/README.md#ProtoBuf) formats.
12+
* Provides JSON (as a part of the core library), [Protobuf](formats/README.md#ProtoBuf), [CBOR](formats/README.md#CBOR), [Hocon](formats/README.md#HOCON) and [Properties](formats/README.md#properties) formats.
1313
* Complete multiplatform support: JVM, JS and Native.
1414

1515
## Table of contents
1616

17-
* [Quick example](#quick-example)
18-
* [Runtime overview](#runtime-overview)
19-
* [Current status](#current-project-status)
20-
* [Library installing](#setup)
21-
+ [Gradle](#gradle)
22-
+ [Android/JVM](#androidjvm)
23-
+ [Multiplatform (common, JS, Native)](#multiplatform-common-js-native)
24-
+ [Maven/JVM](#mavenjvm)
25-
+ [Incompatible changes from older versions](#incompatible-changes)
26-
* [Troubleshooting IntelliJ IDEA](#troubleshooting-intellij-idea)
27-
* [Usage](docs/runtime_usage.md)
28-
* [More examples of supported Kotlin classes](docs/examples.md)
29-
* [Writing JSON transformations](docs/json_transformations.md)
30-
* [Writing custom serializers](docs/custom_serializers.md)
31-
* [Multiplatform polymorphic serialization](docs/polymorphism.md)
32-
* [Add-on formats](formats/README.md)
33-
* [Building library and compiler plugin from source](docs/building.md)
34-
* [Instructions for old versions under Kotlin 1.2 and migration guide](docs/old12.md)
35-
36-
37-
## Quick example
17+
<!--- TOC -->
18+
19+
* [Introduction and references](#introduction-and-references)
20+
* [Setup](#setup)
21+
* [Gradle](#gradle)
22+
* [Using the `plugins` block](#using-the-plugins-block)
23+
* [Using `apply plugin` (the old way)](#using-apply-plugin-the-old-way)
24+
* [Dependency on the runtime library](#dependency-on-the-runtime-library)
25+
* [Android/JVM](#android/jvm)
26+
* [Multiplatform (common, JS, Native)](#multiplatform-common-js-native)
27+
* [Maven/JVM](#maven/jvm)
28+
29+
<!--- END -->
30+
31+
## Introduction and references
32+
33+
Here is a small example.
3834

3935
```kotlin
4036
import kotlinx.serialization.*
4137
import kotlinx.serialization.json.*
4238

43-
@Serializable
44-
data class Data(val a: Int, val b: String = "42")
39+
@Serializable
40+
data class Project(val name: String, val language: String)
4541

4642
fun main() {
47-
// Json also has .Default configuration which provides more reasonable settings,
48-
// but is subject to change in future versions
49-
val json = Json(JsonConfiguration.Stable)
50-
// serializing objects
51-
val jsonData = json.stringify(Data.serializer(), Data(42))
52-
// serializing lists
53-
val jsonList = json.stringify(Data.serializer().list, listOf(Data(42)))
54-
println(jsonData) // {"a": 42, "b": "42"}
55-
println(jsonList) // [{"a": 42, "b": "42"}]
56-
57-
// parsing data back
58-
val obj = json.parse(Data.serializer(), """{"a":42}""") // b is optional since it has default value
59-
println(obj) // Data(a=42, b="42")
43+
// Serializing objects
44+
val data = Project("kotlinx.serialization", "Kotlin")
45+
val string = Json.encodeToString(data)
46+
println(string) // {"name":"kotlinx.serialization","language":"Kotlin"}
47+
// Deserializing back into objects
48+
val obj = Json.decodeFromString<Project>(string)
49+
println(obj) // Project(name=kotlinx.serialization, langauge=Kotlin)
6050
}
61-
```
62-
63-
## Runtime overview
51+
```
6452

65-
This project contains the runtime library. Runtime library provides:
53+
> You can get the full code [here](guide/example/example-readme-01.kt).
6654
67-
* Ready-to-use JSON serialization format.
68-
* Core primitives for plugin-generated code (`Encoder`, `Decoder`, `SerialDescriptor`).
69-
* Basic skeleton implementations of core primitives for custom serialization formats.
70-
* Built-ins and collections serializers.
55+
<!--- TEST_NAME ReadmeTest -->
7156

72-
You can open example projects for [JS](examples/example-js) and [JVM](examples/example-jvm).
57+
<!--- TEST
58+
{"name":"kotlinx.serialization","language":"Kotlin"}
59+
Project(name=kotlinx.serialization, language=Kotlin)
60+
-->
7361

74-
To learn more about JSON usage and other formats, see [usage](docs/runtime_usage.md).
75-
More examples of various kinds of Kotlin classes that can be serialized can be found [here](docs/examples.md).
76-
77-
## Current project status
78-
79-
Starting from Kotlin 1.3-RC2, serialization plugin is shipped with the rest of Kotlin compiler distribution, and the IDEA plugin is bundled into the Kotlin plugin.
80-
81-
Runtime library is under reconstruction to match the corresponding [KEEP](https://github.com/Kotlin/KEEP/blob/serialization/proposals/extensions/serialization.md), so some features described there can be not implemented yet. While library is stable and has successfully been used in various scenarios, there is no API compatibility guarantees between versions, that's why it is called experimental.
82-
This document describes setup for Kotlin 1.3 and higher. To watch instructions regarding 1.2, follow [this document](docs/old12.md).
62+
**Read the [Kotlin Serialization Guide](docs/serialization-guide.md) for all details.**
8363

8464
## Setup
8565

86-
Using Kotlin Serialization requires Kotlin compiler `1.3.30` or higher.
87-
Make sure that you have corresponding Kotlin plugin installed in the IDE.
88-
Since serialization is now bundled into Kotlin plugin, no additional plugins for IDE are required (but make sure you have deleted old additional plugin for 1.2, if you had one).
66+
Kotlin serialization plugin is shipped with the Kotlin compiler distribution, and the IDEA plugin is bundled into the Kotlin plugin.
67+
68+
Using Kotlin Serialization requires Kotlin compiler `1.4.0` or higher.
69+
Make sure you have the corresponding Kotlin plugin installed in the IDE, no additional plugins for IDE are required.
8970
Example projects on JVM are available for [Gradle](examples/example-jvm/build.gradle) and [Maven](examples/example-jvm/pom.xml).
9071

9172
### Gradle
9273

9374
#### Using the `plugins` block
9475

95-
You can setup the serialization plugin with the Kotlin plugin using [Gradle plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
76+
You can set up the serialization plugin with the Kotlin plugin using
77+
[Gradle plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
9678

9779
Kotlin DSL:
9880

9981
```kotlin
10082
plugins {
101-
kotlin("multiplatform") // or kotlin("jvm") or any other kotlin plugin
102-
kotlin("plugin.serialization") version "1.3.70"
83+
kotlin("jvm") version "1.4.0" // or kotlin("multiplatform") or any other kotlin plugin
84+
kotlin("plugin.serialization") version "1.4.0"
10385
}
104-
```
86+
```
87+
10588
Groovy DSL:
10689

10790
```gradle
10891
plugins {
109-
id 'org.jetbrains.kotlin.multiplatform' version '1.3.70' // or any other kotlin plugin
110-
id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.70'
92+
id 'org.jetbrains.kotlin.multiplatform' version '1.4.0'
93+
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.0'
11194
}
11295
```
11396

114-
Note: plugin marker for serialization has been published in Kotlin 1.3.50. If you need to use the earlier Kotlin version, see [KT-27612](https://youtrack.jetbrains.com/issue/KT-27612) for workaround with [plugin resolution rules](https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_resolution_rules).
97+
> Kotlin versions before are not supported by the stable release of Kotlin serialization
11598
11699
#### Using `apply plugin` (the old way)
117100

@@ -124,7 +107,7 @@ buildscript {
124107
repositories { jcenter() }
125108

126109
dependencies {
127-
val kotlinVersion = "1.3.70"
110+
val kotlinVersion = "1.4.0"
128111
classpath(kotlin("gradle-plugin", version = kotlinVersion))
129112
classpath(kotlin("serialization", version = kotlinVersion))
130113
}
@@ -135,7 +118,7 @@ Groovy DSL:
135118

136119
```gradle
137120
buildscript {
138-
ext.kotlin_version = '1.3.70'
121+
ext.kotlin_version = '1.4.0'
139122
repositories { jcenter() }
140123
141124
dependencies {
@@ -165,7 +148,7 @@ repositories {
165148

166149
dependencies {
167150
implementation(kotlin("stdlib", KotlinCompilerVersion.VERSION)) // or "stdlib-jdk8"
168-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0") // JVM dependency
151+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC") // JVM dependency
169152
}
170153
```
171154

@@ -178,7 +161,7 @@ repositories {
178161
179162
dependencies {
180163
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // or "kotlin-stdlib-jdk8"
181-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0" // JVM dependency
164+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC" // JVM dependency
182165
}
183166
```
184167

@@ -245,17 +228,14 @@ sourceSets {
245228
}
246229
```
247230

248-
JavaScript example is located at [`example-js`](examples/example-js) folder.
249-
Multiplatform example is located at [`example-multiplatform`](examples/example-multiplatform) folder.
250-
251231
### Maven/JVM
252232

253233
Ensure the proper version of Kotlin and serialization version:
254234

255235
```xml
256236
<properties>
257-
<kotlin.version>1.3.70</kotlin.version>
258-
<serialization.version>0.20.0</serialization.version>
237+
<kotlin.version>1.4.0</kotlin.version>
238+
<serialization.version>1.0.0-RC</serialization.version>
259239
</properties>
260240
```
261241

@@ -301,29 +281,7 @@ Add dependency on serialization runtime library:
301281
```xml
302282
<dependency>
303283
<groupId>org.jetbrains.kotlinx</groupId>
304-
<artifactId>kotlinx-serialization-runtime</artifactId>
284+
<artifactId>kotlinx-serialization-core</artifactId>
305285
<version>${serialization.version}</version>
306286
</dependency>
307287
```
308-
309-
### Incompatible changes
310-
311-
Library versions `0.20.0` and higher require Kotlin 1.3.70 and higher and incompatible with previous versions.
312-
313-
Library version `0.14.0` require Kotlin 1.3.60/61 and incompatible with other versions.
314-
315-
All versions of library before `0.13.0` are using Gradle metadata v0.4 and therefore it is recommended to use Gradle 4.8-5.1 to build.
316-
317-
Library versions `0.11.0` and higher require Kotlin 1.3.30 and higher and incompatible with previous versions.
318-
319-
All versions of library before `0.10.0` are using Gradle metadata v0.3 and therefore require Gradle 4.7 for build.
320-
321-
Maven plugin coordinates before Kotlin 1.3.20 were `kotlinx-maven-serialization-plugin`.
322-
323-
324-
## Troubleshooting IntelliJ IDEA
325-
326-
Serialization support should work out of the box, if you have `1.3.x` Kotlin plugin installed and have imported the project from Maven or Gradle with serialization enabled in their build scripts.
327-
If you have Kotlin `1.3.10` or lower, you have to delegate build to Gradle (`Settings - Build, Execution, Deployment - Build Tools - Gradle - Runner -` tick `Delegate IDE build/run actions to gradle`).
328-
Starting from `1.3.11`, no delegation is required.
329-
In case of problems, force project re-import from Gradle.

build.gradle

+18-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ buildscript {
1919

2020
ext.experimentalsInTestEnabled = ["-progressive", "-Xuse-experimental=kotlin.Experimental",
2121
"-Xuse-experimental=kotlin.ExperimentalMultiplatform",
22-
"-Xuse-experimental=kotlinx.serialization.UnsafeSerializationApi",
23-
"-Xuse-experimental=kotlinx.serialization.InternalSerializationApi"
22+
"-Xuse-experimental=kotlinx.serialization.InternalSerializationApi",
23+
"-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"
2424
]
2525

2626
/*
@@ -71,6 +71,7 @@ buildscript {
7171
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
7272
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
7373
classpath "org.jetbrains.kotlinx:binary-compatibility-validator:$validator_version"
74+
classpath "org.jetbrains.kotlinx:kotlinx-knit:$knit_version"
7475

7576
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
7677

@@ -86,9 +87,19 @@ ext.compilerVersion = org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION
8687
apply plugin: 'binary-compatibility-validator'
8788

8889
apiValidation {
89-
ignoredProjects += ["benchmark", "kotlinx-serialization"]
90+
ignoredProjects += ["benchmark", "guide", "kotlinx-serialization"]
9091
}
9192

93+
apply plugin: 'base'
94+
apply plugin: 'kotlinx-knit'
95+
96+
knit {
97+
siteRoot = "https://kotlin.github.io/kotlinx.serialization"
98+
}
99+
100+
// Build API docs for all modules with dokka before running Knit
101+
knitPrepare.dependsOn "dokka"
102+
92103
allprojects {
93104
group 'org.jetbrains.kotlinx'
94105

@@ -144,8 +155,10 @@ subprojects {
144155

145156
apply from: rootProject.file('gradle/teamcity.gradle')
146157
// Configure publishing for some artifacts
147-
if (project.name.contains("benchmark")) return
148-
apply from: rootProject.file('gradle/publishing.gradle')
158+
if (project.name != "benchmark" && project.name != "guide") {
159+
apply from: rootProject.file('gradle/publishing.gradle')
160+
}
161+
149162
}
150163

151164
apply from: rootProject.file('gradle/compilerVersion.gradle')

0 commit comments

Comments
 (0)