Skip to content

Commit

Permalink
Remove @Serializer support
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov committed Feb 8, 2022
1 parent 0edaff7 commit 29806d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 104 deletions.
21 changes: 1 addition & 20 deletions core/commonMain/src/kotlinx/serialization/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,6 @@ public annotation class Serializable(
* serializer.descriptor.annotations.filterIsInstance<MySerializable>().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<out KSerializer<*>>,
* val data: String
* )
*
* @MetaSerializable(serializer = MyDataCustomSerializer::class, data = "some_data")
* class MyData(...)
*
* MyData.serializer() // <- returns MyDataCustomSerializer
* ```
*
* @see Serializable
* @see SerialInfo
* @see UseSerializers
Expand All @@ -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].
Expand Down
53 changes: 26 additions & 27 deletions core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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

Expand All @@ -11,47 +8,49 @@ import kotlin.test.Test
class MetaSerializableTest {

@MetaSerializable
@Target(AnnotationTarget.CLASS)
annotation class MySerializableWithCustomSerializer(@MetaSerializable.Serializer val with: KClass<out KSerializer<*>> = 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<Project1> {
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() {
fun testMetaSerializable() {
// val serializer = serializer<Project1>()
// assertEquals(serializer, MySerializer)
// assertNotNull(serializer)
}

@Test
fun testDefaultSerializer() {
// val serializer = serializer<Project2>()
// assertNotNull(serializer)
fun testMetaSerializableWithInfo() {
// val info = serializer<Project2>().descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first()
// assertEquals(123, info.value)
// assertEquals(String::class, info.kclass)
}

@Test
fun testDefaultSerialInfo() {
// val descriptor = serializer<Project3>().descriptor
// val annotation = descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first()
// assertEquals(123, annotation.value)
// assertEquals(String::class, annotation.klass)
fun testMetaSerializableOnProperty() {
// val info = serializer<Wrapper>().descriptor
// .getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first()
// assertEquals(234, info.value)
// assertEquals(Int::class, info.kclass)
}
}
57 changes: 0 additions & 57 deletions guide/test/MySerializable.kt

This file was deleted.

0 comments on commit 29806d0

Please sign in to comment.