diff options
author | Leonid Startsev <sandwwraith@gmail.com> | 2018-11-19 15:15:02 +0300 |
---|---|---|
committer | Leonid Startsev <sandwwraith@gmail.com> | 2018-11-19 15:15:02 +0300 |
commit | 5c2bdde588accb59e7173690169f1e66a30bb859 (patch) | |
tree | 54c351fc97a442507babab324a2d7cd3a1c434c2 /docs/custom_serializers.md | |
parent | 07de3db16c54c8b7adcc8f9715ac6b85ace510b3 (diff) | |
parent | bfd10620233cbd5ea0fff073af40b975cffa6063 (diff) | |
download | kotlinx.serialization-5c2bdde588accb59e7173690169f1e66a30bb859.tar.gz |
Merge branch 'dev'
Diffstat (limited to 'docs/custom_serializers.md')
-rw-r--r-- | docs/custom_serializers.md | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/docs/custom_serializers.md b/docs/custom_serializers.md index fc1c9b08..ae150303 100644 --- a/docs/custom_serializers.md +++ b/docs/custom_serializers.md @@ -41,7 +41,7 @@ import kotlinx.serialization.internal.* @Serializable class MyData(val s: String) { @Serializer(forClass = MyData::class) - companion object /*: KSerializer<MyData> can be omitted or not, if you like*/{ + companion object : KSerializer<MyData> { override val descriptor: SerialDescriptor = StringDescriptor override fun serialize(output: Encoder, obj: MyData) { @@ -65,7 +65,7 @@ First, we need to correctly fill-in descriptor so all formats would know about m @Serializable class BinaryPayload(val req: ByteArray, val res: ByteArray) { @Serializer(forClass = BinaryPayload::class) - companion object { + companion object : KSerializer<MyData> { override val descriptor: SerialDescriptor = object : SerialClassDescImpl("BinaryPayload") { init { addElement("req") // req will have index 0 @@ -87,7 +87,8 @@ override fun serialize(output: Encoder, obj: BinaryPayload) { } ``` -Deserializing a class with multiple values is a complex task, mainly because you don't know the order of fields in the input stream in advance. So crucial part here is make a `when` over index of an incoming element: +Deserializing a class with multiple values is a complex task, mainly because you don't know the order of fields in the input stream in advance. +So crucial part here is to make a `when` over an index of an incoming element: ```kotlin override fun deserialize(input: Decoder): BinaryPayload { @@ -170,6 +171,9 @@ If you're familiar with DI concepts, think of it as _constructor injection_ of s Note that we haven't applied `@Serializable` on the class, because we can't customize it via companion object since companion object can't have constructor arguments. +*Current limitation*: Because primary constructor in such case is generated by the compiler itself, not the plugin, +you have to override `descriptor` manually since it can't be initialized in non-synthetic constructor. + See full sample [here](https://github.com/kotlin/kotlinx.serialization/blob/a4c41392bb735a36788db1d789ec60afdbad3ca8/runtime/jvm/src/test/kotlin/kotlinx/serialization/features/GenericCustomSerializerTest.kt). *Note*: is is broken on JS for now ([#244](https://github.com/Kotlin/kotlinx.serialization/issues/244)). ## Using custom serializers |