-
Notifications
You must be signed in to change notification settings - Fork 662
Documentation rewrite - Get started page #3059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daniCsorbaJB
wants to merge
5
commits into
doc-restructuring-master
Choose a base branch
from
doc-restructuring-update-2
base: doc-restructuring-master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7ef54f
feat: adding the get started page
daniCsorbaJB c8e2920
implementing review comments from first round of review
daniCsorbaJB c31a5b7
implementing second round of comments for the get started page
daniCsorbaJB 537ff6b
fix: using slightly smoother wording for R8
daniCsorbaJB 68f4be6
update: clarifying Bazel support and addressing last comment about R8
daniCsorbaJB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| [//]: # (title: Get started with Kotlin serialization) | ||
|
|
||
| [Serialization](serialization.md) converts objects into a format that can be stored or transmitted and later reconstructed. | ||
|
|
||
| This tutorial shows you how to add the necessary plugins and dependencies for Kotlin serialization and how to serialize and deserialize objects in JSON format. | ||
|
|
||
| ## Add plugins and dependencies for Kotlin serialization | ||
|
|
||
| To include the `kotlinx.serialization` library in your project, add the corresponding plugin and dependency configuration based on your build tool. | ||
|
|
||
| > To set up the Kotlin compiler plugin for Bazel, see the example provided in the [rules_kotlin repository](https://github.com/bazelbuild/rules_kotlin/tree/master/examples/plugin/src/serialization). | ||
daniCsorbaJB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| > | ||
| {style="tip"} | ||
|
|
||
| ### Gradle | ||
|
|
||
| Add the following dependency to your `build.gradle(.kts)` file: | ||
|
|
||
| <tabs> | ||
| <tab id="kotlin" title="Kotlin"> | ||
|
|
||
| ```kotlin | ||
| // build.gradle.kts | ||
| plugins { | ||
| kotlin("plugin.serialization") version "%kotlinVersion%" | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%") | ||
| } | ||
| ``` | ||
|
|
||
| </tab> | ||
|
|
||
| <tab id="groovy" title="Groovy"> | ||
|
|
||
| ```groovy | ||
| // build.gradle | ||
| plugins { | ||
| id 'org.jetbrains.kotlin.plugin.serialization' version '%kotlinVersion%' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%' | ||
| } | ||
| ``` | ||
|
|
||
| </tab> | ||
|
|
||
| </tabs> | ||
|
|
||
| ### Maven | ||
|
|
||
| Add the serialization plugin and library to your `pom.xml` file: | ||
|
|
||
| 1. Specify the Kotlin and serialization version in the `<properties>` section: | ||
|
|
||
| ```xml | ||
| <properties> | ||
| <kotlin.version>%kotlinVersion%</kotlin.version> | ||
sandwwraith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <serialization.version>%serializationVersion%</serialization.version> | ||
| </properties> | ||
| ``` | ||
|
|
||
| 2. Add the Kotlin serialization Maven plugin to the `<build>` section: | ||
|
|
||
| ```xml | ||
| <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> | ||
| ``` | ||
|
|
||
| 3. Add the JSON serialization library dependency to the `<dependencies>` section: | ||
|
|
||
| ```xml | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.jetbrains.kotlinx</groupId> | ||
| <artifactId>kotlinx-serialization-json</artifactId> | ||
| <version>${serialization.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| ``` | ||
|
|
||
| ### Add the Kotlin serialization library to a multiplatform project | ||
daniCsorbaJB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| To use Kotlin serialization in multiplatform projects, add the JSON serialization library dependency to your common source set: | ||
daniCsorbaJB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```kotlin | ||
| commonMain { | ||
| dependencies { | ||
| implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%") | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Serialize objects to JSON | ||
|
|
||
| Serialization is the process of converting an object into a format that can be easily stored or transmitted, such as JSON. | ||
sandwwraith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| In Kotlin, you can serialize objects to JSON using the `kotlinx.serialization` library. | ||
|
|
||
| To make a class serializable, you need to mark it with the [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation. | ||
daniCsorbaJB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| This annotation instructs the compiler to generate the code required for serializing and deserializing instances of the class. | ||
| For more information, see [The @Serialization annotation](serialization-customization-options.md#the-serializable-annotation). | ||
|
|
||
| Let's look at an example: | ||
|
|
||
| 1. Import the necessary serialization libraries: | ||
sandwwraith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```kotlin | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.json.* | ||
| ``` | ||
|
|
||
| 2. Make a class serializable by annotating it with `@Serializable`: | ||
|
|
||
| ```kotlin | ||
| @Serializable | ||
| data class Data(val a: Int, val b: String) | ||
| ``` | ||
|
|
||
| > The `@Serializable` annotation enables default serialization of all properties in the primary constructor. | ||
sandwwraith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| > You can customize serialization behavior with custom constructors, optional properties, and more. | ||
sandwwraith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| > | ||
| > For more information, see [Serialize classes](serialization-customization-options.md). | ||
| > | ||
| {style="note"} | ||
|
|
||
| 3. Use the [`Json.encodeToString()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/encode-to-string.html) function to serialize an instance of this class: | ||
|
|
||
| ```kotlin | ||
| // Imports the necessary libraries for serialization and JSON handling | ||
sandwwraith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.json.* | ||
|
|
||
| // Marks the Data class as serializable | ||
| @Serializable | ||
| data class Data(val a: Int, val b: String) | ||
|
|
||
| fun main() { | ||
| // Serializes an instance of the Data class into a JSON string | ||
| val json = Json.encodeToString(Data(42, "str")) | ||
| println(json) | ||
| // {"a":42,"b":"str"} | ||
| } | ||
| ``` | ||
| {kotlin-runnable="true"} | ||
|
|
||
| As a result, you get a string containing the state of this object in JSON format: `{"a":42,"b":"str"}` | ||
|
|
||
| > You can also serialize a collection of objects in a single call: | ||
| > | ||
| > ```kotlin | ||
| > val dataList = listOf(Data(42, "str"), Data(12, "test")) | ||
| > val jsonList = Json.encodeToString(dataList) | ||
| > ``` | ||
| > | ||
| {style="tip"} | ||
|
|
||
| ## Deserialize objects from JSON | ||
|
|
||
| Deserialization converts a JSON string back into an object. | ||
|
|
||
| To deserialize an object from JSON in Kotlin: | ||
|
|
||
| 1. Import the necessary serialization libraries: | ||
|
|
||
| ```kotlin | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.json.* | ||
| ``` | ||
|
|
||
| 2. Make a class serializable by annotating it with `@Serializable`: | ||
|
|
||
| ```kotlin | ||
| @Serializable | ||
| data class Data(val a: Int, val b: String) | ||
| ``` | ||
|
|
||
| 3. Use the [`Json.decodeFromString()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/decode-from-string.html) function to deserialize an object from JSON: | ||
|
|
||
| ```kotlin | ||
| // Imports the necessary libraries for serialization and JSON handling | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.json.* | ||
|
|
||
| // Marks the Data class as serializable | ||
| @Serializable | ||
| data class Data(val a: Int, val b: String) | ||
|
|
||
| fun main() { | ||
| // Deserializes a JSON string into an instance of the Data class | ||
| val obj = Json.decodeFromString<Data>("""{"a":42, "b": "str"}""") | ||
| println(obj) | ||
| // Data(a=42, b=str) | ||
| } | ||
| ``` | ||
| {kotlin-runnable="true"} | ||
|
|
||
| Congratulations! You have successfully serialized an object to JSON and deserialized it back into an object in Kotlin! | ||
|
|
||
| ## What's next | ||
sandwwraith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Learn how to serialize standard types, including built-in types like numbers and strings, in [Serialize built-in types](serialization-serialize-builtin-types.md). | ||
| * Discover how to customize class serialization and adjust the default behavior of the `@Serializable` annotation in the [Serialize classes](serialization-customization-options.md) section. | ||
| * Dive deeper into handling JSON data and configuring JSON serialization in the [JSON serialization overview](configure-json-serialization.md). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.