diff --git a/core/commonMain/src/kotlinx/serialization/Annotations.kt b/core/commonMain/src/kotlinx/serialization/Annotations.kt index a6843f4288..c23244d4ba 100644 --- a/core/commonMain/src/kotlinx/serialization/Annotations.kt +++ b/core/commonMain/src/kotlinx/serialization/Annotations.kt @@ -105,22 +105,6 @@ public annotation class Serializable( * serializer.descriptor.annotations.filterIsInstance().first().data // <- returns "some_data" * ``` * - * Additionally, the user-defined serializer can be specified using parameter - * annotated with [MetaSerializable.Serializer]: - * ``` - * @MetaSerializable - * @Target(AnnotationTarget.CLASS) - * annotation class MySerializable( - * @MetaSerializable.Serializer val serializer: KClass>, - * val data: String - * ) - * - * @MetaSerializable(serializer = MyDataCustomSerializer::class, data = "some_data") - * class MyData(...) - * - * MyData.serializer() // <- returns MyDataCustomSerializer - * ``` - * * @see Serializable * @see SerialInfo * @see UseSerializers @@ -129,10 +113,7 @@ public annotation class Serializable( @Target(AnnotationTarget.ANNOTATION_CLASS) //@Retention(AnnotationRetention.RUNTIME) // Runtime is the default retention, also see KT-41082 @ExperimentalSerializationApi -public annotation class MetaSerializable { - @Target(AnnotationTarget.PROPERTY) - public annotation class Serializer -} +public annotation class MetaSerializable /** * Instructs the serialization plugin to turn this class into serializer for specified class [forClass]. diff --git a/core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt b/core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt index 94ba45abb6..6334bb12d7 100644 --- a/core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt +++ b/core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt @@ -1,57 +1,58 @@ package kotlinx.serialization -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.encoding.Decoder -import kotlinx.serialization.encoding.Encoder import kotlin.reflect.KClass import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull // TODO: for this test to work, kotlin dependency should be updated // to serialization plugin with @MetaSerializable support class MetaSerializableTest { @MetaSerializable - @Target(AnnotationTarget.CLASS) - annotation class MySerializableWithCustomSerializer(@MetaSerializable.Serializer val with: KClass> = KSerializer::class) + @Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) + annotation class MySerializable @MetaSerializable - @Target(AnnotationTarget.CLASS) - annotation class MySerializableWithInfo(val value: Int, val klass: KClass<*>) + @Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) + annotation class MySerializableWithInfo( + val value: Int, + val kclass: KClass<*> + ) - @MySerializableWithCustomSerializer(MySerializer::class) + @MySerializable class Project1(val name: String, val language: String) - @MySerializableWithCustomSerializer + @MySerializableWithInfo(123, String::class) class Project2(val name: String, val language: String) - @MySerializableWithInfo(value = 123, String::class) + @MySerializableWithInfo(123, String::class) + @Serializable class Project3(val name: String, val language: String) - object MySerializer : KSerializer { - override val descriptor: SerialDescriptor - get() = throw NotImplementedError() - - override fun serialize(encoder: Encoder, value: Project1) = throw NotImplementedError() - override fun deserialize(decoder: Decoder): Project1 = throw NotImplementedError() - } + @Serializable + class Wrapper( + @MySerializableWithInfo(234, Int::class) val project: Project3 + ) @Test - fun testCustomSerializer() { -// val serializer = serializer() -// assertEquals(serializer, MySerializer) + fun testMetaSerializable() { + val serializer = serializer() + assertNotNull(serializer) } @Test - fun testDefaultSerializer() { -// val serializer = serializer() -// assertNotNull(serializer) + fun testMetaSerializableWithInfo() { + val info = serializer().descriptor.annotations.filterIsInstance().first() + assertEquals(123, info.value) + assertEquals(String::class, info.kclass) } @Test - fun testDefaultSerialInfo() { -// val descriptor = serializer().descriptor -// val annotation = descriptor.annotations.filterIsInstance().first() -// assertEquals(123, annotation.value) -// assertEquals(String::class, annotation.klass) + fun testMetaSerializableOnProperty() { + val info = serializer().descriptor + .getElementAnnotations(0).filterIsInstance().first() + assertEquals(234, info.value) + assertEquals(Int::class, info.kclass) } }