summaryrefslogtreecommitdiff
path: root/guide/example/example-json-17.kt
diff options
context:
space:
mode:
Diffstat (limited to 'guide/example/example-json-17.kt')
-rw-r--r--guide/example/example-json-17.kt37
1 files changed, 15 insertions, 22 deletions
diff --git a/guide/example/example-json-17.kt b/guide/example/example-json-17.kt
index 7b1b88f3..72a696a2 100644
--- a/guide/example/example-json-17.kt
+++ b/guide/example/example-json-17.kt
@@ -4,27 +4,20 @@ package example.exampleJson17
import kotlinx.serialization.*
import kotlinx.serialization.json.*
-import kotlinx.serialization.builtins.*
-
-@Serializable
-data class Project(
- val name: String,
- @Serializable(with = UserListSerializer::class)
- val users: List<User>
-)
-
-@Serializable
-data class User(val name: String)
-
-object UserListSerializer : JsonTransformingSerializer<List<User>>(ListSerializer(User.serializer())) {
-
- override fun transformSerialize(element: JsonElement): JsonElement {
- require(element is JsonArray) // this serializer is used only with lists
- return element.singleOrNull() ?: element
- }
-}
-
fun main() {
- val data = Project("kotlinx.serialization", listOf(User("kotlin")))
- println(Json.encodeToString(data))
+ val element = buildJsonObject {
+ put("name", "kotlinx.serialization")
+ putJsonObject("owner") {
+ put("name", "kotlin")
+ }
+ putJsonArray("forks") {
+ addJsonObject {
+ put("votes", 42)
+ }
+ addJsonObject {
+ put("votes", 9000)
+ }
+ }
+ }
+ println(element)
}