Skip to content

Commit a6c4d67

Browse files
Added annotation to keep generated serializer when a custom one is specified (#2566)
If the @KeepGeneratedSerializer annotation is specified on the class, and a custom serializer is specified, then keep to generate a plugin serializer. Resolves #1169 Co-authored-by: Leonid Startsev <[email protected]>
1 parent 729fc24 commit a6c4d67

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

core/api/kotlinx-serialization-core.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public abstract interface class kotlinx/serialization/KSerializer : kotlinx/seri
4444
public abstract fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
4545
}
4646

47+
public abstract interface annotation class kotlinx/serialization/KeepGeneratedSerializer : java/lang/annotation/Annotation {
48+
}
49+
4750
public abstract interface annotation class kotlinx/serialization/MetaSerializable : java/lang/annotation/Annotation {
4851
}
4952

core/commonMain/src/kotlinx/serialization/Annotations.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,23 @@ public annotation class UseSerializers(vararg val serializerClasses: KClass<out
324324
//@Retention(AnnotationRetention.RUNTIME) // Runtime is the default retention, also see KT-41082
325325
public annotation class Polymorphic
326326

327+
/**
328+
* Instructs the serialization plugin to keep automatically generated implementation of [KSerializer]
329+
* for the current class if a custom serializer is specified at the same time `@Serializable(with=SomeSerializer::class)`.
330+
*
331+
* Automatically generated serializer is available via `generatedSerializer()` function in companion object of serializable class.
332+
*
333+
* Generated serializers allow to use custom serializers on classes from which other serializable classes are inherited.
334+
*
335+
* Used only with the [Serializable] annotation.
336+
*
337+
* A compiler version `2.0.0` and higher is required.
338+
*/
339+
@InternalSerializationApi
340+
@Target(AnnotationTarget.CLASS)
341+
@Retention(AnnotationRetention.RUNTIME)
342+
public annotation class KeepGeneratedSerializer
343+
327344
/**
328345
* Marks declarations that are still **experimental** in kotlinx.serialization, which means that the design of the
329346
* corresponding declarations has open issues which may (or may not) lead to their changes in the future.

0 commit comments

Comments
 (0)