Skip to content

Support using an integer as the class/case discriminator in polymorphic serialization #2587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Review all changes since commit 41c0bb1, improving some code and comm…
…ents and reverting some unnecessary changes
  • Loading branch information
ShreckYe committed Mar 2, 2024
commit 4973f071d84d8119008fc8facf3db43858305d0b
2 changes: 1 addition & 1 deletion core/commonMain/src/kotlinx/serialization/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public annotation class Serializer(
public annotation class SerialName(val value: String)

/**
* Requires all subclasses to use [SerialPolymorphicNumber].
* Requires all subclasses marked with this annotation to use [SerialPolymorphicNumber].
*/
@SerialInfo
@Target(AnnotationTarget.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public class SealedClassSerializer<T : Any>(
// Plugin should produce identical serializers, although they are not always strictly equal (e.g. new ObjectSerializer
// may be created every time)
class2Serializer = subclasses.zip(subclassSerializers).toMap()

serialName2Serializer = class2Serializer.entries.groupingBy { it.value.descriptor.serialName }
.aggregate<Map.Entry<KClass<out T>, KSerializer<out T>>, String, Map.Entry<KClass<*>, KSerializer<out T>>>
{ key, accumulator, element, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ internal fun throwSubtypeNotRegistered(serialPolymorphicNumber: Int?, baseClass:
throw SerializationException(
(
if (serialPolymorphicNumber == null)
"Class discriminator serial polymorphic number was missing and no default serializers were registered $scope."
"Class discriminator (serial polymorphic number) was missing and no default serializers were registered $scope."
else
"Serializer for subclass serial polymorphic number '$serialPolymorphicNumber' is not found $scope.\n" +
"Serializer for subclass serial polymorphic number '$serialPolymorphicNumber' is not found in $scope.\n" +
"Check if class with serial polymorphic number '$serialPolymorphicNumber' exists and serializer is registered in a corresponding SerializersModule.\n" +
"To be registered automatically, class annotated with '@SerialPolymorphicNumber($serialPolymorphicNumber)' has to be '@Serializable', and the base class '${baseClass.simpleName}' has to be sealed and '@Serializable'.\n"
"To be registered automatically, class annotated with '@SerialPolymorphicNumber($serialPolymorphicNumber)' has to be '@Serializable', and the base class '${baseClass.simpleName}' marked with `@UseSerialPolymorphicNumbers` has to be sealed and '@Serializable'.\n"
) +
"\nRemove the `@UseSerialPolymorphicNumbers` annotation from the base class `${baseClass.simpleName}` if you want to switch back to polymorphic serialization using the serial name strings."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class SerializersModuleBuilder @PublishedApi internal constructor() : Ser
internal fun <Base : Any> registerDefaultPolymorphicDeserializer(
baseClass: KClass<Base>,
defaultDeserializerProvider: (className: String?) -> DeserializationStrategy<Base>?,
//defaultDeserializerProvider: PolymorphicDeserializerProvider<Base>,
//defaultDeserializerProvider: PolymorphicDeserializerProvider<Base>, // this causes the build to fail on JS, but only when there is no trailing comment such as this one, which is strange
allowOverwrite: Boolean
) {
val previous = polyBase2DefaultDeserializerProvider[baseClass]
Expand All @@ -202,7 +202,7 @@ public class SerializersModuleBuilder @PublishedApi internal constructor() : Ser
internal fun <Base : Any> registerDefaultPolymorphicDeserializerForNumber(
baseClass: KClass<Base>,
defaultDeserializerProvider: (polymorphicSerialNumber: Int?) -> DeserializationStrategy<Base>?,
//defaultDeserializerProvider: PolymorphicDeserializerProviderForNumber<Base>,
//defaultDeserializerProvider: PolymorphicDeserializerProviderForNumber<Base>, // this causes the build to fail on JS, but only when there is no trailing comment such as this one, which is strange
allowOverwrite: Boolean
) {
val previous = polyBase2DefaultDeserializerProviderForNumber[baseClass]
Expand Down