• Home
  • Raw
  • Download

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

1 <!--- TEST_NAME PolymorphismTest -->
5 This is the fourth chapter of the [Kotlin Serialization Guide](serialization-guide.md).
10 <!--- TOC -->
12 * [Closed polymorphism](#closed-polymorphism)
13 * [Static types](#static-types)
14 * [Designing serializable hierarchy](#designing-serializable-hierarchy)
15 * [Sealed classes](#sealed-classes)
16 * [Custom subclass serial name](#custom-subclass-serial-name)
17 * [Concrete properties in a base class](#concrete-properties-in-a-base-class)
19 * [Open polymorphism](#open-polymorphism)
20 * [Registered subclasses](#registered-subclasses)
21 * [Serializing interfaces](#serializing-interfaces)
22 * [Property of an interface type](#property-of-an-interface-type)
23 * [Static parent type lookup for polymorphism](#static-parent-type-lookup-for-polymorphism)
24 …* [Explicitly marking polymorphic class properties](#explicitly-marking-polymorphic-class-properti…
25 * [Registering multiple superclasses](#registering-multiple-superclasses)
26 * [Polymorphism and generic classes](#polymorphism-and-generic-classes)
27 * [Merging library serializers modules](#merging-library-serializers-modules)
28 …* [Default polymorphic type handler for deserialization](#default-polymorphic-type-handler-for-des…
29 …* [Default polymorphic type handler for serialization](#default-polymorphic-type-handler-for-seria…
31 <!--- END -->
33 <!--- INCLUDE .*-poly-.*
36 -->
45 by *compile-time* types of objects. Let's examine this aspect in more detail and learn how
48 … the static nature of Kotlin Serialization let us make the following setup. An `open class Project`
49 has just the `name` property, while its derived `class OwnedProject` adds an `owner` property.
51 `Project` that is initialized with an instance of `OwnedProject` at runtime.
55 open class Project(val name: String)
57 class OwnedProject(name: String, val owner: String) : Project(name)
60 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
65 > You can get the full code [here](../guide/example/example-poly-01.kt).
67 Despite the runtime type of `OwnedProject`, only the `Project` class properties are getting seriali…
70 {"name":"kotlinx.coroutines"}
73 <!--- TEST -->
75 Let's change the compile-time type of `data` to `OwnedProject`.
79 open class Project(val name: String)
81 class OwnedProject(name: String, val owner: String) : Project(name)
89 > You can get the full code [here](../guide/example/example-poly-02.kt).
98 <!--- TEST LINES_START -->
103 …ning into the [constructor properties requirement](basic-serialization.md#constructor-properties-r…
105 making the `Project` class `abstract`, too.
109 abstract class Project {
110 abstract val name: String
113 class OwnedProject(override val name: String, val owner: String) : Project()
116 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
121 > You can get the full code [here](../guide/example/example-poly-03.kt).
126 …ception: Serializer for subclass 'OwnedProject' is not found in the polymorphic scope of 'Project'.
127 Check if class with serial name 'OwnedProject' exists and serializer is registered in a correspondi…
128 …lly, class 'OwnedProject' has to be '@Serializable', and the base class 'Project' has to be sealed…
131 <!--- TEST LINES_START -->
140 sealed class Project {
141 abstract val name: String
145 class OwnedProject(override val name: String, val owner: String) : Project()
148 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
149 println(Json.encodeToString(data)) // Serializing data of compile-time type Project
153 > You can get the full code [here](../guide/example/example-poly-04.kt).
159 {"type":"example.examplePoly04.OwnedProject","name":"kotlinx.coroutines","owner":"kotlin"}
162 <!--- TEST -->
164 …l, but very important detail in the above example that is related to [Static types](#static-types):
165 the `val data` property has a compile-time type of `Project`, even though its run-time type is `Own…
166 When serializing polymorphic class hierarchies you must ensure that the compile-time type of the se…
169 Let us see what happens if the example is slightly changed, so that the compile-time of the object …
170 serialized is `OwnedProject` (the same as its run-time type).
174 sealed class Project {
175 abstract val name: String
179 class OwnedProject(override val name: String, val owner: String) : Project()
183 println(Json.encodeToString(data)) // Serializing data of compile-time type OwnedProject
187 > You can get the full code [here](../guide/example/example-poly-05.kt).
193 {"name":"kotlinx.coroutines","owner":"kotlin"}
196 <!--- TEST -->
198 In general, Kotlin Serialization is designed to work correctly only when the compile-time type used…
199 is the same one as the compile-time type used during deserialization. You can always specify the ty…
200 when calling serialization functions. The previous example can be corrected to use `Project` type f…
201 by calling `Json.encodeToString<Project>(data)`.
203 ### Custom subclass serial name
205 A value of the `type` key is a fully qualified class name by default. We can put [SerialName] annot…
210 sealed class Project {
211 abstract val name: String
216 class OwnedProject(override val name: String, val owner: String) : Project()
219 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
224 > You can get the full code [here](../guide/example/example-poly-06.kt).
226 This way we can have a stable _serial name_ that is not affected by the class's name in the source …
229 {"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}
232 <!--- TEST -->
234 > In addition to that, JSON can be configured to use a different key name for the class discriminat…
235 …example in the [Class discriminator for polymorphism](json.md#class-discriminator-for-polymorphism…
243 sealed class Project {
244 abstract val name: String
250 class OwnedProject(override val name: String, val owner: String) : Project()
254 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
259 > You can get the full code [here](../guide/example/example-poly-07.kt).
264 {"type":"owned","status":"open","name":"kotlinx.coroutines","owner":"kotlin"}
267 <!--- TEST -->
294 > You can get the full code [here](../guide/example/example-poly-08.kt).
296 An object serializes as an empty class, also using its fully-qualified class name as type by defaul…
302 <!--- TEST -->
310 … other modules, the list of subclasses that are serialized cannot be determined at compile-time and
315 Let us start with the code from the [Designing serializable hierarchy](#designing-serializable-hier…
322 > the [JSON configuration](json.md#json-configuration) section.
324 <!--- INCLUDE
326 -->
330 polymorphic(Project::class) {
338 abstract class Project {
339 abstract val name: String
344 class OwnedProject(override val name: String, val owner: String) : Project()
347 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
352 > You can get the full code [here](../guide/example/example-poly-09.kt).
355 the [Sealed classes](#sealed-classes) section, but here subclasses can be spread arbitrarily throug…
358 {"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}
361 <!--- TEST -->
363 …cit serializer should be used: `format.encodeToString(PolymorphicSerializer(Project::class), data)`
368 We can update the previous example and turn `Project` superclass into an interface. However, we can…
374 <!--- INCLUDE
378 polymorphic(Project::class) {
384 -->
387 interface Project {
388 val name: String
393 class OwnedProject(override val name: String, val owner: String) : Project
396 Now if we declare `data` with the type of `Project` we can simply call `format.encodeToString` as b…
400 val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
405 > You can get the full code [here](../guide/example/example-poly-10.kt).
408 {"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}
411 > Note: On Kotlin/Native, you should use `format.encodeToString(PolymorphicSerializer(Project::clas…
413 <!--- TEST LINES_START -->
417 Continuing the previous example, let us see what happens if we use `Project` interface as a propert…
420 <!--- INCLUDE
424 polymorphic(Project::class) {
431 interface Project {
432 val name: String
437 class OwnedProject(override val name: String, val owner: String) : Project
438 -->
442 class Data(val project: Project) // Project is an interface
450 > You can get the full code [here](../guide/example/example-poly-11.kt).
456 {"project":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}}
459 <!--- TEST -->
463 During serialization of a polymorphic class the root type of the polymorphic hierarchy (`Project` i…
464 is determined statically. Let us take the example with the serializable `abstract class Project`,
467 <!--- INCLUDE
471 polymorphic(Project::class) {
479 abstract class Project {
480 abstract val name: String
485 class OwnedProject(override val name: String, val owner: String) : Project()
486 -->
495 > You can get the full code [here](../guide/example/example-poly-12.kt).
504 <!--- TEST LINES_START -->
509 <!--- INCLUDE
511 -->
521 <!--- INCLUDE
525 abstract class Project {
526 abstract val name: String
531 class OwnedProject(override val name: String, val owner: String) : Project()
532 -->
543 > You can get the full code [here](../guide/example/example-poly-13.kt).
552 <!--- TEST LINES_START -->
557 <!--- INCLUDE
569 abstract class Project {
570 abstract val name: String
575 class OwnedProject(override val name: String, val owner: String) : Project()
576 -->
585 > You can get the full code [here](../guide/example/example-poly-14.kt).
590 {"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}
593 <!--- TEST -->
598 However, Kotlin Serialization does not compile a serializable class with a property of a non-serial…
599 If we have a property of `Any` class or other non-serializable class, then we must explicitly provi…
601 the [Specifying serializer on a property](serializers.md#specifying-serializer-on-a-property) secti…
602 To specify a polymorphic serialization strategy of a property, the special-purpose [`@Polymorphic`]…
605 <!--- INCLUDE
616 interface Project {
617 val name: String
622 class OwnedProject(override val name: String, val owner: String) : Project
623 -->
629 val project: Any
638 > You can get the full code [here](../guide/example/example-poly-15.kt).
640 <!--- TEST
641 {"project":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}}
642 -->
646 When the same class gets serialized as a value of properties with different compile-time type from …
651 <!--- INCLUDE
654 -->
658 fun PolymorphicModuleBuilder<Project>.registerProjectSubclasses() {
662 polymorphic(Project::class) { registerProjectSubclasses() }
666 <!--- INCLUDE
670 interface Project {
671 val name: String
676 class OwnedProject(override val name: String, val owner: String) : Project
680 val project: Project,
685 val project = OwnedProject("kotlinx.coroutines", "kotlin")
686 val data = Data(project, project)
689 -->
691 > You can get the full code [here](../guide/example/example-poly-16.kt).
693 <!--- TEST
694 {"project":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"},"any":{"type":"owned","nam…
695 -->
701 <!--- INCLUDE
703 -->
718 the [Plugin-generated generic serializer](serializers.md#plugin-generated-generic-serializer) of
735 Let us add a library with the `Project` hierarchy to the code from the previous section.
739 fun PolymorphicModuleBuilder<Project>.registerProjectSubclasses() {
743 polymorphic(Project::class) { registerProjectSubclasses() }
747 <!--- INCLUDE
750 abstract class Project {
751 abstract val name: String
756 data class OwnedProject(override val name: String, val owner: String) : Project()
757 -->
773 // both Response and Project are abstract and their concrete subtypes are being serialized
774 val data: Response<Project> = OkResponse(OwnedProject("kotlinx.serialization", "kotlin"))
777 println(format.decodeFromString<Response<Project>>(string))
782 > You can get the full code [here](../guide/example/example-poly-17.kt).
787 {"type":"OkResponse","data":{"type":"OwnedProject","name":"kotlinx.serialization","owner":"kotlin"}}
788 OkResponse(data=OwnedProject(name=kotlinx.serialization, owner=kotlin))
791 <!--- TEST -->
801 <!--- INCLUDE
805 abstract class Project {
806 abstract val name: String
811 data class OwnedProject(override val name: String, val owner: String) : Project()
814 polymorphic(Project::class) {
820 -->
824 println(format.decodeFromString<Project>("""
825 {"type":"unknown","name":"example"}
830 > You can get the full code [here](../guide/example/example-poly-18.kt).
835 … 0: Serializer for subclass 'unknown' is not found in the polymorphic scope of 'Project' at path: $
836 Check if class with serial name 'unknown' exists and serializer is registered in a corresponding Se…
839 <!--- TEST LINES_START -->
842 we can have a `BasicProject` subtype to represent all kinds of unknown `Project` subtypes.
844 <!--- INCLUDE
846 -->
850 abstract class Project {
851 abstract val name: String
855 data class BasicProject(override val name: String, val type: String): Project()
859 data class OwnedProject(override val name: String, val owner: String) : Project()
865 but always return the [Plugin-generated serializer](serializers.md#plugin-generated-serializer)
870 polymorphic(Project::class) {
884 println(format.decodeFromString<List<Project>>("""
886 {"type":"unknown","name":"example"},
887 {"type":"OwnedProject","name":"kotlinx.serialization","owner":"kotlin"}
893 > You can get the full code [here](../guide/example/example-poly-19.kt).
898 [BasicProject(name=example, type=unknown), OwnedProject(name=kotlinx.serialization, owner=kotlin)]
901 <!--- TEST -->
903 We used a plugin-generated serializer as a default serializer, implying that
904 the structure of the "unknown" data is known in advance. In a real-world API it's rarely the case.
905 For that purpose a custom, less-structured serializer is needed. You will see the example of such s…
906 on [Maintaining custom JSON attributes](json.md#maintaining-custom-json-attributes).
913 <!--- INCLUDE
917 -->
952 polymorphicDefaultSerializer(Animal::class) { instance ->
955 is Cat -> CatSerializer as SerializationStrategy<Animal>
956 is Dog -> DogSerializer as SerializationStrategy<Animal>
957 else -> null
997 > You can get the full code [here](../guide/example/example-poly-20.kt)
1004 <!--- TEST -->
1006 ---
1010 <!--- MODULE /kotlinx-serialization-core -->
1011 <!--- INDEX kotlinx-serialization-core/kotlinx.serialization -->
1013 …/kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serial
1014 …/kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-polymo…
1015 …ttps://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-
1016 …ttps://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-
1017 …otlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-deserial…
1018 …kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-seriali…
1020 <!--- INDEX kotlinx-serialization-core/kotlinx.serialization.modules -->
1022 …linlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-se…
1023 …linlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-se…
1024 [_polymorphic]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx…
1025 [subclass]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.ser…
1026 [plus]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.seriali…
1027 …ang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-serial…
1028 …api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-polymorphic-mo…
1029 …ang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-polymo…
1030 …tlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-serializers-module-b…
1031 …ang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-serial…
1033 <!--- MODULE /kotlinx-serialization-json -->
1034 <!--- INDEX kotlinx-serialization-json/kotlinx.serialization.json -->
1036 …linlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/…
1037 …s://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json…
1039 <!--- END -->