@@ -19,6 +19,16 @@ import kotlinx.serialization.modules.*
19
19
* Typically, formats have their specific [Encoder] and [Decoder] implementations
20
20
* as private classes and do not expose them.
21
21
*
22
+ * ### Exception types for `SerialFormat` implementation
23
+ *
24
+ * Methods responsible for format-specific encoding and decoding are allowed to throw
25
+ * any subtype of [IllegalArgumentException] in order to indicate serialization
26
+ * and deserialization errors. It is recommended to throw subtypes of [SerializationException]
27
+ * for encoder and decoder specific errors and [IllegalArgumentException] for input
28
+ * and output validation-specific errors.
29
+ *
30
+ * For formats
31
+ *
22
32
* ### Not stable for inheritance
23
33
*
24
34
* `SerialFormat` interface is not stable for inheritance in 3rd party libraries, as new methods
@@ -49,11 +59,17 @@ public interface BinaryFormat : SerialFormat {
49
59
50
60
/* *
51
61
* Serializes and encodes the given [value] to byte array using the given [serializer].
62
+ *
63
+ * @throws SerializationException in case of any encoding-specific error
64
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
52
65
*/
53
66
public fun <T > encodeToByteArray (serializer : SerializationStrategy <T >, value : T ): ByteArray
54
67
55
68
/* *
56
- * Decodes and deserializes the given [byte array][bytes] to the value of type [T] using the given [deserializer]
69
+ * Decodes and deserializes the given [byte array][bytes] to the value of type [T] using the given [deserializer].
70
+ *
71
+ * @throws SerializationException in case of any decoding-specific error
72
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
57
73
*/
58
74
public fun <T > decodeFromByteArray (deserializer : DeserializationStrategy <T >, bytes : ByteArray ): T
59
75
}
@@ -72,27 +88,37 @@ public interface StringFormat : SerialFormat {
72
88
73
89
/* *
74
90
* Serializes and encodes the given [value] to string using the given [serializer].
91
+ *
92
+ * @throws SerializationException in case of any encoding-specific error
93
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
75
94
*/
76
95
public fun <T > encodeToString (serializer : SerializationStrategy <T >, value : T ): String
77
96
78
97
/* *
79
- * Decodes and deserializes the given [string] to the value of type [T] using the given [deserializer]
98
+ * Decodes and deserializes the given [string] to the value of type [T] using the given [deserializer].
99
+ *
100
+ * @throws SerializationException in case of any decoding-specific error
101
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
80
102
*/
81
103
public fun <T > decodeFromString (deserializer : DeserializationStrategy <T >, string : String ): T
82
104
}
83
105
84
106
/* *
85
107
* Serializes and encodes the given [value] to string using serializer retrieved from the reified type parameter.
108
+ *
109
+ * @throws SerializationException in case of any encoding-specific error
110
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
86
111
*/
87
- @OptIn(ExperimentalSerializationApi ::class )
88
112
public inline fun <reified T > StringFormat.encodeToString (value : T ): String =
89
113
encodeToString(serializersModule.serializer(), value)
90
114
91
115
/* *
92
116
* Decodes and deserializes the given [string] to the value of type [T] using deserializer
93
117
* retrieved from the reified type parameter.
118
+ *
119
+ * @throws SerializationException in case of any decoding-specific error
120
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
94
121
*/
95
- @OptIn(ExperimentalSerializationApi ::class )
96
122
public inline fun <reified T > StringFormat.decodeFromString (string : String ): T =
97
123
decodeFromString(serializersModule.serializer(), string)
98
124
@@ -104,18 +130,22 @@ public inline fun <reified T> StringFormat.decodeFromString(string: String): T =
104
130
* Hex representation does not interfere with serialization and encoding process of the format and
105
131
* only applies transformation to the resulting array. It is recommended to use for debugging and
106
132
* testing purposes.
133
+ *
134
+ * @throws SerializationException in case of any encoding-specific error
135
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
107
136
*/
108
- @OptIn(ExperimentalSerializationApi ::class )
109
137
public fun <T > BinaryFormat.encodeToHexString (serializer : SerializationStrategy <T >, value : T ): String =
110
138
InternalHexConverter .printHexBinary(encodeToByteArray(serializer, value), lowerCase = true )
111
139
112
140
/* *
113
141
* Decodes byte array from the given [hex] string and the decodes and deserializes it
114
142
* to the value of type [T], delegating it to the [BinaryFormat].
115
143
*
116
- * This method is a counterpart to [encodeToHexString]
144
+ * This method is a counterpart to [encodeToHexString].
145
+ *
146
+ * @throws SerializationException in case of any decoding-specific error
147
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
117
148
*/
118
- @OptIn(ExperimentalSerializationApi ::class )
119
149
public fun <T > BinaryFormat.decodeFromHexString (deserializer : DeserializationStrategy <T >, hex : String ): T =
120
150
decodeFromByteArray(deserializer, InternalHexConverter .parseHexBinary(hex))
121
151
@@ -126,33 +156,41 @@ public fun <T> BinaryFormat.decodeFromHexString(deserializer: DeserializationStr
126
156
* Hex representation does not interfere with serialization and encoding process of the format and
127
157
* only applies transformation to the resulting array. It is recommended to use for debugging and
128
158
* testing purposes.
159
+ *
160
+ * @throws SerializationException in case of any encoding-specific error
161
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
129
162
*/
130
- @OptIn(ExperimentalSerializationApi ::class )
131
163
public inline fun <reified T > BinaryFormat.encodeToHexString (value : T ): String =
132
164
encodeToHexString(serializersModule.serializer(), value)
133
165
134
166
/* *
135
167
* Decodes byte array from the given [hex] string and the decodes and deserializes it
136
168
* to the value of type [T], delegating it to the [BinaryFormat].
137
169
*
138
- * This method is a counterpart to [encodeToHexString]
170
+ * This method is a counterpart to [encodeToHexString].
171
+ *
172
+ * @throws SerializationException in case of any decoding-specific error
173
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
139
174
*/
140
- @OptIn(ExperimentalSerializationApi ::class )
141
175
public inline fun <reified T > BinaryFormat.decodeFromHexString (hex : String ): T =
142
176
decodeFromHexString(serializersModule.serializer(), hex)
143
177
144
178
/* *
145
179
* Serializes and encodes the given [value] to byte array using serializer
146
180
* retrieved from the reified type parameter.
181
+ *
182
+ * @throws SerializationException in case of any encoding-specific error
183
+ * @throws IllegalArgumentException if the encoded input does not comply format's specification
147
184
*/
148
- @OptIn(ExperimentalSerializationApi ::class )
149
185
public inline fun <reified T > BinaryFormat.encodeToByteArray (value : T ): ByteArray =
150
186
encodeToByteArray(serializersModule.serializer(), value)
151
187
152
188
/* *
153
189
* Decodes and deserializes the given [byte array][bytes] to the value of type [T] using deserializer
154
190
* retrieved from the reified type parameter.
191
+ *
192
+ * @throws SerializationException in case of any decoding-specific error
193
+ * @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
155
194
*/
156
- @OptIn(ExperimentalSerializationApi ::class )
157
195
public inline fun <reified T > BinaryFormat.decodeFromByteArray (bytes : ByteArray ): T =
158
196
decodeFromByteArray(serializersModule.serializer(), bytes)
0 commit comments