Name Date Size #Lines LOC

..--

.github/ISSUE_TEMPLATE/H25-Apr-2025-3727

.idea/H25-Apr-2025-6262

benchmark/H25-Apr-2025-67,84867,478

bom/H25-Apr-2025-4238

buildSrc/H25-Apr-2025-437334

core/H25-Apr-2025-14,1318,381

docs/H25-Apr-2025-6,8054,242

dokka/H25-Apr-2025-6344

dokka-templates/H25-Apr-2025-129

formats/H25-Apr-2025-38,48228,677

gradle/H25-Apr-2025-615518

guide/H25-Apr-2025-4,2093,145

integration-test/H25-Apr-2025-2,2791,693

kotlin-js-store/H25-Apr-2025-555469

license/H25-Apr-2025-2522

rules/H25-Apr-2025-4941

.gitignoreH A D25-Apr-2025237 2317

Android.bpH A D25-Apr-20251.7 KiB4846

CHANGELOG.mdH A D25-Apr-202576.6 KiB1,3241,005

CODE_OF_CONDUCT.mdH A D25-Apr-2025269 52

CONTRIBUTING.mdH A D25-Apr-20255.5 KiB10081

LICENSEH A D25-Apr-202511.1 KiB201169

LICENSE.txtH A D25-Apr-202511.1 KiB201169

METADATAH A D25-Apr-2025750 2119

MODULE_LICENSE_APACHE2HD25-Apr-20250

OWNERSH A D25-Apr-202551 21

README.mdH A D25-Apr-202511.1 KiB324249

RELEASING.mdH A D25-Apr-20253.8 KiB7955

build.gradleH A D25-Apr-202510.3 KiB255218

gradle.propertiesH A D25-Apr-2025900 3626

gradlewH A D25-Apr-20257.9 KiB23598

gradlew.batH A D25-Apr-20252.7 KiB9068

knit.propertiesH A D25-Apr-202593 64

settings.gradleH A D25-Apr-20251.4 KiB4329

README.md

1# Kotlin multiplatform / multi-format reflectionless serialization
2
3[![Kotlin Stable](https://kotl.in/badges/stable.svg)](https://kotlinlang.org/docs/components-stability.html)
4[![JetBrains official project](https://jb.gg/badges/official.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
5[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
6[![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)
7[![Kotlin](https://img.shields.io/badge/kotlin-1.9.22-blue.svg?logo=kotlin)](http://kotlinlang.org)
8[![Maven Central](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-serialization-core/1.6.3)](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-core/1.6.3)
9[![KDoc link](https://img.shields.io/badge/API_reference-KDoc-blue)](https://kotlinlang.org/api/kotlinx.serialization/)
10[![Slack channel](https://img.shields.io/badge/chat-slack-blue.svg?logo=slack)](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