Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
org.jetbrains.kotlinx:kotlinx-serialization-json
Advanced tools
Kotlin multiplatform serialization runtime library
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.
You can find auto-generated documentation website on kotlinlang.org.
New versions of the serialization plugin are released in tandem with each new Kotlin compiler version.
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.
To set up kotlinx.serialization, you have to do two things:
You can set up the serialization plugin with the Kotlin plugin using the Gradle plugins DSL:
Kotlin DSL:
plugins {
kotlin("jvm") version "2.0.0" // or kotlin("multiplatform") or any other kotlin plugin
kotlin("plugin.serialization") version "2.0.0"
}
Groovy DSL:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '2.0.0'
id 'org.jetbrains.kotlin.plugin.serialization' version '2.0.0'
}
Kotlin versions before 1.4.0 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 { mavenCentral() }
dependencies {
val kotlinVersion = "2.0.0"
classpath(kotlin("gradle-plugin", version = kotlinVersion))
classpath(kotlin("serialization", version = kotlinVersion))
}
}
Groovy DSL:
buildscript {
ext.kotlin_version = '2.0.0'
repositories { mavenCentral() }
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, 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 {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
}
Groovy DSL:
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1"
}
We also provide
kotlinx-serialization-core
artifact that contains all serialization API but does not have a bundled serialization format with it
By default, proguard rules are supplied with the library. These rules keep serializers for all serializable classes that are retained after shrinking, so you don't need additional setup.
However, these rules do not affect serializable classes if they have named companion objects.
If you want to serialize classes with named companion objects, you need to add and edit rules below to your proguard-rules.pro
configuration.
Note that the rules for R8 differ depending on the compatibility mode used.
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
# If you have any, replace classes with those containing named companion objects.
-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
-if @kotlinx.serialization.Serializable class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
static **$* *;
}
-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
static <1>$$serializer INSTANCE;
}
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
# If you have any, replace classes with those containing named companion objects.
-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
-if @kotlinx.serialization.Serializable class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
static **$* *;
}
-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
static <1>$$serializer INSTANCE;
}
# Keep both serializer and serializable classes to save the attribute InnerClasses
-keepclasseswithmembers, allowshrinking, allowobfuscation, allowaccessmodification class
com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
com.example.myapplication.HasNamedCompanion2
{
*;
}
In case you want to exclude serializable classes that are used, but never serialized at runtime, you will need to write custom rules with narrower class specifications.
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 {
// Works as common dependency as well as the platform one
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$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>2.0.0</kotlin.version>
<serialization.version>1.7.1</serialization.version>
</properties>
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>
To setup the Kotlin compiler plugin for Bazel, follow the
example
from the rules_kotlin
repository.
FAQs
Unknown package
We found that org.jetbrains.kotlinx:kotlinx-serialization-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.