-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce @JsonClassDiscriminator to configure discriminator per poly…
…morphic base class Fixes #546
- Loading branch information
1 parent
9d965ba
commit 4af5547
Showing
8 changed files
with
191 additions
and
70 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
formats/json/commonMain/src/kotlinx/serialization/json/JsonAnnotations.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 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.json | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.json.internal.* | ||
import kotlin.native.concurrent.* | ||
|
||
/** | ||
* Indicates that the field can be represented in JSON | ||
* with multiple possible alternative names. | ||
* [Json] format recognizes this annotation and is able to decode | ||
* the data using any of the alternative names. | ||
* | ||
* Unlike [SerialName] annotation, does not affect JSON encoding in any way. | ||
* | ||
* Example of usage: | ||
* ``` | ||
* @Serializable | ||
* data class Project(@JsonNames("title") val name: String) | ||
* | ||
* val project = Json.decodeFromString<Project>("""{"name":"kotlinx.serialization"}""") | ||
* println(project) | ||
* val oldProject = Json.decodeFromString<Project>("""{"title":"kotlinx.coroutines"}""") | ||
* println(oldProject) | ||
* ``` | ||
* | ||
* This annotation has lesser priority than [SerialName]. | ||
* | ||
* @see JsonBuilder.useAlternativeNames | ||
*/ | ||
@SerialInfo | ||
@Target(AnnotationTarget.PROPERTY) | ||
@ExperimentalSerializationApi | ||
public annotation class JsonNames(vararg val names: String) | ||
|
||
/** | ||
* Specifies key for class discriminator value used during polymorphic serialization in [Json]. | ||
* Provided key is used only for an annotated class, to configure global class discriminator, use [JsonBuilder.classDiscriminator] | ||
* property. | ||
* | ||
* It is possible to define different class discriminators for different parts of class hierarchy. | ||
* Pay attention to the fact that class discriminator, same as polymorphic serializer's base class, is | ||
* determined statically. | ||
* | ||
* Example: | ||
* ``` | ||
* @Serializable | ||
* @JsonTypeDiscriminator("class") | ||
* abstract class Base | ||
* | ||
* @Serializable | ||
* @JsonTypeDiscriminator("error_class") | ||
* abstract class ErrorClass: Base() | ||
* | ||
* @Serializable | ||
* class Message(val object: Base, val error: ErrorClass?) | ||
* | ||
* val message = Json.decodeFromString<Message>("""{"object": {"class":"my.app.BaseMessage", "message": "not found"}, "error": {"error_class":"my.app.GenericError", "error_code": 404}}""") | ||
* ``` | ||
* | ||
* @see JsonBuilder.classDiscriminator | ||
*/ | ||
@SerialInfo | ||
@Target(AnnotationTarget.CLASS) | ||
@ExperimentalSerializationApi | ||
public annotation class JsonClassDiscriminator(val discriminator: String) |
39 changes: 0 additions & 39 deletions
39
formats/json/commonMain/src/kotlinx/serialization/json/JsonNames.kt
This file was deleted.
Oops, something went wrong.
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
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
70 changes: 70 additions & 0 deletions
70
formats/json/commonTest/src/kotlinx/serialization/features/JsonClassDiscriminatorTest.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,70 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.builtins.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.* | ||
import kotlin.test.* | ||
|
||
class JsonClassDiscriminatorTest : JsonTestBase() { | ||
@Serializable | ||
@JsonClassDiscriminator("sealedType") | ||
sealed class SealedMessage { | ||
@Serializable | ||
@SerialName("SealedMessage.StringMessage") | ||
data class StringMessage(val description: String, val message: String) : SealedMessage() | ||
|
||
@SerialName("EOF") | ||
@Serializable | ||
object EOF : SealedMessage() | ||
} | ||
|
||
@Serializable | ||
@JsonClassDiscriminator("abstractType") | ||
abstract class Message { | ||
@Serializable | ||
@SerialName("Message.StringMessage") | ||
data class StringMessage(val description: String, val message: String) : Message() | ||
|
||
@Serializable | ||
@SerialName("Message.IntMessage") | ||
data class IntMessage(val description: String, val message: Int) : Message() | ||
} | ||
|
||
@Test | ||
fun testSealedClassesHaveCustomDiscriminator() { | ||
val messages = listOf( | ||
SealedMessage.StringMessage("string message", "foo"), | ||
SealedMessage.EOF | ||
) | ||
val expected = | ||
"""[{"sealedType":"SealedMessage.StringMessage","description":"string message","message":"foo"},{"sealedType":"EOF"}]""" | ||
assertJsonFormAndRestored( | ||
ListSerializer(SealedMessage.serializer()), | ||
messages, | ||
expected, | ||
) | ||
} | ||
|
||
@Test | ||
fun testAbstractClassesHaveCustomDiscriminator() { | ||
val messages = listOf( | ||
Message.StringMessage("string message", "foo"), | ||
Message.IntMessage("int message", 42), | ||
) | ||
val module = SerializersModule { | ||
polymorphic(Message::class) { | ||
subclass(Message.StringMessage.serializer()) | ||
subclass(Message.IntMessage.serializer()) | ||
} | ||
} | ||
val json = Json { serializersModule = module } | ||
val expected = | ||
"""[{"abstractType":"Message.StringMessage","description":"string message","message":"foo"},{"abstractType":"Message.IntMessage","description":"int message","message":42}]""" | ||
assertJsonFormAndRestored(ListSerializer(Message.serializer()), messages, expected, json) | ||
} | ||
} |
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