Skip to content
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

Serializable meta-annotation #1678

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions core/commonMain/src/kotlinx/serialization/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,48 @@ public annotation class Serializable(
val with: KClass<out KSerializer<*>> = KSerializer::class // Default value indicates that auto-generated serializer is used
)

/**
* The meta-annotation for adding behaviour of [Serializable] to custom annotations.
* Applying [MetaSerializable] to the annotation class instructs the serialization plugin to treat this annotation
* as [Serializable].
*
* ```
* @MetaSerializable
* @Target(AnnotationTarget.CLASS)
* annotation class MySerializable
*
* @MySerializable
* class MyData(val myData: AnotherData, val intProperty: Int, ...)
*
* // Produces JSON string using the generated serializer
* val jsonString = Json.encodeToJson(MyData.serializer(), instance)
* ```
*
* All annotations marked with [MetaSerializable] are saved in the generated [SerialDescriptor]
* as if they are annotated [SerialInfo].
*
* ```
* @MetaSerializable
* @Target(AnnotationTarget.CLASS)
* annotation class MySerializable(val data: String)
*
* @MySerializable("some_data")
* class MyData(val myData: AnotherData, val intProperty: Int, ...)
*
* val serializer = MyData.serializer()
* serializer.descriptor.annotations.filterIsInstance<MySerializable>().first().data // <- returns "some_data"
* ```
*
* @see Serializable
* @see SerialInfo
* @see UseSerializers
* @see Serializer
*/
@Target(AnnotationTarget.ANNOTATION_CLASS)
//@Retention(AnnotationRetention.RUNTIME) // Runtime is the default retention, also see KT-41082
@ExperimentalSerializationApi
public annotation class MetaSerializable

/**
* Instructs the serialization plugin to turn this class into serializer for specified class [forClass].
* However, it would not be used automatically. To apply it on particular class or property,
Expand Down
56 changes: 56 additions & 0 deletions core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kotlinx.serialization

import kotlin.reflect.KClass
import kotlin.test.Test

// TODO: for this test to work, kotlin dependency should be updated
// to serialization plugin with @MetaSerializable support
class MetaSerializableTest {

@MetaSerializable
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
annotation class MySerializable

@MetaSerializable
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
annotation class MySerializableWithInfo(
val value: Int,
val kclass: KClass<*>
)

@MySerializable
class Project1(val name: String, val language: String)

@MySerializableWithInfo(123, String::class)
class Project2(val name: String, val language: String)

@MySerializableWithInfo(123, String::class)
@Serializable
class Project3(val name: String, val language: String)

@Serializable
class Wrapper(
@MySerializableWithInfo(234, Int::class) val project: Project3
)

@Test
fun testMetaSerializable() {
// val serializer = serializer<Project1>()
// assertNotNull(serializer)
}

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

@Test
fun testMetaSerializableOnProperty() {
// val info = serializer<Wrapper>().descriptor
// .getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first()
// assertEquals(234, info.value)
// assertEquals(Int::class, info.kclass)
}
}