Skip to content

Commit

Permalink
fixes #225 Upgrade kotlinx.serialization to 1.0.0-RC
Browse files Browse the repository at this point in the history
  • Loading branch information
zigzago committed Aug 19, 2020
1 parent 8b3c919 commit b2e8e8b
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 44 deletions.
4 changes: 2 additions & 2 deletions kmongo-benchmark/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-benchmark</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>kmongo-benchmark-common</artifactId>
Expand All @@ -39,7 +39,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-runtime</artifactId>
<artifactId>kotlinx-serialization-core</artifactId>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion kmongo-benchmark/jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-benchmark</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>kmongo-benchmark-jackson</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion kmongo-benchmark/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-benchmark</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>kmongo-benchmark-native</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion kmongo-benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-root</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>kmongo-benchmark</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion kmongo-benchmark/serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-benchmark</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.1.2-SNAPSHOT</version>
</parent>

<artifactId>kmongo-benchmark-serialization</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion kmongo-id-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-runtime</artifactId>
<artifactId>kotlinx-serialization-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
2 changes: 1 addition & 1 deletion kmongo-serialization-mapping/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-runtime</artifactId>
<artifactId>kotlinx-serialization-core</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import com.github.jershell.kbson.ByteArraySerializer
import com.github.jershell.kbson.Configuration
import com.github.jershell.kbson.DateSerializer
import com.github.jershell.kbson.ObjectIdSerializer
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PolymorphicSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.UnsafeSerializationApi
import kotlinx.serialization.builtins.ArraySerializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.TripleSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.JsonNullSerializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.serializer
import org.bson.BsonTimestamp
Expand Down Expand Up @@ -65,7 +65,7 @@ private val customModules = CopyOnWriteArraySet<SerializersModule>()
internal var checkBaseModule: Boolean = true

/**
* Add a custom [SerialModule] to KMongo kotlinx.serialization mapping.
* Add a custom [SerializersModule] to KMongo kotlinx.serialization mapping.
*/
fun registerModule(module: SerializersModule) {
customModules.add(module)
Expand All @@ -87,7 +87,7 @@ inline fun <reified T> registerSerializer(serializer: KSerializer<T>) {
var configuration: Configuration = Configuration()

/**
* The KMongo serialization module.
* The KMongo [SerializersModule].
*/
val kmongoSerializationModule: SerializersModule get() = KMongoSerializationRepository.module

Expand Down Expand Up @@ -120,7 +120,8 @@ internal object KMongoSerializationRepository {
Regex::class to RegexSerializer
)

@UnsafeSerializationApi
@ExperimentalSerializationApi
@InternalSerializationApi
private fun <T : Any> getBaseSerializer(obj: T, kClass: KClass<T> = obj.javaClass.kotlin): KSerializer<*>? {
@Suppress("UNCHECKED_CAST")
return when (obj) {
Expand All @@ -144,32 +145,35 @@ internal object KMongoSerializationRepository {
}
}

@ExperimentalSerializationApi
@InternalSerializationApi
@Suppress("UNCHECKED_CAST")
@UnsafeSerializationApi
fun <T : Any> getSerializer(kClass: KClass<T>, obj: T?): KSerializer<T> =
if (obj == null) {
JsonNullSerializer as? KSerializer<T> ?: error("no serializer for null")
error("no serializer for null")
} else {
(serializersMap[kClass]
?: getBaseSerializer(obj, kClass)
?: kClass.serializer()) as? KSerializer<T>
?: error("no serializer for $obj of class $kClass")
}

@ExperimentalSerializationApi
@InternalSerializationApi
@Suppress("UNCHECKED_CAST")
@UnsafeSerializationApi
private fun <T : Any> getSerializer(obj: T?): KSerializer<T> =
if (obj == null) {
JsonNullSerializer as? KSerializer<T> ?: error("no serializer for null")
error("no serializer for null")
} else {
(serializersMap[obj.javaClass.kotlin]
?: getBaseSerializer(obj)
?: obj.javaClass.kotlin.serializer()) as? KSerializer<T>
?: error("no serializer for $obj of class ${obj.javaClass.kotlin}")
}

@ExperimentalSerializationApi
@InternalSerializationApi
@Suppress("UNCHECKED_CAST")
@UnsafeSerializationApi
fun <T : Any> getSerializer(kClass: KClass<T>): KSerializer<T> =
(serializersMap[kClass]
?: module.getContextual(kClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package org.litote.kmongo.serialization
import com.github.jershell.kbson.BsonEncoder
import com.github.jershell.kbson.BsonFlexibleDecoder
import com.github.jershell.kbson.Configuration
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.UnsafeSerializationApi
import org.bson.AbstractBsonReader
import org.bson.BsonReader
import org.bson.BsonValue
Expand All @@ -47,20 +48,23 @@ internal class SerializationCodec<T : Any>(
ClassMappingType.findIdProperty(clazz) as KProperty1<T, *>?
}

@UnsafeSerializationApi
@ExperimentalSerializationApi
@InternalSerializationApi
private val decoderSerializer: KSerializer<T> by lazy(LazyThreadSafetyMode.PUBLICATION) {
KMongoSerializationRepository.getSerializer(clazz)
}

override fun getEncoderClass(): Class<T> = clazz.java

@UnsafeSerializationApi
@ExperimentalSerializationApi
@InternalSerializationApi
override fun encode(writer: BsonWriter, value: T, encoderContext: EncoderContext) {
val serializer = KMongoSerializationRepository.getSerializer(clazz, value)
BsonEncoder(writer, module, configuration).encodeSerializableValue(serializer, value)
}

@UnsafeSerializationApi
@ExperimentalSerializationApi
@InternalSerializationApi
override fun decode(reader: BsonReader, decoderContext: DecoderContext): T {
return BsonFlexibleDecoder(reader as AbstractBsonReader, module, configuration).decodeSerializableValue(
decoderSerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

package org.litote.kmongo.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.UnsafeSerializationApi
import kotlinx.serialization.UseContextualSerialization
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
Expand Down Expand Up @@ -60,10 +61,12 @@ class ExternalSerializerTest {
val updatedAt: LocalDateTime
)

@ExperimentalSerializationApi
@Serializer(forClass = User::class)
object UserSerializer {}
object UserSerializer


@ExperimentalSerializationApi
@Serializer(forClass = Role::class)
object RoleSerializer : KSerializer<Role> {
override val descriptor = PrimitiveSerialDescriptor("RoleStringSerializer", PrimitiveKind.STRING)
Expand All @@ -81,6 +84,7 @@ class ExternalSerializerTest {
}
}

@ExperimentalSerializationApi
@Serializer(forClass = Role.ADMIN::class)
object AdminRoleSerializer : KSerializer<Role.ADMIN> {
override val descriptor = PrimitiveSerialDescriptor("AdminRoleStringSerializer", PrimitiveKind.STRING)
Expand All @@ -92,6 +96,7 @@ class ExternalSerializerTest {
encoder.encodeString(value.name)
}

@ExperimentalSerializationApi
@Serializer(forClass = Role.USER::class)
object UserRoleSerializer : KSerializer<Role.USER> {
override val descriptor = PrimitiveSerialDescriptor("UserRoleStringSerializer", PrimitiveKind.STRING)
Expand All @@ -103,6 +108,7 @@ class ExternalSerializerTest {
encoder.encodeString(value.name)
}

@ExperimentalSerializationApi
@Serializer(forClass = Role.NONE::class)
object NoneRoleSerializer : KSerializer<Role.NONE> {
override val descriptor = PrimitiveSerialDescriptor("NoneRoleStringSerializer", PrimitiveKind.STRING)
Expand All @@ -114,7 +120,8 @@ class ExternalSerializerTest {
encoder.encodeString(value.name)
}

@UnsafeSerializationApi
@InternalSerializationApi
@ExperimentalSerializationApi
@Test
fun `encode and decode User`() {
registerSerializer(UserSerializer)
Expand Down
Loading

0 comments on commit b2e8e8b

Please sign in to comment.