-
Notifications
You must be signed in to change notification settings - Fork 624
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
Add @MetaSerializable annotation #1979
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.test.* | ||
import kotlin.reflect.KClass | ||
import kotlin.test.* | ||
|
||
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() = noJsLegacy { | ||
val serializer = serializer<Project1>() | ||
assertNotNull(serializer) | ||
} | ||
|
||
@Test | ||
fun testMetaSerializableWithInfo() = noJsLegacy { | ||
val info = serializer<Project2>().descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first() | ||
assertEquals(123, info.value) | ||
assertEquals(String::class, info.kclass) | ||
} | ||
|
||
@Test | ||
fun testMetaSerializableOnProperty() = noJsLegacy { | ||
val info = serializer<Wrapper>().descriptor | ||
.getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first() | ||
assertEquals(234, info.value) | ||
assertEquals(Int::class, info.kclass) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
formats/json-tests/commonTest/src/kotlinx/serialization/features/MetaSerializableJsonTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlin.test.* | ||
|
||
class MetaSerializableJsonTest : JsonTestBase() { | ||
@MetaSerializable | ||
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS) | ||
annotation class JsonComment(val comment: String) | ||
|
||
@JsonComment("class_comment") | ||
data class IntDataCommented(val i: Int) | ||
|
||
@Serializable | ||
data class Carrier( | ||
val plain: String, | ||
@JsonComment("string_comment") val commented: StringData, | ||
val intData: IntDataCommented | ||
) | ||
|
||
class CarrierSerializer : JsonTransformingSerializer<Carrier>(serializer()) { | ||
|
||
private val desc = Carrier.serializer().descriptor | ||
private fun List<Annotation>.comment(): String? = filterIsInstance<JsonComment>().firstOrNull()?.comment | ||
|
||
private val commentMap = (0 until desc.elementsCount).associateBy({ desc.getElementName(it) }, | ||
{ desc.getElementAnnotations(it).comment() ?: desc.getElementDescriptor(it).annotations.comment() }) | ||
|
||
// NB: we may want to add this to public API | ||
private fun JsonElement.editObject(action: (MutableMap<String, JsonElement>) -> Unit): JsonElement { | ||
val mutable = this.jsonObject.toMutableMap() | ||
action(mutable) | ||
return JsonObject(mutable) | ||
} | ||
|
||
override fun transformDeserialize(element: JsonElement): JsonElement { | ||
return element.editObject { result -> | ||
for ((key, value) in result) { | ||
commentMap[key]?.let { | ||
result[key] = value.editObject { | ||
it.remove("comment") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun transformSerialize(element: JsonElement): JsonElement { | ||
return element.editObject { result -> | ||
for ((key, value) in result) { | ||
commentMap[key]?.let { comment -> | ||
result[key] = value.editObject { | ||
it["comment"] = JsonPrimitive(comment) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testMyJsonComment() { | ||
assertJsonFormAndRestored( | ||
CarrierSerializer(), | ||
Carrier("plain", StringData("string1"), IntDataCommented(42)), | ||
"""{"plain":"plain","commented":{"data":"string1","comment":"string_comment"},"intData":{"i":42,"comment":"class_comment"}}""" | ||
) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it affect JS IR as well? Is this feature implemented on JS legacy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's supported on all IR backends. JS legacy not supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the problem mentioned here specific only for JS legacy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but this file is compiled for all backends, and JS legacy compiler will issue a lengthy warning