-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'serializerSeleadClass'
- Loading branch information
Showing
38 changed files
with
262 additions
and
737 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
6 changes: 3 additions & 3 deletions
6
...ppe/firestore/DefaultSerializersModule.kt → ...rmappe/common/DefaultSerializersModule.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
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
3 changes: 1 addition & 2 deletions
3
...ipgate/federmappe/firestore/MapDecoder.kt → ...e/sipgate/federmappe/common/MapDecoder.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
57 changes: 57 additions & 0 deletions
57
common/src/main/kotlin/de/sipgate/federmappe/common/SealedClassWithTypeSerializer.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,57 @@ | ||
package de.sipgate.federmappe.common | ||
|
||
import kotlin.reflect.KClass | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.PolymorphicKind | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.serializerOrNull | ||
|
||
@ExperimentalSerializationApi | ||
@InternalSerializationApi | ||
abstract class SealedClassWithTypeSerializer<Type : Any, DiscriminatorType>( | ||
private val baseClass: KClass<Type>, | ||
private val discriminatorName: String = "type" | ||
) : KSerializer<Type> { | ||
|
||
override val descriptor: SerialDescriptor = | ||
buildSerialDescriptor( | ||
"JsonContentPolymorphicSerializer<${baseClass.simpleName}>", | ||
PolymorphicKind.SEALED | ||
) | ||
|
||
final override fun serialize(encoder: Encoder, value: Type) { | ||
val actualSerializer = | ||
encoder.serializersModule.getPolymorphic(baseClass, value) | ||
?: value::class.serializerOrNull() | ||
?: throwSubtypeNotRegistered(value::class, baseClass) | ||
@Suppress("UNCHECKED_CAST") | ||
(actualSerializer as KSerializer<Type>).serialize(encoder, value) | ||
} | ||
|
||
final override fun deserialize(decoder: Decoder): Type { | ||
val type = (decoder as? TypeAwareDecoder) | ||
?.decodeType<DiscriminatorType>(typeKey = discriminatorName) | ||
?: throw IllegalStateException("We need to know the type to decode first!") | ||
|
||
val actualSerializer = selectDeserializer(type) as KSerializer<Type> | ||
return decoder.decodeSerializableValue(actualSerializer) | ||
} | ||
|
||
protected abstract fun selectDeserializer(element: DiscriminatorType): DeserializationStrategy<Type> | ||
|
||
private fun throwSubtypeNotRegistered(subClass: KClass<*>, baseClass: KClass<*>): Nothing { | ||
val subClassName = subClass.simpleName ?: "$subClass" | ||
val scope = "in the scope of '${baseClass.simpleName}'" | ||
throw SerializationException( | ||
"Class '${subClassName}' is not registered for polymorphic serialization $scope.\n" + | ||
"Mark the base class as 'sealed' or register the serializer explicitly." | ||
) | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
...gate/federmappe/firestore/StringMapExt.kt → ...sipgate/federmappe/common/StringMapExt.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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package de.sipgate.federmappe.firestore | ||
package de.sipgate.federmappe.common | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.serializer | ||
|
||
@ExperimentalSerializationApi | ||
inline fun <reified T : Any> Map<String, Any>.toObjectWithSerializer( | ||
serializer: KSerializer<T> = serializer<T>(), | ||
customSerializers: SerializersModule | ||
customSerializers: SerializersModule, | ||
noinline subtypeDecoder: (Any?) -> CompositeDecoder? = {null} | ||
): T = serializer.deserialize( | ||
StringMapToObjectDecoder( | ||
this, | ||
ignoreUnknownProperties = true, | ||
serializersModule = customSerializers, | ||
subtypeDecoder = subtypeDecoder | ||
), | ||
) |
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
5 changes: 5 additions & 0 deletions
5
common/src/main/kotlin/de/sipgate/federmappe/common/TypeAwareDecoder.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,5 @@ | ||
package de.sipgate.federmappe.common | ||
|
||
interface TypeAwareDecoder { | ||
fun <T> decodeType(typeKey: String = "type"): T? | ||
} |
2 changes: 1 addition & 1 deletion
2
...e/firestore/serializers/DateSerializer.kt → ...appe/common/serializers/DateSerializer.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
2 changes: 1 addition & 1 deletion
2
common/src/main/kotlin/de/sipgate/federmappe/common/serializers/UriSerializer.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
2 changes: 1 addition & 1 deletion
2
...gate/federmappe/firestore/BooleanTests.kt → ...sipgate/federmappe/common/BooleanTests.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
2 changes: 1 addition & 1 deletion
2
...ate/federmappe/firestore/EnumListTests.kt → ...ipgate/federmappe/common/EnumListTests.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
2 changes: 1 addition & 1 deletion
2
...sipgate/federmappe/firestore/EnumTests.kt → ...de/sipgate/federmappe/common/EnumTests.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
2 changes: 1 addition & 1 deletion
2
...ppe/firestore/FloatingPointNumberTests.kt → ...rmappe/common/FloatingPointNumberTests.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
79 changes: 79 additions & 0 deletions
79
common/src/test/kotlin/de/sipgate/federmappe/common/SealedTypeTests.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,79 @@ | ||
package de.sipgate.federmappe.common | ||
|
||
import de.sipgate.federmappe.common.SealedTypeTests.BaseType | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlinx.serialization.modules.subclass | ||
import kotlinx.serialization.serializer | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertInstanceOf | ||
import org.junit.jupiter.api.Test | ||
|
||
@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class) | ||
internal class CustomSerializer : SealedClassWithTypeSerializer<BaseType, String>(BaseType::class) { | ||
override fun selectDeserializer(element: String): DeserializationStrategy<BaseType> { | ||
return when (element) { | ||
"A" -> BaseType.A.serializer() | ||
"B" -> BaseType.B.serializer() | ||
else -> throw IllegalArgumentException("unknown element $element") | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal class SealedTypeTests { | ||
|
||
@Serializable(with = CustomSerializer::class) | ||
sealed interface BaseType { | ||
val type: String | ||
|
||
@Serializable | ||
data class A(val value: String) : BaseType { | ||
override val type = "A" | ||
} | ||
|
||
@Serializable | ||
data class B(val value: Boolean) : BaseType { | ||
override val type = "B" | ||
} | ||
} | ||
|
||
@Test | ||
fun deserializeBasicDataClassWithBooleanFieldSetToTrue() { | ||
// Arrange | ||
@Serializable | ||
data class TestClass(val a: BaseType) | ||
|
||
val serializer = serializer<TestClass>() | ||
val data = mapOf<String, Any?>( | ||
"a" to mapOf( | ||
"type" to "A", | ||
"value" to "some string" | ||
) | ||
) | ||
|
||
// Act | ||
val result = | ||
serializer.deserialize( | ||
StringMapToObjectDecoder( | ||
data, | ||
ignoreUnknownProperties = true, | ||
serializersModule = SerializersModule { | ||
polymorphic(BaseType::class) { | ||
subclass(BaseType.A::class) | ||
subclass(BaseType.B::class) | ||
} | ||
} | ||
), | ||
) | ||
|
||
// Assert | ||
assertInstanceOf(TestClass::class.java, result) | ||
assertInstanceOf(BaseType.A::class.java, result.a) | ||
assertEquals("some string", (result.a as BaseType.A).value) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...e/federmappe/firestore/StringListTests.kt → ...gate/federmappe/common/StringListTests.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
2 changes: 1 addition & 1 deletion
2
...ermappe/firestore/StringMapDecoderTest.kt → ...federmappe/common/StringMapDecoderTest.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
2 changes: 1 addition & 1 deletion
2
...pgate/federmappe/firestore/StringTests.kt → .../sipgate/federmappe/common/StringTests.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
Oops, something went wrong.