-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kotlinx.serialization] Correctly handle square bracket syntax for fi…
…le-level annotations such as @UseContextualSerialization and @UseSerializers. Fixes Kotlin/kotlinx.serialization#2783 Merge-request: KT-MR-17576 Merged-by: Leonid Startsev <leonid.startsev@jetbrains.com>
- Loading branch information
1 parent
5529c50
commit 87f948b
Showing
7 changed files
with
116 additions
and
8 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
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
75 changes: 75 additions & 0 deletions
75
plugins/kotlinx-serialization/testData/boxIr/annotationsOnFileExplicitArray.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,75 @@ | ||
// WITH_STDLIB | ||
|
||
// FILE: a.kt | ||
|
||
package a | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
|
||
object MultiplyingIntSerializer : KSerializer<Int> { | ||
override val descriptor: SerialDescriptor | ||
get() = PrimitiveSerialDescriptor("MultiplyingInt", PrimitiveKind.INT) | ||
|
||
override fun deserialize(decoder: Decoder): Int { | ||
return decoder.decodeInt() / 2 | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Int) { | ||
encoder.encodeInt(value * 2) | ||
} | ||
} | ||
|
||
data class Cont(val i: Int) | ||
|
||
object ContSerializer: KSerializer<Cont> { | ||
override fun deserialize(decoder: Decoder): Cont { | ||
return Cont(decoder.decodeInt()) | ||
} | ||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ContSerializer", PrimitiveKind.INT) | ||
|
||
override fun serialize(encoder: Encoder, value: Cont) { | ||
encoder.encodeInt(value.i) | ||
} | ||
} | ||
|
||
// FILE: test.kt | ||
|
||
@file:UseContextualSerialization(forClasses = [Cont::class]) | ||
@file:UseSerializers(*[MultiplyingIntSerializer::class]) | ||
|
||
package a | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.* | ||
|
||
@Serializable | ||
class Holder( | ||
val i: Int, | ||
val c: Cont | ||
) | ||
|
||
fun testOnFile(): String { | ||
val j = Json { | ||
serializersModule = SerializersModule { | ||
contextual(ContSerializer) | ||
} | ||
} | ||
val h = Holder(3, Cont(4)) | ||
val str = j.encodeToString( | ||
Holder.serializer(), | ||
h | ||
) | ||
if ("""{"i":6,"c":4}""" != str) return str | ||
val decoded = j.decodeFromString(Holder.serializer(), str) | ||
if (decoded.i != h.i) return "i: ${decoded.i}" | ||
if (decoded.c.i != h.c.i) return "c.i: ${decoded.c.i}" | ||
return "OK" | ||
} | ||
|
||
fun box(): String { | ||
return testOnFile() | ||
} |
6 changes: 6 additions & 0 deletions
6
...s-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...tbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ts-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.