mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
add Base64BytesToFromStringSerializer
This commit is contained in:
parent
2d8ad47083
commit
94e26ee8a0
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## 0.4.23
|
## 0.4.23
|
||||||
|
|
||||||
|
* `Serialization`
|
||||||
|
* `Base64`
|
||||||
|
* New serializer `Base64BytesToFromStringSerializer` has been added
|
||||||
|
|
||||||
## 0.4.22
|
## 0.4.22
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package dev.inmo.micro_utils.serialization.base64
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.crypto.*
|
||||||
|
import kotlinx.serialization.KSerializer
|
||||||
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
|
import kotlinx.serialization.encoding.Decoder
|
||||||
|
import kotlinx.serialization.encoding.Encoder
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts [ByteArray] into base64 encoded string due to serialization and decode base64 encoded string into bytes
|
||||||
|
*/
|
||||||
|
object Base64BytesToFromStringSerializer: KSerializer<ByteArray> {
|
||||||
|
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||||
|
override fun deserialize(decoder: Decoder): SourceBytes = decoder.decodeString().decodeBase64()
|
||||||
|
override fun serialize(encoder: Encoder, value: SourceBytes) = encoder.encodeString(value.encodeBase64String())
|
||||||
|
}
|
@ -1,9 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.serialization.base64
|
package dev.inmo.micro_utils.serialization.base64
|
||||||
|
|
||||||
import dev.inmo.micro_utils.crypto.*
|
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.Serializer
|
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
import kotlinx.serialization.encoding.Decoder
|
import kotlinx.serialization.encoding.Decoder
|
||||||
import kotlinx.serialization.encoding.Encoder
|
import kotlinx.serialization.encoding.Encoder
|
||||||
@ -12,9 +9,9 @@ open class Base64Serializer<T>(
|
|||||||
private val converterFrom: (T) -> String,
|
private val converterFrom: (T) -> String,
|
||||||
private val converterTo: (String) -> T,
|
private val converterTo: (String) -> T,
|
||||||
) : KSerializer<T> {
|
) : KSerializer<T> {
|
||||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
override val descriptor: SerialDescriptor = Base64BytesToFromStringSerializer.descriptor
|
||||||
override fun deserialize(decoder: Decoder): T = converterTo(decoder.decodeString().decodeBase64String())
|
override fun deserialize(decoder: Decoder): T = converterTo(Base64BytesToFromStringSerializer.deserialize(decoder).decodeToString())
|
||||||
override fun serialize(encoder: Encoder, value: T) = encoder.encodeString(converterFrom(value).encodeBase64String())
|
override fun serialize(encoder: Encoder, value: T) = Base64BytesToFromStringSerializer.serialize(encoder, converterFrom(value).encodeToByteArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
object Base64StringSerializer : Base64Serializer<String>({ it }, { it })
|
object Base64StringSerializer : Base64Serializer<String>({ it }, { it })
|
||||||
|
Loading…
Reference in New Issue
Block a user