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

Improved test of supporting of nullable serializer in UserSerializers annotation #1195

Merged
merged 1 commit into from
Dec 24, 2020
Merged
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
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tasks.withType(Jar).named(kotlin.jvm().artifactsTaskName) {
manifest {
attributes(
"Implementation-Version": version,
"Require-Kotlin-Version": "1.4-RC",
"Require-Kotlin-Version": "1.4.30-M1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: bug fixes do not require raising minimal kotlin version, but for this case, we wanted to do it anyway

)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:UseSerializers(MyIntSerializer::class)
@file:UseSerializers(NullableIntSerializer::class, NonNullableIntSerializer::class)

package kotlinx.serialization

Expand All @@ -14,25 +14,47 @@ import kotlin.test.*
class SerializationForNullableTypeOnFileTest {

@Serializable
data class Holder(val i: Int?)
data class Holder(val nullable: Int?, val nonNullable: Int)

@Serializer(forClass = Int::class)
object MyIntSerializer : KSerializer<Int?> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("MIS", PrimitiveKind.INT).nullable
object NullableIntSerializer : KSerializer<Int?> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("NullableIntSerializer", PrimitiveKind.INT).nullable

override fun serialize(encoder: Encoder, value: Int?) {
if (value == null) encoder.encodeInt(42)
else encoder.encodeInt(239)
if (value == null) encoder.encodeNull()
else encoder.encodeInt(value + 1)
}
override fun deserialize(decoder: Decoder): Int? {
return if (decoder.decodeNotNullMark()) {
val value = decoder.decodeInt()
value - 1
} else {
decoder.decodeNull()
}
}
}

@Serializer(forClass = Int::class)
object NonNullableIntSerializer : KSerializer<Int> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("NotNullIntSerializer", PrimitiveKind.INT)

override fun serialize(encoder: Encoder, value: Int) {
return encoder.encodeInt(value + 2)
}

override fun deserialize(decoder: Decoder): Int {
TODO()
return (decoder.decodeInt() - 2)
}
}

@Test
fun testFileLevel() {
assertEquals("""{"i":42}""", Json.encodeToString(Holder(null)))
assertEquals("""{"i":239}""", Json.encodeToString(Holder(314)))
assertEquals("""{"nullable":null,"nonNullable":52}""", Json.encodeToString(Holder(nullable = null, nonNullable = 50)))
assertEquals("""{"nullable":1,"nonNullable":2}""", Json.encodeToString(Holder(nullable = 0, nonNullable = 0)))
assertEquals("""{"nullable":11,"nonNullable":52}""", Json.encodeToString(Holder(nullable = 10, nonNullable = 50)))

assertEquals(Holder(nullable = 0, nonNullable = 50), Json.decodeFromString("""{"nullable":1,"nonNullable":52}"""))
assertEquals(Holder(nullable = null, nonNullable = 50), Json.decodeFromString("""{"nullable":null,"nonNullable":52}"""))
assertEquals(Holder(nullable = 10, nonNullable = 50), Json.decodeFromString("""{"nullable":11,"nonNullable":52}"""))
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
group=org.jetbrains.kotlinx
version=1.0.1-SNAPSHOT

kotlin.version=1.4.10
kotlin.version=1.4.30-M1

# This version take precedence if 'bootstrap' property passed to project
kotlin.version.snapshot=1.4.255-SNAPSHOT
Expand Down
2 changes: 1 addition & 1 deletion integration-test/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
#

mainKotlinVersion=1.4.10
mainKotlinVersion=1.4.30-M1
mainLibVersion=1.0.1-SNAPSHOT

kotlin.code.style=official
Expand Down