@@ -19,6 +19,7 @@ In this chapter we'll see how Kotlin Serialization deals with polymorphic class
1919* [ Open polymorphism] ( #open-polymorphism )
2020 * [ Registered subclasses] ( #registered-subclasses )
2121 * [ Serializing interfaces] ( #serializing-interfaces )
22+ * [ Registering sealed children as subclasses] ( #registering-sealed-children-as-subclasses )
2223 * [ Property of an interface type] ( #property-of-an-interface-type )
2324 * [ Static parent type lookup for polymorphism] ( #static-parent-type-lookup-for-polymorphism )
2425 * [ Explicitly marking polymorphic class properties] ( #explicitly-marking-polymorphic-class-properties )
@@ -421,6 +422,60 @@ note that this is will remain open serialization, and the sealed parent serializ
421422In addition, it is not valid if the hierarchy contains open (not sealed) polymorphic children (this will result in
422423an error at runtime). In other words all children/descendants must be either concrete or sealed.
423424
425+ <!-- - TEST -->
426+
427+ <!-- - INCLUDE
428+ import kotlinx.serialization.modules.*
429+ -->
430+
431+ ``` kotlin
432+ interface Base
433+
434+ @Serializable
435+ sealed interface Sub : Base
436+
437+ @Serializable
438+ class Sub1 (val data : String ): Sub
439+
440+ val module1 = SerializersModule {
441+ polymorphic(Base ::class ) {
442+ subclassesOfSealed(Sub .serializer())
443+ }
444+ }
445+
446+ val format1 = Json { serializersModule = module1 }
447+ ```
448+
449+ Alternatively the convenience overload allows specifying the sealed type as type parameter.
450+
451+ ``` kotlin
452+ val module2 = SerializersModule {
453+ polymorphic(Base ::class ) {
454+ subclassesOfSealed<Sub >()
455+ }
456+ }
457+
458+ val format2 = Json { serializersModule = module2 }
459+ ```
460+
461+ Now if we declare ` data ` with the type of ` Base ` we can simply call ` format.encodeToString ` as before.
462+ ``` kotlin
463+
464+ fun main () {
465+ val data: Base = Sub1 (" kotlin" )
466+ println (format1.encodeToString(data))
467+ println (format2.encodeToString(data))
468+ }
469+ ```
470+
471+ ``` text
472+ {"type":"example.examplePoly11.Sub1","data":"kotlin"}
473+ {"type":"example.examplePoly11.Sub1","data":"kotlin"}
474+ ```
475+
476+ > You can get the full code [ here] ( ../guide/example/example-poly-11.kt ) .
477+
478+
424479<!-- - TEST LINES_START -->
425480
426481### Property of an interface type
@@ -458,7 +513,7 @@ fun main() {
458513}
459514```
460515
461- > You can get the full code [ here] ( ../guide/example/example-poly-11 .kt ) .
516+ > You can get the full code [ here] ( ../guide/example/example-poly-12 .kt ) .
462517
463518As long as we've registered the actual subtype of the interface that is being serialized in
464519the [ SerializersModule] of our ` format ` , we get it working at runtime.
@@ -503,7 +558,7 @@ fun main() {
503558}
504559```
505560
506- > You can get the full code [ here] ( ../guide/example/example-poly-12 .kt ) .
561+ > You can get the full code [ here] ( ../guide/example/example-poly-13 .kt ) .
507562
508563We get the exception.
509564
@@ -551,7 +606,7 @@ fun main() {
551606}
552607```
553608
554- > You can get the full code [ here] ( ../guide/example/example-poly-13 .kt ) .
609+ > You can get the full code [ here] ( ../guide/example/example-poly-14 .kt ) .
555610
556611However, ` Any ` is a class and it is not serializable:
557612
@@ -593,7 +648,7 @@ fun main() {
593648}
594649```
595650
596- > You can get the full code [ here] ( ../guide/example/example-poly-14 .kt ) .
651+ > You can get the full code [ here] ( ../guide/example/example-poly-15 .kt ) .
597652
598653With the explicit serializer it works as before.
599654
@@ -646,7 +701,7 @@ fun main() {
646701}
647702```
648703
649- > You can get the full code [ here] ( ../guide/example/example-poly-15 .kt ) .
704+ > You can get the full code [ here] ( ../guide/example/example-poly-16 .kt ) .
650705
651706<!-- - TEST
652707{"project":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}}
@@ -699,7 +754,7 @@ fun main() {
699754}
700755-->
701756
702- > You can get the full code [ here] ( ../guide/example/example-poly-16 .kt ) .
757+ > You can get the full code [ here] ( ../guide/example/example-poly-17 .kt ) .
703758
704759<!-- - TEST
705760{"project":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"},"any":{"type":"owned","name":"kotlinx.coroutines","owner":"kotlin"}}
@@ -790,7 +845,7 @@ fun main() {
790845
791846```
792847
793- > You can get the full code [ here] ( ../guide/example/example-poly-17 .kt ) .
848+ > You can get the full code [ here] ( ../guide/example/example-poly-18 .kt ) .
794849
795850The JSON that is being produced is deeply polymorphic.
796851
@@ -838,7 +893,7 @@ fun main() {
838893}
839894```
840895
841- > You can get the full code [ here] ( ../guide/example/example-poly-18 .kt ) .
896+ > You can get the full code [ here] ( ../guide/example/example-poly-19 .kt ) .
842897
843898We get the following exception.
844899
@@ -901,7 +956,7 @@ fun main() {
901956}
902957```
903958
904- > You can get the full code [ here] ( ../guide/example/example-poly-19 .kt ) .
959+ > You can get the full code [ here] ( ../guide/example/example-poly-20 .kt ) .
905960
906961Notice, how ` BasicProject ` had also captured the specified type key in its ` type ` property.
907962
@@ -1005,7 +1060,7 @@ fun main() {
10051060}
10061061```
10071062
1008- > You can get the full code [ here] ( ../guide/example/example-poly-20 .kt )
1063+ > You can get the full code [ here] ( ../guide/example/example-poly-21 .kt )
10091064
10101065``` text
10111066{"type":"Cat","catType":"Tabby"}
0 commit comments