Skip to content

Commit 4bdff60

Browse files
committed
Tidy up the code a bit (and use the cast method on the serializer to avoid casting warnings). Use a map to allow faster lookup of serializers to class (to allow for overlapping hierarchies).
1 parent 20d410e commit 4bdff60

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

core/commonMain/src/kotlinx/serialization/modules/PolymorphicModuleBuilder.kt

+13-11
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
1919
private val baseClass: KClass<Base>,
2020
private val baseSerializer: KSerializer<Base>? = null
2121
) {
22-
private val subclasses: MutableList<Pair<KClass<out Base>, KSerializer<out Base>>> = mutableListOf()
22+
private val subclasses: MutableMap<KClass<out Base>, KSerializer<out Base>> = mutableMapOf()
2323
private var defaultSerializerProvider: ((Base) -> SerializationStrategy<Base>?)? = null
2424
private var defaultDeserializerProvider: ((String?) -> DeserializationStrategy<Base>?)? = null
2525

26+
27+
/**
28+
* Registers the child serializers for the sealed [subclass] [serializer] in the resulting module under the [base class][Base].
29+
*/
30+
public inline fun <reified T : Base> subclassesOf(): Unit =
31+
subclassesOf(serializer<T>())
32+
33+
2634
/**
27-
* Registers the subclasses of the given class as subclasses of the outer class. Currently this requires `baseClass`
35+
* Registers the subclasses of the given class as subclasses of the outer class. This currently requires `baseClass`
2836
* to be sealed.
2937
*/
30-
public fun <T: Base> subclassesOf(baseClass: KClass<T>, serializer: KSerializer<T>) {
38+
public fun <T: Base> subclassesOf(serializer: KSerializer<T>) {
3139
require(serializer is SealedClassSerializer) {
3240
"subClassesOf only supports automatic adding of subclasses of sealed types."
3341
}
@@ -42,7 +50,7 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
4250
* Registers a [subclass] [serializer] in the resulting module under the [base class][Base].
4351
*/
4452
public fun <T : Base> subclass(subclass: KClass<T>, serializer: KSerializer<T>) {
45-
subclasses.add(subclass to serializer)
53+
subclasses[subclass] = serializer
4654
}
4755

4856
/**
@@ -132,14 +140,8 @@ public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.
132140
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclass(clazz: KClass<T>): Unit =
133141
subclass(clazz, serializer())
134142

135-
/**
136-
* Registers the child serializers for the sealed [subclass] [serializer] in the resulting module under the [base class][Base].
137-
*/
138-
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclassesOf(serializer: KSerializer<T>): Unit =
139-
subclassesOf(T::class, serializer)
140-
141143
/**
142144
* Registers the child serializers for the sealed class [T] in the resulting module under the [base class][Base].
143145
*/
144146
public inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder<Base>.subclassesOf(clazz: KClass<T>): Unit =
145-
subclassesOf(clazz, serializer())
147+
subclassesOf(clazz.serializer())

docs/polymorphism.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ fun main() {
411411
> Note: On Kotlin/Native, you should use `format.encodeToString(PolymorphicSerializer(Project::class), data))` instead due to limited reflection capabilities.
412412
413413
### Registering sealed children as subclasses
414-
A sealed parent interface or class can be used to directly register all its children using subclassesOf. This will
414+
A sealed parent interface or class can be used to directly register all its children using `subclassesOf`. This will
415415
expose all children that would be available when serializing the parent directly, but now as sealed. Please note that
416416
this is will remain open serialization, and the sealed parent serializer will not be used in serialization.
417417

formats/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicSealedChildTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PolymorphicSealedChildTest {
3434

3535
val sealedModule = SerializersModule {
3636
polymorphic(FooBase::class) {
37-
subclassesOf(Foo.serializer())
37+
subclassesOf<Foo>()
3838
}
3939
}
4040

0 commit comments

Comments
 (0)