Skip to content

Commit c253abe

Browse files
e5lerokhinsqurbonzoda
authored
Update to kotlin 1.4-RC (#2017)
* Bump kotlin version to 1.4.0-rc-154 * Migrate to the new serialization version * Temporary disable benchmarks. Unfortunately it isn't possible to recompile kotlinx.benchmarks due to KT-40058. And it isn't possible to use 1.4-M2 version because of pre-release flag. * Update to new version of kotlinx.serialization * Bump kotlin to 1.4.0-rc-218 * Fix cinterop publishing (#1916) KT-39302, cherry-pich from f239813 * Add linuxX64 back to list of cross-compile targets This change were forgotten in a41ddf2 * Hack konanTarget list of native-shared compilations * Bump kotlin version to 1.4.0-rc * Enable benchmarks with new version of kotlinx-benchmark (#2015) * Revert "Temporary disable benchmarks." This reverts commit b4ee050 * Upgrade benchmarks plugin to 0.2.0-dev-17 * Update to new serialization API * Update public API Co-authored-by: Stanislav Erokhin <[email protected]> Co-authored-by: Abduqodiri Qurbonzoda <[email protected]>
1 parent 22b5d7a commit c253abe

File tree

13 files changed

+154
-102
lines changed

13 files changed

+154
-102
lines changed

gradle.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ org.gradle.daemon=true
1414
org.gradle.jvmargs=-Xmx2g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
1515

1616
# kotlin
17-
kotlin_version=1.4-M3
17+
kotlin_version=1.4.0-rc
1818
kotlin.native.ignoreDisabledTargets=true
1919

2020
# kotlin libraries
21-
benchmarks_version=0.2.0-mpp-dev-5
21+
benchmarks_version=0.2.0-dev-17
2222

23-
serialization_version=0.20.0-1.4-M3
24-
coroutines_version=1.3.7-1.4-M3
25-
atomicfu_version=0.14.3-1.4-M3
23+
serialization_version=1.0-M1-1.4.0-rc
24+
coroutines_version=1.3.8-1.4.0-rc
25+
atomicfu_version=0.14.3-1.4.0-rc
2626
validator_version=0.2.2
2727

2828
# server

gradle/posix.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,22 @@ kotlin {
104104
}
105105

106106
project.ext.set("hasNative", true)
107+
108+
109+
// hack for 1.4-M2, remove after
110+
// see https://youtrack.jetbrains.com/issue/KT-39302
111+
afterEvaluate {
112+
def metadataTarget = kotlin.targets.getByName("metadata")
113+
114+
def currentHostKonanTarget = hostManager.Companion.getHost()
115+
116+
if (metadataTarget != null) {
117+
metadataTarget.compilations.forEach {
118+
if (it.class.name == "org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation"
119+
&& it.konanTargets.any { it == currentHostKonanTarget }
120+
) {
121+
it.kotlinOptions.freeCompilerArgs += ["-target", currentHostKonanTarget.name]
122+
}
123+
}
124+
}
125+
}

ktor-client/ktor-client-features/ktor-client-json/common/test/CollectionsSerializationTest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ class CollectionsSerializationTest {
1515

1616
@Test
1717
fun testJsonElements() {
18-
testSerializer.testWrite(json {
19-
"a" to "1"
20-
"b" to json {
21-
"c" to 3
22-
}
23-
"x" to JsonNull
18+
testSerializer.testWrite(buildJsonObject {
19+
put("a", "1")
20+
put("b", buildJsonObject {
21+
put("c", 3)
22+
})
23+
put("x", JsonNull)
2424
}).let { result ->
2525
assertEquals("""{"a":"1","b":{"c":3},"x":null}""", result)
2626
}
2727

28-
testSerializer.testWrite(json {
29-
"a" to "1"
30-
"b" to jsonArray {
31-
+"c"
32-
+JsonPrimitive(2)
33-
}
28+
testSerializer.testWrite(buildJsonObject {
29+
put("a", "1")
30+
put("b", buildJsonArray {
31+
add("c")
32+
add(JsonPrimitive(2))
33+
})
3434
}).let { result ->
3535
assertEquals("""{"a":"1","b":["c",2]}""", result)
3636
}

ktor-client/ktor-client-features/ktor-client-json/ktor-client-json-tests/jvm/test/KotlinxSerializerTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ class KotlinxSerializerTest {
2020
fun testCustomDeserializer() {
2121
val upwrapper = indexListUnwrapper<TestEntry>()
2222

23-
@OptIn(UnstableDefault::class)
2423
val serializer = Json {
2524
ignoreUnknownKeys = true
26-
serialModule = serializersModule(upwrapper)
25+
serializersModule = serializersModuleOf(upwrapper)
2726
}
2827

2928
val kotlinxSerializer = KotlinxSerializer(serializer)
@@ -49,11 +48,10 @@ class KotlinxSerializerTest {
4948
@Serializable
5049
data class TestEntry(val a: String, val b: Int)
5150

52-
@ImplicitReflectionSerializer
53-
inline fun <reified T> indexListUnwrapper() =
54-
object : JsonTransformingSerializer<List<T>>(serializer<T>().list, "unwrap") {
55-
override fun readTransform(element: JsonElement): JsonElement {
56-
return if (element is JsonArray) element else element.jsonObject.values.firstOrNull { it is JsonArray }
57-
?: error("Collection not found in json")
58-
}
51+
inline fun <reified T> indexListUnwrapper(
52+
) = object : JsonTransformingSerializer<List<T>>(ListSerializer<T>(serializer<T>())) {
53+
override fun transformDeserialize(element: JsonElement): JsonElement {
54+
return if (element is JsonArray) element else element.jsonObject.values.firstOrNull { it is JsonArray }
55+
?: error("Collection not found in json")
5956
}
57+
}

ktor-client/ktor-client-features/ktor-client-json/ktor-client-serialization/api/ktor-client-serialization.api

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public final class io/ktor/client/features/json/serializer/KotlinxSerializer : i
99
}
1010

1111
public final class io/ktor/client/features/json/serializer/KotlinxSerializer$Companion {
12-
public final fun getDefaultJsonConfiguration ()Lkotlinx/serialization/json/JsonConfiguration;
12+
public final fun getDefaultJson ()Lkotlinx/serialization/json/Json;
13+
public final fun getDefaultJsonConfiguration ()Lkotlinx/serialization/json/Json;
1314
}
1415

ktor-client/ktor-client-features/ktor-client-json/ktor-client-serialization/common/src/io/ktor/client/features/json/serializer/KotlinxSerializer.kt

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import kotlinx.serialization.modules.*
1717
/**
1818
* A [JsonSerializer] implemented for kotlinx [Serializable] classes.
1919
*/
20-
@Suppress("EXPERIMENTAL_API_USAGE_ERROR")
2120
class KotlinxSerializer(
22-
private val json: Json = Json(DefaultJsonConfiguration)
21+
private val json: Json = DefaultJson
2322
) : JsonSerializer {
2423

2524
override fun write(data: Any, contentType: ContentType): OutgoingContent {
@@ -28,44 +27,58 @@ class KotlinxSerializer(
2827
}
2928

3029
internal fun writeContent(data: Any): String =
31-
json.stringify(buildSerializer(data, json.context) as KSerializer<Any>, data)
30+
json.encodeToString(buildSerializer(data, json.serializersModule), data)
3231

32+
@OptIn(UnsafeSerializationApi::class)
3333
override fun read(type: TypeInfo, body: Input): Any {
3434
val text = body.readText()
35-
val deserializationStrategy = json.context.getContextual(type.type)
35+
val deserializationStrategy = json.serializersModule.getContextual(type.type)
3636
val mapper = deserializationStrategy ?: (type.kotlinType?.let { serializer(it) } ?: type.type.serializer())
37-
return json.parse(mapper, text)!!
37+
return json.decodeFromString(mapper, text)!!
3838
}
3939

4040
companion object {
4141
/**
4242
* Default [Json] configuration for [KotlinxSerializer].
4343
*/
44-
val DefaultJsonConfiguration: JsonConfiguration = JsonConfiguration(
45-
isLenient = false,
46-
ignoreUnknownKeys = false,
47-
serializeSpecialFloatingPointValues = true,
48-
useArrayPolymorphism = false
44+
@Deprecated(
45+
level = DeprecationLevel.ERROR,
46+
message = "DefaultJsonConfiguration is deprecated. Consider using DefaultJson instead."
4947
)
48+
@Suppress("DEPRECATION_ERROR")
49+
val DefaultJsonConfiguration: Json = Json {
50+
isLenient = false
51+
ignoreUnknownKeys = false
52+
allowSpecialFloatingPointValues = true
53+
useArrayPolymorphism = false
54+
}
55+
56+
val DefaultJson: Json = Json {
57+
isLenient = false
58+
ignoreUnknownKeys = false
59+
allowSpecialFloatingPointValues = true
60+
useArrayPolymorphism = false
61+
}
5062
}
5163
}
5264

53-
@Suppress("UNCHECKED_CAST", "EXPERIMENTAL_API_USAGE_ERROR")
54-
private fun buildSerializer(value: Any, module: SerialModule): KSerializer<*> = when (value) {
65+
@Suppress("UNCHECKED_CAST")
66+
@OptIn(UnsafeSerializationApi::class)
67+
private fun buildSerializer(value: Any, module: SerializersModule): KSerializer<Any> = when (value) {
5568
is JsonElement -> JsonElementSerializer
56-
is List<*> -> value.elementSerializer(module).list
57-
is Array<*> -> value.firstOrNull()?.let { buildSerializer(it, module) } ?: String.serializer().list
58-
is Set<*> -> value.elementSerializer(module).set
69+
is List<*> -> ListSerializer(value.elementSerializer(module))
70+
is Array<*> -> value.firstOrNull()?.let { buildSerializer(it, module) } ?: ListSerializer(String.serializer())
71+
is Set<*> -> SetSerializer(value.elementSerializer(module))
5972
is Map<*, *> -> {
60-
val keySerializer = value.keys.elementSerializer(module) as KSerializer<Any>
61-
val valueSerializer = value.values.elementSerializer(module) as KSerializer<Any>
73+
val keySerializer = value.keys.elementSerializer(module)
74+
val valueSerializer = value.values.elementSerializer(module)
6275
MapSerializer(keySerializer, valueSerializer)
6376
}
6477
else -> module.getContextual(value::class) ?: value::class.serializer()
65-
}
78+
} as KSerializer<Any>
6679

6780
@Suppress("EXPERIMENTAL_API_USAGE_ERROR")
68-
private fun Collection<*>.elementSerializer(module: SerialModule): KSerializer<*> {
81+
private fun Collection<*>.elementSerializer(module: SerializersModule): KSerializer<*> {
6982
val serializers: List<KSerializer<*>> =
7083
filterNotNull().map { buildSerializer(it, module) }.distinctBy { it.descriptor.serialName }
7184

ktor-client/ktor-client-features/ktor-client-json/ktor-client-serialization/common/test/ContextualSerializationTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import io.ktor.client.call.*
55
import io.ktor.client.features.json.serializer.*
66
import io.ktor.utils.io.core.*
77
import kotlinx.serialization.*
8+
import kotlinx.serialization.descriptors.*
9+
import kotlinx.serialization.encoding.*
810
import kotlinx.serialization.json.*
911
import kotlinx.serialization.modules.*
10-
import kotlin.reflect.*
11-
import kotlin.reflect.typeOf
1212
import kotlin.test.*
1313

1414

1515
@Serializable
1616
data class UserData(val id: Int, val name: String)
1717

1818
object UserDataSerializer : KSerializer<UserData> {
19-
override val descriptor: SerialDescriptor = SerialDescriptor("UserData")
19+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("UserData")
2020

2121
override fun deserialize(decoder: Decoder): UserData {
2222
val id = decoder.decodeString().toInt(16)
@@ -35,7 +35,7 @@ class ContextualSerializationTest {
3535
@Test
3636
fun testSerializationWithContext() {
3737
val context = serializersModuleOf(UserData::class, UserDataSerializer)
38-
val contextual = KotlinxSerializer(Json(context = context))
38+
val contextual = KotlinxSerializer(Json { serializersModule = context })
3939
val simple = KotlinxSerializer()
4040

4141
val data = UserData(1, "kotlin")
@@ -46,7 +46,7 @@ class ContextualSerializationTest {
4646
assertEquals("\"1\"\"kotlin\"", contextualString)
4747
assertEquals("{\"id\":1,\"name\":\"kotlin\"}", simpleString)
4848

49-
val makeInput = { text: String ->
49+
val makeInput = { text: String ->
5050
buildPacket { writeText(text) }
5151
}
5252

ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/MultiPartFormDataTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import io.ktor.client.request.forms.*
99
import io.ktor.client.statement.*
1010
import io.ktor.client.tests.utils.*
1111
import io.ktor.http.*
12-
import kotlinx.io.*
13-
import kotlinx.serialization.internal.*
1412
import kotlin.test.*
1513

1614
/**

ktor-features/ktor-serialization/jvm/src/io/ktor/serialization/JsonSupport.kt

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,39 @@ import kotlinx.serialization.modules.*
2020
*
2121
* See [JsonConfiguration] for more details.
2222
*/
23-
val DefaultJsonConfiguration: JsonConfiguration = JsonConfiguration.Stable.copy(
24-
encodeDefaults = true,
25-
isLenient = true,
26-
serializeSpecialFloatingPointValues = true,
27-
allowStructuredMapKeys = true,
28-
unquotedPrint = false,
29-
prettyPrint = false,
30-
useArrayPolymorphism = true
23+
@Deprecated(
24+
level = DeprecationLevel.ERROR,
25+
message = "JsonConfiguration is deprecated, consider using DefaultJson instead.",
26+
replaceWith = ReplaceWith("DefaultJson")
3127
)
28+
@Suppress("DEPRECATION_ERROR")
29+
val DefaultJsonConfiguration: Json = Json {
30+
encodeDefaults = true
31+
isLenient = true
32+
allowSpecialFloatingPointValues = true
33+
allowStructuredMapKeys = true
34+
prettyPrint = false
35+
useArrayPolymorphism = true
36+
}
37+
38+
/**
39+
* The default json configuration used in [SerializationConverter]. The settings are:
40+
* - defaults are serialized
41+
* - mode is not strict so extra json fields are ignored
42+
* - pretty printing is disabled
43+
* - array polymorphism is enabled
44+
* - keys and values are quoted, non-quoted are not allowed
45+
*
46+
* See [JsonConfiguration] for more details.
47+
*/
48+
val DefaultJson: Json = Json {
49+
encodeDefaults = true
50+
isLenient = true
51+
allowSpecialFloatingPointValues = true
52+
allowStructuredMapKeys = true
53+
prettyPrint = false
54+
useArrayPolymorphism = true
55+
}
3256

3357
/**
3458
* Register `application/json` (or another specified [contentType]) content type
@@ -38,12 +62,18 @@ val DefaultJsonConfiguration: JsonConfiguration = JsonConfiguration.Stable.copy(
3862
* @param module is used for serialization (optional)
3963
* @param contentType to register with, application/json by default
4064
*/
65+
@Deprecated(
66+
level = DeprecationLevel.ERROR,
67+
message = "JsonConfiguration is deprecated, consider using `Json { serializersModule = module }` instead.",
68+
replaceWith = ReplaceWith("json(Json { serializersModule = module }, contentType)")
69+
)
70+
@Suppress("DEPRECATION_ERROR")
4171
fun ContentNegotiation.Configuration.json(
42-
json: JsonConfiguration = DefaultJsonConfiguration,
43-
module: SerialModule = EmptyModule,
72+
json: Json = Json.Default,
73+
module: SerializersModule = EmptySerializersModule,
4474
contentType: ContentType = ContentType.Application.Json
4575
) {
46-
json(Json(json, module), contentType)
76+
TODO()
4777
}
4878

4979
/**
@@ -54,7 +84,7 @@ fun ContentNegotiation.Configuration.json(
5484
* @param contentType to register with, application/json by default
5585
*/
5686
fun ContentNegotiation.Configuration.json(
57-
json: Json = Json(DefaultJsonConfiguration),
87+
json: Json = DefaultJson,
5888
contentType: ContentType = ContentType.Application.Json
5989
) {
6090
serialization(contentType, json as StringFormat)

0 commit comments

Comments
 (0)