-
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 @InheritableSerialInfo and @JsonClassDiscriminator to confi…
…gure discriminator per polymorphic base class Fixes #546
- Loading branch information
1 parent
e75d43e
commit c478b94
Showing
11 changed files
with
317 additions
and
72 deletions.
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
46 changes: 46 additions & 0 deletions
46
core/commonTest/src/kotlinx/serialization/InheritableSerialInfoTest.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,46 @@ | ||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlin.test.* | ||
|
||
|
||
class InheritableSerialInfoTest { | ||
|
||
@InheritableSerialInfo | ||
annotation class InheritableDiscriminator(val discriminator: String) | ||
|
||
@InheritableDiscriminator("a") | ||
interface A | ||
|
||
@InheritableDiscriminator("a") | ||
interface B | ||
|
||
@InheritableDiscriminator("a") | ||
@Serializable | ||
abstract class C: A | ||
|
||
@Serializable | ||
sealed class D: C(), B | ||
|
||
@Serializable | ||
class E: D() | ||
|
||
@Serializable | ||
class E2: C() | ||
|
||
@Serializable | ||
class E3: A, B | ||
|
||
private fun doTest(descriptor: SerialDescriptor) { | ||
val list = descriptor.annotations.filterIsInstance<InheritableDiscriminator>() | ||
assertEquals(1, list.size) | ||
assertEquals("a", list.first().discriminator) | ||
} | ||
|
||
@Test | ||
fun testInheritanceFromSealed() = doTest(E.serializer().descriptor) | ||
@Test | ||
fun testInheritanceFromAbstract() = doTest(E2.serializer().descriptor) | ||
@Test | ||
fun testInheritanceFromInterface() = doTest(E3.serializer().descriptor) | ||
} |
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
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
/* | ||
* 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 and its subclasses; | ||
* to configure global class discriminator, use [JsonBuilder.classDiscriminator] | ||
* property. | ||
* | ||
* This annotation is [inheritable][InheritableSerialInfo], so it should be sufficient to place it on a base class of hierarchy. | ||
* It is not 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 | ||
* @JsonClassDiscriminator("message_type") | ||
* abstract class Base | ||
* | ||
* @Serializable // Class discriminator is inherited from Base | ||
* abstract class ErrorClass: Base() | ||
* | ||
* @Serializable | ||
* class Message(val message: Base, val error: ErrorClass?) | ||
* | ||
* val message = Json.decodeFromString<Message>("""{"message": {"message_type":"my.app.BaseMessage", "message": "not found"}, "error": {"message_type":"my.app.GenericError", "error_code": 404}}""") | ||
* ``` | ||
* | ||
* @see JsonBuilder.classDiscriminator | ||
*/ | ||
@InheritableSerialInfo | ||
@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
Oops, something went wrong.