• Home
  • Raw
  • Download

Lines Matching +full:project +full:- +full:name

1 <!--- TEST_NAME BasicSerializationTest -->
5 This is the first chapter of the [Kotlin Serialization Guide](serialization-guide.md).
10 <!--- TOC -->
13 * [JSON encoding](#json-encoding)
14 * [JSON decoding](#json-decoding)
15 * [Serializable classes](#serializable-classes)
16 * [Backing fields are serialized](#backing-fields-are-serialized)
17 * [Constructor properties requirement](#constructor-properties-requirement)
18 * [Data validation](#data-validation)
19 * [Optional properties](#optional-properties)
20 * [Optional property initializer call](#optional-property-initializer-call)
21 * [Required properties](#required-properties)
22 * [Transient properties](#transient-properties)
23 * [Defaults are not encoded by default](#defaults-are-not-encoded-by-default)
24 * [Nullable properties](#nullable-properties)
25 * [Type safety is enforced](#type-safety-is-enforced)
26 * [Referenced objects](#referenced-objects)
27 * [No compression of repeated references](#no-compression-of-repeated-references)
28 * [Generic classes](#generic-classes)
29 * [Serial field names](#serial-field-names)
31 <!--- END -->
44 +---------+ Serialization +------------+ Encoding +---------------+
45 | Objects | --------------> | Primitives | ---------> | Output format |
46 +---------+ +------------+ +---------------+
54 <!--- INCLUDE .*-basic-.*
57 -->
65 Let's start with a class describing a project and try to get its JSON representation.
68 class Project(val name: String, val language: String)
71 val data = Project("kotlinx.serialization", "Kotlin")
76 > You can get the full code [here](../guide/example/example-basic-01.kt).
81 …d "main" kotlinx.serialization.SerializationException: Serializer for class 'Project' is not found.
85 <!--- TEST LINES_START -->
93 class Project(val name: String, val language: String)
96 val data = Project("kotlinx.serialization", "Kotlin")
101 > You can get the full code [here](../guide/example/example-basic-02.kt).
107 {"name":"kotlinx.serialization","language":"Kotlin"}
110 <!--- TEST -->
122 Here we are marking our `Project` class as a `data class`, not because it is required, but because
127 data class Project(val name: String, val language: String)
130 val data = Json.decodeFromString<Project>("""
131 {"name":"kotlinx.serialization","language":"Kotlin"}
137 > You can get the full code [here](../guide/example/example-basic-03.kt).
142 Project(name=kotlinx.serialization, language=Kotlin)
145 <!--- TEST -->
151 <!--- INCLUDE .*-classes-.*
154 -->
163 class Project(
164 // name is a property with backing field -- serialized
165 var name: String
167 var stars: Int = 0 // property with a backing field -- serialized
169 val path: String // getter only, no backing field -- not serialized
170 get() = "kotlin/$name"
172 var id by ::name // delegated property -- not serialized
176 val data = Project("kotlinx.serialization").apply { stars = 9000 }
181 > You can get the full code [here](../guide/example/example-classes-01.kt).
183 We can clearly see that only the `name` and `stars` properties are present in the JSON output.
186 {"name":"kotlinx.serialization","stars":9000}
189 <!--- TEST -->
193 If we want to define the `Project` class so that it takes a path string, and then
198 class Project(path: String) {
200 val name: String = path.substringAfter('/')
204 <!--- CLEAR -->
212 class Project private constructor(val owner: String, val name: String) {
215 name = path.substringAfter('/')
219 get() = "$owner/$name"
227 println(Json.encodeToString(Project("kotlin/kotlinx.serialization")))
231 > You can get the full code [here](../guide/example/example-classes-02.kt).
236 {"owner":"kotlin","name":"kotlinx.serialization"}
239 <!--- TEST -->
249 class Project(val name: String) {
251 require(name.isNotEmpty()) { "name cannot be empty" }
261 val data = Json.decodeFromString<Project>("""
262 {"name":""}
268 > You can get the full code [here](../guide/example/example-classes-03.kt).
273 Exception in thread "main" java.lang.IllegalArgumentException: name cannot be empty
276 <!--- TEST LINES_START -->
285 data class Project(val name: String, val language: String)
288 val data = Json.decodeFromString<Project>("""
289 {"name":"kotlinx.serialization"}
295 > You can get the full code [here](../guide/example/example-classes-04.kt).
300 …ption: Field 'language' is required for type with serial name 'example.exampleClasses04.Project', …
303 <!--- TEST LINES_START -->
310 data class Project(val name: String, val language: String = "Kotlin")
313 val data = Json.decodeFromString<Project>("""
314 {"name":"kotlinx.serialization"}
320 > You can get the full code [here](../guide/example/example-classes-05.kt).
325 Project(name=kotlinx.serialization, language=Kotlin)
328 <!--- TEST -->
343 data class Project(val name: String, val language: String = computeLanguage())
346 val data = Json.decodeFromString<Project>("""
347 {"name":"kotlinx.serialization","language":"Kotlin"}
353 > You can get the full code [here](../guide/example/example-classes-06.kt).
359 Project(name=kotlinx.serialization, language=Kotlin)
362 <!--- TEST -->
371 data class Project(val name: String, @Required val language: String = "Kotlin")
374 val data = Json.decodeFromString<Project>("""
375 {"name":"kotlinx.serialization"}
381 > You can get the full code [here](../guide/example/example-classes-07.kt).
386 …ption: Field 'language' is required for type with serial name 'example.exampleClasses07.Project', …
389 <!--- TEST LINES_START -->
398 data class Project(val name: String, @Transient val language: String = "Kotlin")
401 val data = Json.decodeFromString<Project>("""
402 {"name":"kotlinx.serialization","language":"Kotlin"}
408 > You can get the full code [here](../guide/example/example-classes-08.kt).
414 …xception: Unexpected JSON token at offset 42: Encountered an unknown key 'language' at path: $.name
418 <!--- TEST LINES_START -->
420 …feature is explained in the [Ignoring Unknown Keys section](json.md#ignoring-unknown-keys) section.
424 …encoded by default in JSON. This behavior is motivated by the fact that in most real-life scenarios
429 data class Project(val name: String, val language: String = "Kotlin")
432 val data = Project("kotlinx.serialization")
437 > You can get the full code [here](../guide/example/example-classes-09.kt).
442 {"name":"kotlinx.serialization"}
445 <!--- TEST -->
447 See JSON's [Encoding defaults](json.md#encoding-defaults) section on how this behavior can be confi…
453 data class Project(
454 val name: String,
466 val name: String,
467 @EncodeDefault(EncodeDefault.Mode.NEVER) val projects: List<Project> = emptyList()
471 val userA = User("Alice", listOf(Project("kotlinx.serialization")))
478 > You can get the full code [here](../guide/example/example-classes-10.kt).
483 {"name":"Alice","projects":[{"name":"kotlinx.serialization","language":"Kotlin"}]}
484 {"name":"Bob"}
487 <!--- TEST -->
495 class Project(val name: String, val renamedTo: String? = null)
498 val data = Project("kotlinx.serialization")
503 > You can get the full code [here](../guide/example/example-classes-11.kt).
505 This example does not encode `null` in JSON because [Defaults are not encoded](#defaults-are-not-en…
508 {"name":"kotlinx.serialization"}
511 <!--- TEST -->
516 In particular, let us try to decode a `null` value from a JSON object into a non-nullable Kotlin pr…
520 data class Project(val name: String, val language: String = "Kotlin")
523 val data = Json.decodeFromString<Project>("""
524 {"name":"kotlinx.serialization","language":null}
530 > You can get the full code [here](../guide/example/example-classes-12.kt).
540 <!--- TEST LINES_START -->
542 > It might be desired, when decoding 3rd-party JSONs, to coerce `null` to a default value.
543 > The corresponding feature is explained in the [Coercing input values](json.md#coercing-input-valu…
552 class Project(val name: String, val owner: User)
555 class User(val name: String)
559 val data = Project("kotlinx.serialization", owner)
564 > You can get the full code [here](../guide/example/example-classes-13.kt).
569 {"name":"kotlinx.serialization","owner":{"name":"kotlin"}}
572 > References to non-serializable classes can be marked as [Transient properties](#transient-propert…
575 <!--- TEST -->
585 class Project(val name: String, val owner: User, val maintainer: User)
588 class User(val name: String)
592 val data = Project("kotlinx.serialization", owner, owner)
597 > You can get the full code [here](../guide/example/example-classes-14.kt).
602 {"name":"kotlinx.serialization","owner":{"name":"kotlin"},"maintainer":{"name":"kotlin"}}
606 > You can use the [Transient properties](#transient-properties) to exclude some references from ser…
608 <!--- TEST -->
612 Generic classes in Kotlin provide type-polymorphic behavior, which is enforced by Kotlin Serializat…
613 compile-time. For example, consider a generic serializable class `Box<T>`.
620 …class can be used with builtin types like `Int`, as well as with user-defined types like `Project`.
622 <!--- INCLUDE
625 data class Project(val name: String, val language: String)
626 -->
632 val b: Box<Project>
636 val data = Data(Box(42), Box(Project("kotlinx.serialization", "Kotlin")))
641 > You can get the full code [here](../guide/example/example-classes-15.kt).
643 The actual type that we get in JSON depends on the actual compile-time type parameter that was spec…
646 {"a":{"contents":42},"b":{"contents":{"name":"kotlinx.serialization","language":"Kotlin"}}}
649 <!--- TEST -->
651 If the actual generic type is not serializable a compile-time error will be produced.
656 their names in the source code by default. The name that is used for serialization is called a _ser…
658 the source with an abbreviated serial name.
662 class Project(val name: String, @SerialName("lang") val language: String)
665 val data = Project("kotlinx.serialization", "Kotlin")
670 > You can get the full code [here](../guide/example/example-classes-16.kt).
672 Now we see that an abbreviated name `lang` is used in the JSON output.
675 {"name":"kotlinx.serialization","lang":"Kotlin"}
678 <!--- TEST -->
680 ---
682 The next chapter covers [Builtin classes](builtin-classes.md).
684 <!-- stdlib references -->
685 [kotlin.jvm.Transient]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-transient/
687 <!--- MODULE /kotlinx-serialization-core -->
688 <!--- INDEX kotlinx-serialization-core/kotlinx.serialization -->
690 …//kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/encode
691 …ttps://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-
692 …/kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/decode-
693 …ttps://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-
694 …ttps://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-
695 …://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-enco…
696 …otlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-encode-d…
697 …/kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serial
699 <!--- MODULE /kotlinx-serialization-json -->
700 <!--- INDEX kotlinx-serialization-json/kotlinx.serialization.json -->
701 <!--- END -->