|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| .github/ISSUE_TEMPLATE/ | H | 25-Apr-2025 | - | 37 | 27 |
| .idea/ | H | 25-Apr-2025 | - | 62 | 62 |
| benchmark/ | H | 25-Apr-2025 | - | 67,848 | 67,478 |
| bom/ | H | 25-Apr-2025 | - | 42 | 38 |
| buildSrc/ | H | 25-Apr-2025 | - | 437 | 334 |
| core/ | H | 25-Apr-2025 | - | 14,131 | 8,381 |
| docs/ | H | 25-Apr-2025 | - | 6,805 | 4,242 |
| dokka/ | H | 25-Apr-2025 | - | 63 | 44 |
| dokka-templates/ | H | 25-Apr-2025 | - | 12 | 9 |
| formats/ | H | 25-Apr-2025 | - | 38,482 | 28,677 |
| gradle/ | H | 25-Apr-2025 | - | 615 | 518 |
| guide/ | H | 25-Apr-2025 | - | 4,209 | 3,145 |
| integration-test/ | H | 25-Apr-2025 | - | 2,279 | 1,693 |
| kotlin-js-store/ | H | 25-Apr-2025 | - | 555 | 469 |
| license/ | H | 25-Apr-2025 | - | 25 | 22 |
| rules/ | H | 25-Apr-2025 | - | 49 | 41 |
| .gitignore | H A D | 25-Apr-2025 | 237 | 23 | 17 |
| Android.bp | H A D | 25-Apr-2025 | 1.7 KiB | 48 | 46 |
| CHANGELOG.md | H A D | 25-Apr-2025 | 76.6 KiB | 1,324 | 1,005 |
| CODE_OF_CONDUCT.md | H A D | 25-Apr-2025 | 269 | 5 | 2 |
| CONTRIBUTING.md | H A D | 25-Apr-2025 | 5.5 KiB | 100 | 81 |
| LICENSE | H A D | 25-Apr-2025 | 11.1 KiB | 201 | 169 |
| LICENSE.txt | H A D | 25-Apr-2025 | 11.1 KiB | 201 | 169 |
| METADATA | H A D | 25-Apr-2025 | 750 | 21 | 19 |
| MODULE_LICENSE_APACHE2 | HD | 25-Apr-2025 | 0 | | |
| OWNERS | H A D | 25-Apr-2025 | 51 | 2 | 1 |
| README.md | H A D | 25-Apr-2025 | 11.1 KiB | 324 | 249 |
| RELEASING.md | H A D | 25-Apr-2025 | 3.8 KiB | 79 | 55 |
| build.gradle | H A D | 25-Apr-2025 | 10.3 KiB | 255 | 218 |
| gradle.properties | H A D | 25-Apr-2025 | 900 | 36 | 26 |
| gradlew | H A D | 25-Apr-2025 | 7.9 KiB | 235 | 98 |
| gradlew.bat | H A D | 25-Apr-2025 | 2.7 KiB | 90 | 68 |
| knit.properties | H A D | 25-Apr-2025 | 93 | 6 | 4 |
| settings.gradle | H A D | 25-Apr-2025 | 1.4 KiB | 43 | 29 |
README.md
1# Kotlin multiplatform / multi-format reflectionless serialization
2
3[](https://kotlinlang.org/docs/components-stability.html)
4[](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
5[](http://www.apache.org/licenses/LICENSE-2.0)
6[](https://teamcity.jetbrains.com/viewType.html?buildTypeId=KotlinTools_KotlinxSerialization_Ko&guest=1)
7[](http://kotlinlang.org)
8[](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-core/1.6.3)
9[](https://kotlinlang.org/api/kotlinx.serialization/)
10[](https://kotlinlang.slack.com/messages/serialization/)
11
12Kotlin serialization consists of a compiler plugin, that generates visitor code for serializable classes,
13 runtime library with core serialization API and support libraries with various serialization formats.
14
15* Supports Kotlin classes marked as `@Serializable` and standard collections.
16* Provides [JSON](formats/README.md#JSON), [Protobuf](formats/README.md#ProtoBuf), [CBOR](formats/README.md#CBOR), [Hocon](formats/README.md#HOCON) and [Properties](formats/README.md#properties) formats.
17* Complete multiplatform support: JVM, JS and Native.
18
19## Table of contents
20
21<!--- TOC -->
22
23* [Introduction and references](#introduction-and-references)
24* [Setup](#setup)
25 * [Gradle](#gradle)
26 * [1) Setting up the serialization plugin](#1-setting-up-the-serialization-plugin)
27 * [2) Dependency on the JSON library](#2-dependency-on-the-json-library)
28 * [Android](#android)
29 * [Multiplatform (Common, JS, Native)](#multiplatform-common-js-native)
30 * [Maven](#maven)
31 * [Bazel](#bazel)
32
33<!--- END -->
34
35* **Additional links**
36 * [Kotlin Serialization Guide](docs/serialization-guide.md)
37 * [Full API reference](https://kotlinlang.org/api/kotlinx.serialization/)
38 * [Submitting issues and PRs](CONTRIBUTING.md)
39 * [Building this library](docs/building.md)
40
41## Introduction and references
42
43Here is a small example.
44
45```kotlin
46import kotlinx.serialization.*
47import kotlinx.serialization.json.*
48
49@Serializable
50data class Project(val name: String, val language: String)
51
52fun main() {
53 // Serializing objects
54 val data = Project("kotlinx.serialization", "Kotlin")
55 val string = Json.encodeToString(data)
56 println(string) // {"name":"kotlinx.serialization","language":"Kotlin"}
57 // Deserializing back into objects
58 val obj = Json.decodeFromString<Project>(string)
59 println(obj) // Project(name=kotlinx.serialization, language=Kotlin)
60}
61```
62
63> You can get the full code [here](guide/example/example-readme-01.kt).
64
65<!--- TEST_NAME ReadmeTest -->
66
67<!--- TEST
68{"name":"kotlinx.serialization","language":"Kotlin"}
69Project(name=kotlinx.serialization, language=Kotlin)
70-->
71
72**Read the [Kotlin Serialization Guide](docs/serialization-guide.md) for all details.**
73
74You can find auto-generated documentation website on [kotlinlang.org](https://kotlinlang.org/api/kotlinx.serialization/).
75
76## Setup
77
78[New versions](https://plugins.gradle.org/plugin/org.jetbrains.kotlin.plugin.serialization) of the serialization plugin are released in tandem with each new Kotlin compiler version.
79
80Using Kotlin Serialization requires Kotlin compiler `1.4.0` or higher.
81Make sure you have the corresponding Kotlin plugin installed in the IDE, no additional plugins for IDE are required.
82
83### Gradle
84
85To set up kotlinx.serialization, you have to do two things:
861) Add the **[serialization plugin](#1-setting-up-the-serialization-plugin)**.
872) Add the **[serialization library dependency](#2-dependency-on-the-json-library)**.
88
89#### 1) Setting up the serialization plugin
90
91You can set up the serialization plugin with the Kotlin plugin using the
92[Gradle plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
93
94Kotlin DSL:
95
96```kotlin
97plugins {
98 kotlin("jvm") version "1.9.22" // or kotlin("multiplatform") or any other kotlin plugin
99 kotlin("plugin.serialization") version "1.9.22"
100}
101```
102
103Groovy DSL:
104
105```gradle
106plugins {
107 id 'org.jetbrains.kotlin.multiplatform' version '1.9.22'
108 id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.22'
109}
110```
111
112> Kotlin versions before 1.4.0 are not supported by the stable release of Kotlin serialization.
113
114<details>
115 <summary>Using <code>apply plugin</code> (the old way)</summary>
116
117First, you have to add the serialization plugin to your classpath as the other [compiler plugins](https://kotlinlang.org/docs/reference/compiler-plugins.html):
118
119Kotlin DSL:
120
121```kotlin
122buildscript {
123 repositories { mavenCentral() }
124
125 dependencies {
126 val kotlinVersion = "1.9.22"
127 classpath(kotlin("gradle-plugin", version = kotlinVersion))
128 classpath(kotlin("serialization", version = kotlinVersion))
129 }
130}
131```
132
133Groovy DSL:
134
135```gradle
136buildscript {
137 ext.kotlin_version = '1.9.22'
138 repositories { mavenCentral() }
139
140 dependencies {
141 classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
142 }
143}
144```
145
146Then you can `apply plugin` (example in Groovy):
147
148```gradle
149apply plugin: 'kotlin' // or 'kotlin-multiplatform' for multiplatform projects
150apply plugin: 'kotlinx-serialization'
151```
152</details>
153
154#### 2) Dependency on the JSON library
155
156After setting up the plugin, you have to add a dependency on the serialization library.
157Note that while the plugin has version the same as the compiler one, runtime library has different coordinates, repository and versioning.
158
159Kotlin DSL:
160
161```kotlin
162repositories {
163 mavenCentral()
164}
165
166dependencies {
167 implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
168}
169```
170
171Groovy DSL:
172
173```gradle
174repositories {
175 mavenCentral()
176}
177
178dependencies {
179 implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
180}
181```
182
183>We also provide `kotlinx-serialization-core` artifact that contains all serialization API but does not have a bundled serialization format with it
184
185### Android
186
187By default, proguard rules are supplied with the library.
188[These rules](rules/common.pro) keep serializers for _all_ serializable classes that are retained after shrinking,
189so you don't need additional setup.
190
191**However, these rules do not affect serializable classes if they have named companion objects.**
192
193If you want to serialize classes with named companion objects, you need to add and edit rules below to your `proguard-rules.pro` configuration.
194
195Note that the rules for R8 differ depending on the [compatibility mode](https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md) used.
196
197<details>
198<summary>Example of named companion rules for ProGuard and R8 compatibility mode</summary>
199
200```proguard
201# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
202# If you have any, replace classes with those containing named companion objects.
203-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
204
205-if @kotlinx.serialization.Serializable class
206com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
207com.example.myapplication.HasNamedCompanion2
208{
209 static **$* *;
210}
211-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
212 static <1>$$serializer INSTANCE;
213}
214```
215</details>
216
217
218<details>
219<summary>Example of named companion rules for R8 full mode</summary>
220
221```proguard
222# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
223# If you have any, replace classes with those containing named companion objects.
224-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
225
226-if @kotlinx.serialization.Serializable class
227com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
228com.example.myapplication.HasNamedCompanion2
229{
230 static **$* *;
231}
232-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
233 static <1>$$serializer INSTANCE;
234}
235
236# Keep both serializer and serializable classes to save the attribute InnerClasses
237-keepclasseswithmembers, allowshrinking, allowobfuscation, allowaccessmodification class
238com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
239com.example.myapplication.HasNamedCompanion2
240{
241 *;
242}
243```
244</details>
245
246In case you want to exclude serializable classes that are used, but never serialized at runtime,
247you will need to write custom rules with narrower [class specifications](https://www.guardsquare.com/manual/configuration/usage).
248
249### Multiplatform (Common, JS, Native)
250
251Most of the modules are also available for Kotlin/JS and Kotlin/Native.
252You can add dependency to the required module right to the common source set:
253```gradle
254commonMain {
255 dependencies {
256 // Works as common dependency as well as the platform one
257 implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
258 }
259}
260```
261The same artifact coordinates can be used to depend on platform-specific artifact in platform-specific source-set.
262
263### Maven
264
265Ensure the proper version of Kotlin and serialization version:
266
267```xml
268<properties>
269 <kotlin.version>1.9.22</kotlin.version>
270 <serialization.version>1.6.3</serialization.version>
271</properties>
272```
273
274Add serialization plugin to Kotlin compiler plugin:
275
276```xml
277<build>
278 <plugins>
279 <plugin>
280 <groupId>org.jetbrains.kotlin</groupId>
281 <artifactId>kotlin-maven-plugin</artifactId>
282 <version>${kotlin.version}</version>
283 <executions>
284 <execution>
285 <id>compile</id>
286 <phase>compile</phase>
287 <goals>
288 <goal>compile</goal>
289 </goals>
290 </execution>
291 </executions>
292 <configuration>
293 <compilerPlugins>
294 <plugin>kotlinx-serialization</plugin>
295 </compilerPlugins>
296 </configuration>
297 <dependencies>
298 <dependency>
299 <groupId>org.jetbrains.kotlin</groupId>
300 <artifactId>kotlin-maven-serialization</artifactId>
301 <version>${kotlin.version}</version>
302 </dependency>
303 </dependencies>
304 </plugin>
305 </plugins>
306</build>
307```
308
309Add dependency on serialization runtime library:
310
311```xml
312<dependency>
313 <groupId>org.jetbrains.kotlinx</groupId>
314 <artifactId>kotlinx-serialization-json</artifactId>
315 <version>${serialization.version}</version>
316</dependency>
317```
318
319### Bazel
320
321To setup the Kotlin compiler plugin for Bazel, follow [the
322example](https://github.com/bazelbuild/rules_kotlin/tree/master/examples/plugin/src/serialization)
323from the `rules_kotlin` repository.
324