You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
val map =mapOf("key" to "value", "key2" to "value2", "key3" to "value3")
76
-
val encoded = encode(map, shouldEncodeElementDefault =true)
73
+
val encoded = encode<Map<String, String>>(map) { shouldEncodeElementDefault =true }
77
74
78
75
nativeAssertEquals(nativeMapOf("key" to "value", "key2" to "value2", "key3" to "value3"), encoded)
79
76
@@ -83,7 +80,7 @@ class EncodersTest {
83
80
84
81
@Test
85
82
funencodeDecodeObject() {
86
-
val encoded = encode(TestObject.serializer(), TestObject, shouldEncodeElementDefault =false)
83
+
val encoded = encode(TestObject.serializer(), TestObject) { shouldEncodeElementDefault =false }
87
84
nativeAssertEquals(nativeMapOf(), encoded)
88
85
89
86
val decoded = decode(TestObject.serializer(), encoded)
@@ -93,7 +90,7 @@ class EncodersTest {
93
90
@Test
94
91
funencodeDecodeClass() {
95
92
val testDataClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
96
-
val encoded = encode(TestData.serializer(), testDataClass, shouldEncodeElementDefault =false)
93
+
val encoded = encode(TestData.serializer(), testDataClass) { shouldEncodeElementDefault =false }
97
94
98
95
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true), encoded)
99
96
@@ -104,7 +101,7 @@ class EncodersTest {
104
101
@Test
105
102
funencodeDecodeClassNullableValue() {
106
103
val testDataClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true, nullableBool =true)
107
-
val encoded = encode(TestData.serializer(), testDataClass, shouldEncodeElementDefault =true)
104
+
val encoded = encode(TestData.serializer(), testDataClass) { shouldEncodeElementDefault =true }
108
105
109
106
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true), encoded)
110
107
@@ -116,7 +113,7 @@ class EncodersTest {
116
113
funencodeDecodeGenericClass() {
117
114
val innerClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
118
115
val genericClass =GenericClass(innerClass)
119
-
val encoded = encode(GenericClass.serializer(TestData.serializer()), genericClass, shouldEncodeElementDefault =true)
116
+
val encoded = encode(GenericClass.serializer(TestData.serializer()), genericClass) { shouldEncodeElementDefault =true }
120
117
121
118
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null)), encoded)
122
119
@@ -188,4 +185,142 @@ class EncodersTest {
188
185
}
189
186
assertEquals(nestedClass, decoded)
190
187
}
188
+
189
+
@Test
190
+
funreencodeTransformationList() {
191
+
val reencoded = reencodeTransformation<List<String>>(nativeListOf("One", "Two", "Three")) {
val reencoded = reencodeTransformation<Map<String, String>>(nativeMapOf("key" to "value", "key2" to "value2", "key3" to "value3")) {
201
+
assertEquals(mapOf("key" to "value", "key2" to "value2", "key3" to "value3"), it)
202
+
it.mapValues { (_, value) ->"new-$value" }
203
+
}
204
+
205
+
nativeAssertEquals(nativeMapOf("key" to "new-value", "key2" to "new-value2", "key3" to "new-value3"), reencoded)
206
+
}
207
+
208
+
@Test
209
+
funreencodeTransformationObject() {
210
+
val reencoded = reencodeTransformation<TestObject>(nativeMapOf(), { shouldEncodeElementDefault =false }) {
211
+
assertEquals(TestObject, it)
212
+
it
213
+
}
214
+
nativeAssertEquals(nativeMapOf(), reencoded)
215
+
}
216
+
217
+
@Test
218
+
funreencodeTransformationClass() {
219
+
val reencoded = reencodeTransformation<TestData>(
220
+
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
221
+
{ shouldEncodeElementDefault =false }
222
+
) {
223
+
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =true), it)
224
+
it.copy(map =mapOf("newKey" to "newValue"), nullableBool =null)
225
+
}
226
+
227
+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true), reencoded)
228
+
}
229
+
230
+
@Test
231
+
funreencodeTransformationNullableValue() {
232
+
val reencoded = reencodeTransformation<TestData?>(
233
+
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
234
+
{ shouldEncodeElementDefault =false }
235
+
) {
236
+
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =true), it)
237
+
null
238
+
}
239
+
240
+
nativeAssertEquals(null, reencoded)
241
+
}
242
+
243
+
@Test
244
+
funreencodeTransformationGenericClass() {
245
+
val reencoded = reencodeTransformation(
246
+
GenericClass.serializer(TestData.serializer()),
247
+
nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to false)),
248
+
{ shouldEncodeElementDefault =false }
249
+
) {
250
+
assertEquals(
251
+
GenericClass(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =false)),
252
+
it
253
+
)
254
+
GenericClass(it.inner.copy(map =mapOf("newKey" to "newValue"), nullableBool =null))
255
+
}
256
+
257
+
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true)), reencoded)
258
+
}
259
+
260
+
@Test
261
+
funreencodeTransformationSealedClass() {
262
+
val reencoded = reencodeTransformation(SealedClass.serializer(), nativeMapOf("type" to "test", "value" to "value")) {
263
+
assertEquals(SealedClass.Test("value"), it)
264
+
SealedClass.Test("newTest")
265
+
}
266
+
267
+
nativeAssertEquals(nativeMapOf("type" to "test", "value" to "newTest"), reencoded)
val sealedClass:SealedClass=SealedClass.Test("value")
297
+
val abstractClass:AbstractClass=ImplementedClass("value", true)
298
+
val nestedClass =NestedClass(sealedClass, abstractClass, listOf(sealedClass), listOf(abstractClass), mapOf(sealedClass to sealedClass), mapOf(abstractClass to abstractClass))
299
+
val encoded = encode(NestedClass.serializer(), nestedClass) {
300
+
shouldEncodeElementDefault =true
301
+
serializersModule = module
302
+
}
303
+
304
+
val reencoded = reencodeTransformation(NestedClass.serializer(), encoded, builder = {
305
+
shouldEncodeElementDefault =true
306
+
serializersModule = module
307
+
}) {
308
+
assertEquals(nestedClass, it)
309
+
it.copy(sealed =SealedClass.Test("newValue"))
310
+
}
311
+
312
+
val sealedEncoded = nativeMapOf("type" to "test", "value" to "value")
313
+
val abstractEncoded = nativeMapOf("type" to "implemented", "value" to "value", "otherValue" to true)
314
+
nativeAssertEquals(
315
+
nativeMapOf(
316
+
"sealed" to nativeMapOf("type" to "test", "value" to "newValue"),
317
+
"abstract" to abstractEncoded,
318
+
"sealedList" to nativeListOf(sealedEncoded),
319
+
"abstractList" to nativeListOf(abstractEncoded),
320
+
"sealedMap" to nativeMapOf(sealedEncoded to sealedEncoded),
321
+
"abstractMap" to nativeMapOf(abstractEncoded to abstractEncoded)
0 commit comments