-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathexample-json-17.kt
57 lines (48 loc) · 1.66 KB
/
example-json-17.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// This file was automatically generated from json.md by Knit tool. Do not edit.
package example.exampleJson17
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.descriptors.*
import kotlin.io.encoding.*
@OptIn(ExperimentalEncodingApi::class)
object ByteArrayAsBase64Serializer : KSerializer<ByteArray> {
private val base64 = Base64.Default
override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor(
"ByteArrayAsBase64Serializer",
PrimitiveKind.STRING
)
override fun serialize(encoder: Encoder, value: ByteArray) {
val base64Encoded = base64.encode(value)
encoder.encodeString(base64Encoded)
}
override fun deserialize(decoder: Decoder): ByteArray {
val base64Decoded = decoder.decodeString()
return base64.decode(base64Decoded)
}
}
@Serializable
data class Value(
@Serializable(with = ByteArrayAsBase64Serializer::class)
val base64Input: ByteArray
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Value
return base64Input.contentEquals(other.base64Input)
}
override fun hashCode(): Int {
return base64Input.contentHashCode()
}
}
fun main() {
val string = "foo string"
val value = Value(string.toByteArray())
val encoded = Json.encodeToString(value)
println(encoded)
val decoded = Json.decodeFromString<Value>(encoded)
println(decoded.base64Input.decodeToString())
}