Merge pull request #44 from InsanusMokrassar/0.4.23

0.4.23
This commit is contained in:
InsanusMokrassar 2021-01-28 15:45:24 +06:00 committed by GitHub
commit a299a5b505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 9 deletions

View File

@ -1,5 +1,13 @@
# Changelog
## 0.4.23
* `Versions`:
* `Ktor`: `1.5.0` -> `1.5.1`
* `Serialization`
* `Base64`
* New serializer `Base64BytesToFromStringSerializer` has been added
## 0.4.22
* `Versions`:

View File

@ -11,7 +11,7 @@ kotlin_coroutines_version=1.4.2
kotlin_serialisation_core_version=1.0.1
kotlin_exposed_version=0.29.1
ktor_version=1.5.0
ktor_version=1.5.1
klockVersion=2.0.4
@ -44,5 +44,5 @@ dokka_version=1.4.20
# Project data
group=dev.inmo
version=0.4.22
android_code_version=26
version=0.4.23
android_code_version=27

View File

@ -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())
}

View File

@ -1,9 +1,6 @@
package dev.inmo.micro_utils.serialization.base64
import dev.inmo.micro_utils.crypto.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@ -12,9 +9,9 @@ open class Base64Serializer<T>(
private val converterFrom: (T) -> String,
private val converterTo: (String) -> T,
) : KSerializer<T> {
override val descriptor: SerialDescriptor = String.serializer().descriptor
override fun deserialize(decoder: Decoder): T = converterTo(decoder.decodeString().decodeBase64String())
override fun serialize(encoder: Encoder, value: T) = encoder.encodeString(converterFrom(value).encodeBase64String())
override val descriptor: SerialDescriptor = Base64BytesToFromStringSerializer.descriptor
override fun deserialize(decoder: Decoder): T = converterTo(Base64BytesToFromStringSerializer.deserialize(decoder).decodeToString())
override fun serialize(encoder: Encoder, value: T) = Base64BytesToFromStringSerializer.serialize(encoder, converterFrom(value).encodeToByteArray())
}
object Base64StringSerializer : Base64Serializer<String>({ it }, { it })