-
Notifications
You must be signed in to change notification settings - Fork 627
/
SerialFormat.kt
196 lines (178 loc) · 8.6 KB
/
SerialFormat.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlinx.serialization.internal.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.modules.*
/**
* Represents an instance of a serialization format
* that can interact with [KSerializer] and is a supertype of all entry points for a serialization.
* It does not impose any restrictions on a serialized form or underlying storage, neither it exposes them.
*
* Concrete data types and API for user-interaction are responsibility of a concrete subclass or subinterface,
* for example [StringFormat], [BinaryFormat] or `Json`.
*
* Typically, formats have their specific [Encoder] and [Decoder] implementations
* as private classes and do not expose them.
*
* ### Exception types for `SerialFormat` implementation
*
* Methods responsible for format-specific encoding and decoding are allowed to throw
* any subtype of [IllegalArgumentException] in order to indicate serialization
* and deserialization errors. It is recommended to throw subtypes of [SerializationException]
* for encoder and decoder specific errors and [IllegalArgumentException] for input
* and output validation-specific errors.
*
* For formats
*
* ### Not stable for inheritance
*
* `SerialFormat` interface is not stable for inheritance in 3rd party libraries, as new methods
* might be added to this interface or contracts of the existing methods can be changed.
*
* It is safe to operate with instances of `SerialFormat` and call its methods.
*/
public interface SerialFormat {
/**
* Contains all serializers registered by format user for [Contextual] and [Polymorphic] serialization.
*
* The same module should be exposed in the format's [Encoder] and [Decoder].
*/
public val serializersModule: SerializersModule
}
/**
* [SerialFormat] that allows conversions to and from [ByteArray] via [encodeToByteArray] and [decodeFromByteArray] methods.
*
* ### Not stable for inheritance
*
* `BinaryFormat` interface is not stable for inheritance in 3rd party libraries, as new methods
* might be added to this interface or contracts of the existing methods can be changed.
*
* It is safe to operate with instances of `BinaryFormat` and call its methods.
*/
public interface BinaryFormat : SerialFormat {
/**
* Serializes and encodes the given [value] to byte array using the given [serializer].
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray
/**
* Decodes and deserializes the given [byte array][bytes] to the value of type [T] using the given [deserializer].
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public fun <T> decodeFromByteArray(deserializer: DeserializationStrategy<T>, bytes: ByteArray): T
}
/**
* [SerialFormat] that allows conversions to and from [String] via [encodeToString] and [decodeFromString] methods.
*
* ### Not stable for inheritance
*
* `StringFormat` interface is not stable for inheritance in 3rd party libraries, as new methods
* might be added to this interface or contracts of the existing methods can be changed.
*
* It is safe to operate with instances of `StringFormat` and call its methods.
*/
public interface StringFormat : SerialFormat {
/**
* Serializes and encodes the given [value] to string using the given [serializer].
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String
/**
* Decodes and deserializes the given [string] to the value of type [T] using the given [deserializer].
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T
}
/**
* Serializes and encodes the given [value] to string using serializer retrieved from the reified type parameter.
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public inline fun <reified T> StringFormat.encodeToString(value: T): String =
encodeToString(serializersModule.serializer(), value)
/**
* Decodes and deserializes the given [string] to the value of type [T] using deserializer
* retrieved from the reified type parameter.
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public inline fun <reified T> StringFormat.decodeFromString(string: String): T =
decodeFromString(serializersModule.serializer(), string)
/**
* Serializes and encodes the given [value] to byte array, delegating it to the [BinaryFormat],
* and then encodes resulting bytes to hex string.
*
* Hex representation does not interfere with serialization and encoding process of the format and
* only applies transformation to the resulting array. It is recommended to use for debugging and
* testing purposes.
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public fun <T> BinaryFormat.encodeToHexString(serializer: SerializationStrategy<T>, value: T): String =
InternalHexConverter.printHexBinary(encodeToByteArray(serializer, value), lowerCase = true)
/**
* Decodes byte array from the given [hex] string and the decodes and deserializes it
* to the value of type [T], delegating it to the [BinaryFormat].
*
* This method is a counterpart to [encodeToHexString].
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public fun <T> BinaryFormat.decodeFromHexString(deserializer: DeserializationStrategy<T>, hex: String): T =
decodeFromByteArray(deserializer, InternalHexConverter.parseHexBinary(hex))
/**
* Serializes and encodes the given [value] to byte array, delegating it to the [BinaryFormat],
* and then encodes resulting bytes to hex string.
*
* Hex representation does not interfere with serialization and encoding process of the format and
* only applies transformation to the resulting array. It is recommended to use for debugging and
* testing purposes.
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public inline fun <reified T> BinaryFormat.encodeToHexString(value: T): String =
encodeToHexString(serializersModule.serializer(), value)
/**
* Decodes byte array from the given [hex] string and the decodes and deserializes it
* to the value of type [T], delegating it to the [BinaryFormat].
*
* This method is a counterpart to [encodeToHexString].
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public inline fun <reified T> BinaryFormat.decodeFromHexString(hex: String): T =
decodeFromHexString(serializersModule.serializer(), hex)
/**
* Serializes and encodes the given [value] to byte array using serializer
* retrieved from the reified type parameter.
*
* @throws SerializationException in case of any encoding-specific error
* @throws IllegalArgumentException if the encoded input does not comply format's specification
*/
public inline fun <reified T> BinaryFormat.encodeToByteArray(value: T): ByteArray =
encodeToByteArray(serializersModule.serializer(), value)
/**
* Decodes and deserializes the given [byte array][bytes] to the value of type [T] using deserializer
* retrieved from the reified type parameter.
*
* @throws SerializationException in case of any decoding-specific error
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
*/
public inline fun <reified T> BinaryFormat.decodeFromByteArray(bytes: ByteArray): T =
decodeFromByteArray(serializersModule.serializer(), bytes)