|
| 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