diff --git a/core/build.gradle b/core/build.gradle index 5a5fc17a9..b30646163 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -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", ) } } diff --git a/formats/json/commonTest/src/kotlinx/serialization/SerializationForNullableTypeOnFileTest.kt b/formats/json/commonTest/src/kotlinx/serialization/SerializationForNullableTypeOnFileTest.kt index 62accdaf3..9f838b11a 100644 --- a/formats/json/commonTest/src/kotlinx/serialization/SerializationForNullableTypeOnFileTest.kt +++ b/formats/json/commonTest/src/kotlinx/serialization/SerializationForNullableTypeOnFileTest.kt @@ -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 @@ -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 { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("MIS", PrimitiveKind.INT).nullable + object NullableIntSerializer : KSerializer { + 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 { + 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}""")) } } diff --git a/gradle.properties b/gradle.properties index 2931b746e..ac3aa94f3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/integration-test/gradle.properties b/integration-test/gradle.properties index 048fa018e..193e6868a 100644 --- a/integration-test/gradle.properties +++ b/integration-test/gradle.properties @@ -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