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
Copy file name to clipboardExpand all lines: README.md
+31-4
Original file line number
Diff line number
Diff line change
@@ -85,16 +85,43 @@ data class City(val name: String)
85
85
Instances of these classes can now be passed [along with their serializer](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#introduction-to-serializers) to the SDK:
The `buildSettings` closure is optional and allows for configuring serialization behaviour.
92
92
93
-
Setting the `shouldEncodeElementDefault` parameter is optional and defaults to `true`, set this to false to omit writing optional properties if they are equal to theirs default values.
93
+
Setting the `encodeDefaults` parameter is optional and defaults to `true`, set this to false to omit writing optional properties if they are equal to theirs default values.
94
94
Using [@EncodeDefault](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-encode-default/) on properties is a recommended way to locally override the behavior set with `encodeDefaults`.
95
95
96
-
You can also omit the serializer but this is discouraged due to a [current limitation on Kotlin/JS and Kotlin/Native](https://github.com/Kotlin/kotlinx.serialization/issues/1116#issuecomment-704342452).
97
-
To support [contextual serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#contextual-serialization) or [open polymorphism](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism) the `serializersModule` can be overridden in any `EncodeSettings` or `DecodeSettings`
96
+
You can also omit the serializer if it can be inferred using `serializer<KType>()`.
97
+
To support [contextual serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#contextual-serialization) or [open polymorphism](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism) the `serializersModule` can be overridden in the `buildSettings` closure:
98
+
99
+
```kotlin
100
+
@Serializable
101
+
abstractclassAbstractCity {
102
+
abstractval name:String
103
+
}
104
+
105
+
@Serializable
106
+
@SerialName("capital")
107
+
data classCapital(overridevalname:String, valisSeatOfGovernment:Boolean) : AbstractCity()
val map =mapOf("key" to "value", "key2" to "value2", "key3" to "value3")
73
-
val encoded = encode<Map<String, String>>(map) { shouldEncodeElementDefault=true }
74
+
val encoded = encode<Map<String, String>>(map) { encodeDefaults=true }
74
75
75
76
nativeAssertEquals(nativeMapOf("key" to "value", "key2" to "value2", "key3" to "value3"), encoded)
76
77
@@ -80,7 +81,7 @@ class EncodersTest {
80
81
81
82
@Test
82
83
funencodeDecodeObject() {
83
-
val encoded = encode(TestObject.serializer(), TestObject) { shouldEncodeElementDefault=false }
84
+
val encoded = encode(TestObject.serializer(), TestObject) { encodeDefaults=false }
84
85
nativeAssertEquals(nativeMapOf(), encoded)
85
86
86
87
val decoded = decode(TestObject.serializer(), encoded)
@@ -90,7 +91,7 @@ class EncodersTest {
90
91
@Test
91
92
funencodeDecodeClass() {
92
93
val testDataClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
93
-
val encoded = encode(TestData.serializer(), testDataClass) { shouldEncodeElementDefault=false }
94
+
val encoded = encode(TestData.serializer(), testDataClass) { encodeDefaults=false }
94
95
95
96
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true), encoded)
96
97
@@ -101,7 +102,7 @@ class EncodersTest {
101
102
@Test
102
103
funencodeDecodeClassNullableValue() {
103
104
val testDataClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true, nullableBool =true)
104
-
val encoded = encode(TestData.serializer(), testDataClass) { shouldEncodeElementDefault=true }
105
+
val encoded = encode(TestData.serializer(), testDataClass) { encodeDefaults=true }
105
106
106
107
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true), encoded)
107
108
@@ -113,7 +114,7 @@ class EncodersTest {
113
114
funencodeDecodeGenericClass() {
114
115
val innerClass =TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
115
116
val genericClass =GenericClass(innerClass)
116
-
val encoded = encode(GenericClass.serializer(TestData.serializer()), genericClass) { shouldEncodeElementDefault=true }
117
+
val encoded = encode(GenericClass.serializer(TestData.serializer()), genericClass) { encodeDefaults=true }
117
118
118
119
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null)), encoded)
119
120
@@ -124,7 +125,7 @@ class EncodersTest {
124
125
@Test
125
126
funencodeDecodeSealedClass() {
126
127
val sealedClass =SealedClass.Test("value")
127
-
val encoded = encode(SealedClass.serializer(), sealedClass) { shouldEncodeElementDefault=true }
128
+
val encoded = encode(SealedClass.serializer(), sealedClass) { encodeDefaults=true }
128
129
129
130
nativeAssertEquals(nativeMapOf("type" to "test", "value" to "value"), encoded)
val sealedClass:SealedClass=SealedClass.Test("value")
162
167
val abstractClass:AbstractClass=ImplementedClass("value", true)
163
168
val nestedClass =NestedClass(sealedClass, abstractClass, listOf(sealedClass), listOf(abstractClass), mapOf(sealedClass to sealedClass), mapOf(abstractClass to abstractClass))
164
169
val encoded = encode(NestedClass.serializer(), nestedClass) {
165
-
shouldEncodeElementDefault=true
170
+
encodeDefaults=true
166
171
serializersModule = module
167
172
}
168
173
@@ -207,7 +212,7 @@ class EncodersTest {
207
212
208
213
@Test
209
214
funreencodeTransformationObject() {
210
-
val reencoded = reencodeTransformation<TestObject>(nativeMapOf(), { shouldEncodeElementDefault=false }) {
215
+
val reencoded = reencodeTransformation<TestObject>(nativeMapOf(), { encodeDefaults=false }) {
211
216
assertEquals(TestObject, it)
212
217
it
213
218
}
@@ -218,7 +223,7 @@ class EncodersTest {
218
223
funreencodeTransformationClass() {
219
224
val reencoded = reencodeTransformation<TestData>(
220
225
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
221
-
{ shouldEncodeElementDefault=false }
226
+
{ encodeDefaults=false }
222
227
) {
223
228
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =true), it)
224
229
it.copy(map =mapOf("newKey" to "newValue"), nullableBool =null)
@@ -231,7 +236,7 @@ class EncodersTest {
231
236
funreencodeTransformationNullableValue() {
232
237
val reencoded = reencodeTransformation<TestData?>(
233
238
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
234
-
{ shouldEncodeElementDefault=false }
239
+
{ encodeDefaults=false }
235
240
) {
236
241
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =true), it)
237
242
null
@@ -245,7 +250,7 @@ class EncodersTest {
245
250
val reencoded = reencodeTransformation(
246
251
GenericClass.serializer(TestData.serializer()),
247
252
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 }
253
+
{ encodeDefaults=false }
249
254
) {
250
255
assertEquals(
251
256
GenericClass(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool =true, nullableBool =false)),
val sealedClass:SealedClass=SealedClass.Test("value")
297
306
val abstractClass:AbstractClass=ImplementedClass("value", true)
298
307
val nestedClass =NestedClass(sealedClass, abstractClass, listOf(sealedClass), listOf(abstractClass), mapOf(sealedClass to sealedClass), mapOf(abstractClass to abstractClass))
299
308
val encoded = encode(NestedClass.serializer(), nestedClass) {
300
-
shouldEncodeElementDefault=true
309
+
encodeDefaults=true
301
310
serializersModule = module
302
311
}
303
312
304
313
val reencoded = reencodeTransformation(NestedClass.serializer(), encoded, builder = {
0 commit comments