Skip to content

Commit 6641175

Browse files
authored
feat: adding JSON overview and Json configuration docs (#3102)
* feat: adding JSON overview and Json configuration docs * update: implementing first round of review comments * feat: adding page for io serialization and implementing review comments * update: created new page for I/O serialization * update: implementing additional review comments * update: small update to section ordering * update: implementing comments related to the IO sources page * update: small additional updates for streams and minor update to json config * update: implementing TWr review comments from Danil and adding example ids * fix: last minor TWr review changes
1 parent eb5f27d commit 6641175

File tree

4 files changed

+1168
-0
lines changed

4 files changed

+1168
-0
lines changed

docs-website/serialization.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<toc-element topic="serialization-json-configuration.md"/>
1414
<toc-element topic="serialization-json-elements.md"/>
1515
<toc-element topic="serialization-transform-json.md" toc-title="JSON transformation"/>
16+
<toc-element topic="serialization-json-io-sources.md"/>
1617
</toc-element>
1718
<toc-element topic="serialization-polymorphism.md"/>
1819
<toc-element toc-title="Custom serializers">
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[//]: # (title: JSON serialization overview)
2+
3+
The Kotlin serialization library allows you to easily convert Kotlin objects to JSON and back.
4+
The [`Json`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/) class is the primary tool for this, offering flexibility in how JSON is generated and parsed.
5+
You can configure `Json` instances to handle specific JSON behaviors or use its default instance for basic tasks.
6+
7+
With the `Json` class, you can:
8+
9+
* Serialize Kotlin objects to JSON strings using the [`encodeToString()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/encode-to-string.html) function.
10+
* Deserialize JSON strings back to Kotlin objects with the [`decodeFromString()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/decode-from-string.html) function.
11+
* [Work directly with the `JsonElement`](serialization-json-elements.md) when handling complex JSON structures using the [`encodeToJsonElement()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/encode-to-json-element.html) and the [`decodeFromJsonElement()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/decode-from-json-element.html) functions.
12+
* Use [Experimental](components-stability.md#stability-levels-explained) extension functions to [serialize and deserialize I/O sources](serialization-json-io-sources.md) without creating intermediate strings, including JVM streams as well as [`kotlinx-io`](https://github.com/Kotlin/kotlinx-io) and [Okio](https://square.github.io/okio/) types.
13+
14+
Before you start, import the following declarations from the serialization library:
15+
16+
```kotlin
17+
import kotlinx.serialization.*
18+
import kotlinx.serialization.json.*
19+
```
20+
21+
Here's a simple example that uses the default `Json` instance to show how JSON serialization works in Kotlin:
22+
23+
```kotlin
24+
// Imports declarations from the serialization library
25+
import kotlinx.serialization.*
26+
import kotlinx.serialization.json.*
27+
28+
//sampleStart
29+
@Serializable
30+
data class User(val name: String, val age: Int)
31+
32+
fun main() {
33+
// Uses the default Json instance
34+
val json = Json
35+
36+
// Creates a User object
37+
val user = User("Alice", 30)
38+
39+
// Converts the User object to a JSON string
40+
val jsonString = json.encodeToString(user)
41+
println(jsonString)
42+
// {"name":"Alice","age":30}
43+
44+
// Converts the JSON string back to a User object
45+
val deserializedUser = json.decodeFromString<User>(jsonString)
46+
println(deserializedUser)
47+
// User(name=Alice, age=30)
48+
//sampleEnd
49+
}
50+
```
51+
{kotlin-runnable="true" id="first-json-example"}
52+
53+
In addition to using the default configuration, you can [customize the `Json` instance](serialization-json-configuration.md) for specific use cases,
54+
such as ignoring unknown keys:
55+
56+
```kotlin
57+
// Imports declarations from the serialization library
58+
import kotlinx.serialization.*
59+
import kotlinx.serialization.json.*
60+
61+
//sampleStart
62+
@Serializable
63+
data class Project(val name: String)
64+
65+
// Configures a Json instance to ignore unknown keys
66+
val customJson = Json {
67+
ignoreUnknownKeys = true
68+
}
69+
70+
fun main() {
71+
val data = customJson.decodeFromString<Project>("""
72+
{"name":"kotlinx.serialization","language":"Kotlin"}
73+
""")
74+
println(data)
75+
// Project(name=kotlinx.serialization)
76+
}
77+
//sampleEnd
78+
```
79+
{kotlin-runnable="true" id="first-json-instance-example"}
80+
81+
## What's next
82+
83+
* Learn how to [customize `Json` instances](serialization-json-configuration.md) to address different use cases for serialization and deserialization.
84+
* Explore [advanced JSON element handling](serialization-json-elements.md) to manipulate and work with JSON data before it is parsed or serialized.
85+
* Discover how to [transform JSON during serialization and deserialization](serialization-transform-json.md) for more control over your data.

0 commit comments

Comments
 (0)