Skip to content

Commit fa940d5

Browse files
committed
doc(docs.topics.serialization): clear notes and add examples
1 parent e834ad1 commit fa940d5

File tree

3 files changed

+143
-121
lines changed

3 files changed

+143
-121
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## How has it been created?
2+
### Manually
3+
* download & paste | "../lib/"
4+
* "kotlinx-serialization-core-jvm-1.6.0.jar"
5+
* "kotlinx-serialization-json-jvm-1.6.0.jar"
6+
### Dependency Manager
7+
* TODO:
8+
9+
## How to run it locally?
10+
### Manually
11+
* via IDE
12+
* TODO: NOT display green click button
13+
* via CL
14+
* | this path
15+
* `kotlinc -cp "../lib/*" serialization.kt -include-runtime -d serialization.jar`
16+
* Problems:
17+
* Problem1: "warning: classpath entry points to a non-existent location: ../lib/*"
18+
* Attempt1: `kotlinc -cp "../lib/kotlinx-serialization-json-jvm-1.6.0.jar:../lib/kotlinx-serialization-core-jvm-1.6.0.jar" serialization.kt -include-runtime -d serialization.jar`
19+
* Solution: TODO:
20+
* `java -jar serialization.jar`
21+
* Problems:
22+
* Problem1: "Caused by: java.lang.ClassNotFoundException: kotlinx.serialization.json.Json"
23+
* Attempt1: `java -cp "../lib/kotlinx-serialization-json-jvm-1.6.0.jar:../lib/kotlinx-serialization-core-jvm-1.6.0.jar" -jar serialization.jar`
24+
* Attempt2: `java -cp "../lib/kotlinx-serialization-json-jvm-1.6.0.jar:../lib/kotlinx-serialization-core-jvm-1.6.0.jar:serialization.jar" SerializationKt`
25+
* Solution: TODO:
26+
### Dependency Manager
27+
* TODO:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import kotlinx.serialization.Serializable
2+
import kotlinx.serialization.json.Json
3+
import kotlinx.serialization.encodeToString
4+
import kotlinx.serialization.decodeFromString
5+
6+
@Serializable
7+
data class Data(val a: Int, val b: String)
8+
9+
fun main() {
10+
// 1. serialize
11+
val classInstance = Json.encodeToString(Data(42, "str"))
12+
println("1. serialize - classInstance " + classInstance)
13+
14+
val dataList = listOf(Data(42, "str"), Data(12, "test"))
15+
val jsonList = Json.encodeToString(dataList)
16+
println("1. serialize - jsonList " + jsonList)
17+
18+
// 2. deserialize
19+
val deserialize = Json.decodeFromString<Data>("""{"a":42, "b": "str"}""")
20+
println("2. deserialize - deserialize " + deserialize)
21+
}

docs/topics/serialization.md

Lines changed: 95 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,120 @@
11
[//]: # (title: Serialization)
22

3-
_Serialization_ is the process of converting data used by an application to a format that can be transferred over a network
4-
or stored in a database or a file. In turn, _deserialization_ is the opposite process of reading data from an external source
5-
and converting it into a runtime object. Together, they are essential to most applications that exchange
6-
data with third parties.
7-
8-
Some data serialization formats, such as [JSON](https://www.json.org/json-en.html) and
9-
[protocol buffers](https://developers.google.com/protocol-buffers) are particularly common. Being language-neutral and
10-
platform-neutral, they enable data exchange between systems written in any modern language.
11-
12-
In Kotlin, data serialization tools are available in a separate component, [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization).
13-
It consists of several parts: the `org.jetbrains.kotlin.plugin.serialization` Gradle plugin, [runtime libraries](#libraries),
14-
and compiler plugins.
15-
16-
Compiler plugins, `kotlinx-serialization-compiler-plugin` and `kotlinx-serialization-compiler-plugin-embeddable`,
17-
are published directly to Maven Central. The second plugin is designed for working with the `kotlin-compiler-embeddable`
18-
artifact, which is the default option for scripting artifacts. Gradle adds compiler plugins to your projects as compiler arguments.
3+
* _Serialization_
4+
* := process of data used by an application -- is converted to a -- format / can be
5+
* transferred -- over a -- network
6+
* stored | database OR file
7+
* formats
8+
* [JSON](https://www.json.org/json-en.html)
9+
* [protocol buffers](https://developers.google.com/protocol-buffers)
10+
11+
* _deserialization_
12+
* := process of data | external source -- is
13+
* read
14+
* converted it into a -- runtime object
15+
16+
* allows
17+
* being language-neutral & platform-neutral
18+
19+
* uses
20+
* MOST applications / exchange data -- with -- third parties
1921

2022
## Libraries
2123

22-
`kotlinx.serialization` provides sets of libraries for all supported platforms – JVM, JavaScript, Native – and for various
23-
serialization formats – JSON, CBOR, protocol buffers, and others. You can find the complete list of supported serialization
24-
formats [below](#formats).
25-
26-
All Kotlin serialization libraries belong to the `org.jetbrains.kotlinx:` group. Their names start with `kotlinx-serialization-`
27-
and have suffixes that reflect the serialization format. Examples:
28-
* `org.jetbrains.kotlinx:kotlinx-serialization-json` provides JSON serialization for Kotlin projects.
29-
* `org.jetbrains.kotlinx:kotlinx-serialization-cbor` provides CBOR serialization.
30-
31-
Platform-specific artifacts are handled automatically; you don't need to add them manually. Use the same dependencies in
32-
JVM, JS, Native, and multiplatform projects.
33-
34-
Note that the `kotlinx.serialization` libraries use their own versioning structure, which doesn't match Kotlin's versioning.
35-
Check out the releases on [GitHub](https://github.com/Kotlin/kotlinx.serialization/releases) to find the latest versions.
36-
37-
## Formats
38-
39-
`kotlinx.serialization` includes libraries for various serialization formats:
40-
41-
* [JSON](https://www.json.org/): [`kotlinx-serialization-json`](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md#json)
42-
* [Protocol buffers](https://developers.google.com/protocol-buffers): [`kotlinx-serialization-protobuf`](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md#protobuf)
43-
* [CBOR](https://cbor.io/): [`kotlinx-serialization-cbor`](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md#cbor)
44-
* [Properties](https://en.wikipedia.org/wiki/.properties): [`kotlinx-serialization-properties`](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md#properties)
45-
* [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md): [`kotlinx-serialization-hocon`](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md#hocon) (only on JVM)
46-
47-
Note that all libraries except JSON serialization (`kotlinx-serialization-json`) are [Experimental](components-stability.md),
48-
which means their API can be changed without notice.
49-
50-
There are also community-maintained libraries that support more serialization formats, such as [YAML](https://yaml.org/)
51-
or [Apache Avro](https://avro.apache.org/). For detailed information about available serialization formats, see the
52-
[`kotlinx.serialization` documentation](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md).
53-
54-
## Example: JSON serialization
55-
56-
Let's take a look at how to serialize Kotlin objects into JSON.
57-
58-
### Add plugins and dependencies
59-
60-
Before starting, you must configure your build script so that you can use Kotlin serialization tools in your project:
61-
62-
1. Apply the Kotlin serialization Gradle plugin `org.jetbrains.kotlin.plugin.serialization` (or `kotlin("plugin.serialization")`
63-
in the Kotlin Gradle DSL).
64-
65-
<tabs group="build-script">
66-
<tab title="Kotlin" group-key="kotlin">
67-
68-
```kotlin
24+
* `org.jetbrains.kotlinx:` group,
25+
* ALL Kotlin serialization libraries
26+
* `kotlinx-serialization-ConcreteSerializationFormat`
27+
* artifact names
28+
* _Examples:_
29+
* `org.jetbrains.kotlinx:kotlinx-serialization-json`
30+
* JSON serialization
31+
* `org.jetbrains.kotlinx:kotlinx-serialization-cbor`
32+
* CBOR serialization
33+
* support ALL platforms
34+
* == 1 artifact / ALL platforms
35+
36+
* [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization)
37+
* == 👀SET of data serialization libraries 👀 /
38+
* SEPARATE component
39+
* == ❌NOT | kotlin ❌
40+
* supports
41+
* platforms
42+
* JVM
43+
* JavaScript
44+
* Native
45+
* serialization formats
46+
* official
47+
* JSON,
48+
* CBOR,
49+
* protocol buffers,
50+
* ...
51+
* [community](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md)
52+
* versioning != Kotlin's versioning
53+
* == `org.jetbrains.kotlin.plugin.serialization` Gradle plugin + [runtime libraries](#libraries) + compiler plugins
54+
* compiler plugins
55+
* are
56+
* `kotlinx-serialization-compiler-plugin`
57+
* `kotlinx-serialization-compiler-plugin-embeddable`
58+
* 's goal
59+
* use + `kotlin-compiler-embeddable` artifact
60+
* published DIRECTLY | Maven Central
61+
* | Gradle,
62+
* if you want to add | your projects -> add -- as -- compiler arguments
63+
* `kotlin-compiler-embeddable` artifact
64+
* allows
65+
* scripting artifacts
66+
* indeed, default option
67+
68+
## _Example:_ JSON serialization
69+
70+
* goal
71+
* serialize Kotlin objects (class instances, collections, ...) -- into -> JSON
72+
73+
### steps
74+
#### 1. Add plugins & dependencies
75+
76+
1. add Kotlin serialization Gradle plugin
77+
```kotlin,tittle=build.gradle.kts
6978
plugins {
70-
kotlin("jvm") version "%kotlinVersion%"
71-
kotlin("plugin.serialization") version "%kotlinVersion%"
72-
}
79+
kotlin("jvm") version "%kotlinVersion%"
80+
kotlin("plugin.serialization") version "%kotlinVersion%"
81+
}
7382
```
7483

75-
</tab>
76-
<tab title="Groovy" group-key="groovy">
77-
78-
```groovy
84+
```groovy, title=build.gradle
7985
plugins {
8086
id 'org.jetbrains.kotlin.jvm' version '%kotlinVersion%'
8187
id 'org.jetbrains.kotlin.plugin.serialization' version '%kotlinVersion%'
8288
}
8389
```
8490

85-
</tab>
86-
</tabs>
87-
88-
2. Add the JSON serialization library dependency:`org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%`
89-
90-
<tabs group="build-script">
91-
<tab title="Kotlin" group-key="kotlin">
91+
2. add JSON serialization library dependency
9292

93-
```kotlin
93+
```kotlin,tittle=build.gradle.kts
9494
dependencies {
9595
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%")
9696
}
9797
```
9898

99-
</tab>
100-
<tab title="Groovy" group-key="groovy">
101-
102-
```groovy
99+
```groovy, title=build.gradle
103100
dependencies {
104101
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:%serializationVersion%'
105102
}
106103
```
107104

108-
</tab>
109-
</tabs>
105+
#### 2. Serialize & deserialize JSON
110106

111-
Now you're ready to use the serialization API in your code. The API is located in the `kotlinx.serialization` package
112-
and its format-specific subpackages, such as `kotlinx.serialization.json`.
113-
114-
### Serialize and deserialize JSON
115-
116-
1. Make a class serializable by annotating it with `@Serializable`.
117-
118-
```kotlin
119-
import kotlinx.serialization.Serializable
107+
1. | class,
108+
1. annotate with `@Serializable`
120109

121-
@Serializable
122-
data class Data(val a: Int, val b: String)
123-
```
110+
```kotlin
111+
import kotlinx.serialization.Serializable
112+
113+
@Serializable
114+
data class Data(val a: Int, val b: String)
115+
```
124116

125-
2. Serialize an instance of this class by calling `Json.encodeToString()`.
117+
2. if you want to serialize a class' instance -> call `Json.encodeToString()`
126118
127119
```kotlin
128120
import kotlinx.serialization.Serializable
@@ -133,22 +125,14 @@ import kotlinx.serialization.encodeToString
133125
data class Data(val a: Int, val b: String)
134126
135127
fun main() {
128+
// serialize a class instance
136129
val json = Json.encodeToString(Data(42, "str"))
130+
val dataList = listOf(Data(42, "str"), Data(12, "test"))
131+
val jsonList = Json.encodeToString(dataList)
137132
}
138133
```
139134
140-
As a result, you get a string containing the state of this object in the JSON format: `{"a": 42, "b": "str"}`
141-
142-
> You can also serialize object collections, such as lists, in a single call:
143-
>
144-
> ```kotlin
145-
> val dataList = listOf(Data(42, "str"), Data(12, "test"))
146-
> val jsonList = Json.encodeToString(dataList)
147-
> ```
148-
>
149-
{type="note"}
150-
151-
3. Use the `decodeFromString()` function to deserialize an object from JSON:
135+
3. if you want from JSON -- deserialize into -- an object -> use `decodeFromString()`
152136
153137
```kotlin
154138
import kotlinx.serialization.Serializable
@@ -159,21 +143,11 @@ import kotlinx.serialization.decodeFromString
159143
data class Data(val a: Int, val b: String)
160144
161145
fun main() {
146+
// deserialize
162147
val obj = Json.decodeFromString<Data>("""{"a":42, "b": "str"}""")
163148
}
164149
```
165150
166-
That's it! You have successfully serialized objects into JSON strings and deserialized them back into objects.
167-
168151
## What's next
169152

170-
For more information about serialization in Kotlin, see the [Kotlin Serialization Guide](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serialization-guide.md).
171-
172-
You can explore different aspects of Kotlin serialization in the following resources:
173-
174-
* [Learn more about Kotlin serialization and its core concepts](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md)
175-
* [Explore the built-in serializable classes of Kotlin](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/builtin-classes.md)
176-
* [Look at serializers in more detail and learn how to create custom serializers](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md)
177-
* [Discover how polymorphic serialization is handled in Kotlin](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism)
178-
* [Look into the various JSON features handling Kotlin serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#json-elements)
179-
* [Learn more about the experimental serialization formats supported by Kotlin](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/formats.md)
153+
* see [Kotlin Serialization Guide](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serialization-guide.md).

0 commit comments

Comments
 (0)