@@ -11,6 +11,7 @@ stable, these are currently experimental features of Kotlin serialization.
11
11
<!-- - TOC -->
12
12
13
13
* [ CBOR (experimental)] ( #cbor-experimental )
14
+ * [ Ignoring unknown keys] ( #ignoring-unknown-keys )
14
15
* [ Byte arrays and CBOR data types] ( #byte-arrays-and-cbor-data-types )
15
16
* [ ProtoBuf (experimental)] ( #protobuf-experimental )
16
17
* [ Field numbers] ( #field-numbers )
@@ -94,6 +95,54 @@ BF # map(*)
94
95
> (see the [ Allowing structured map keys] ( json.md#allowing-structured-map-keys ) section for JSON workarounds),
95
96
> and Kotlin maps are serialized as CBOR maps, but some parsers (like ` jackson-dataformat-cbor ` ) don't support this.
96
97
98
+ ### Ignoring unknown keys
99
+
100
+ CBOR format is often used to communicate with [ IoT] devices where new properties could be added as a part of a device's
101
+ API evolution. By default, unknown keys encountered during deserialization produce an error.
102
+ This behavior can be configured with the [ ignoreUnknownKeys] [ CborBuilder.ignoreUnknownKeys ] property.
103
+
104
+ <!-- - INCLUDE
105
+ import kotlinx.serialization.*
106
+ import kotlinx.serialization.cbor.*
107
+ -->
108
+
109
+ ``` kotlin
110
+ val format = Cbor { ignoreUnknownKeys = true }
111
+
112
+ @Serializable
113
+ data class Project (val name : String )
114
+
115
+ fun main () {
116
+ val data = format.decodeFromHexString<Project >(
117
+ " bf646e616d65756b6f746c696e782e73657269616c697a6174696f6e686c616e6775616765664b6f746c696eff"
118
+ )
119
+ println (data)
120
+ }
121
+ ```
122
+
123
+ > You can get the full code [ here] ( ../guide/example/example-formats-02.kt ) .
124
+
125
+ It decodes the object, despite the fact that ` Project ` is missing the ` language ` property.
126
+
127
+ ``` text
128
+ Project(name=kotlinx.serialization)
129
+ ```
130
+
131
+ <!-- - TEST -->
132
+
133
+ In [ CBOR hex notation] ( http://cbor.me/ ) , the input is equivalent to the following:
134
+ ```
135
+ BF # map(*)
136
+ 64 # text(4)
137
+ 6E616D65 # "name"
138
+ 75 # text(21)
139
+ 6B6F746C696E782E73657269616C697A6174696F6E # "kotlinx.serialization"
140
+ 68 # text(8)
141
+ 6C616E6775616765 # "language"
142
+ 66 # text(6)
143
+ 4B6F746C696E # "Kotlin"
144
+ FF # primitive(*)
145
+ ```
97
146
98
147
### Byte arrays and CBOR data types
99
148
@@ -138,7 +187,7 @@ fun main() {
138
187
}
139
188
```
140
189
141
- > You can get the full code [ here] ( ../guide/example/example-formats-02 .kt ) .
190
+ > You can get the full code [ here] ( ../guide/example/example-formats-03 .kt ) .
142
191
143
192
As we see, the CBOR byte that precedes the data is different for different type of encoding.
144
193
@@ -203,7 +252,7 @@ fun main() {
203
252
}
204
253
```
205
254
206
- > You can get the full code [ here] ( ../guide/example/example-formats-03 .kt ) .
255
+ > You can get the full code [ here] ( ../guide/example/example-formats-04 .kt ) .
207
256
208
257
``` text
209
258
{0A}{15}kotlinx.serialization{12}{06}Kotlin
@@ -253,7 +302,7 @@ fun main() {
253
302
}
254
303
```
255
304
256
- > You can get the full code [ here] ( ../guide/example/example-formats-04 .kt ) .
305
+ > You can get the full code [ here] ( ../guide/example/example-formats-05 .kt ) .
257
306
258
307
We see in the output that the number for the first property ` name ` did not change (as it is numbered from one by default),
259
308
but it did change for the ` language ` property.
@@ -304,7 +353,7 @@ fun main() {
304
353
}
305
354
```
306
355
307
- > You can get the full code [ here] ( ../guide/example/example-formats-05 .kt ) .
356
+ > You can get the full code [ here] ( ../guide/example/example-formats-06 .kt ) .
308
357
309
358
* The [ default] [ ProtoIntegerType.DEFAULT ] is a varint encoding (` intXX ` ) that is optimized for
310
359
small non-negative numbers. The value of ` 1 ` is encoded in one byte ` 01 ` .
@@ -361,7 +410,7 @@ fun main() {
361
410
}
362
411
```
363
412
364
- > You can get the full code [ here] ( ../guide/example/example-formats-06 .kt ) .
413
+ > You can get the full code [ here] ( ../guide/example/example-formats-07 .kt ) .
365
414
366
415
``` text
367
416
{08}{01}{08}{02}{08}{03}
@@ -407,7 +456,7 @@ fun main() {
407
456
}
408
457
```
409
458
410
- > You can get the full code [ here] ( ../guide/example/example-formats-07 .kt ) .
459
+ > You can get the full code [ here] ( ../guide/example/example-formats-08 .kt ) .
411
460
412
461
The resulting map has dot-separated keys representing keys of the nested objects.
413
462
@@ -487,7 +536,7 @@ fun main() {
487
536
}
488
537
```
489
538
490
- > You can get the full code [ here] ( ../guide/example/example-formats-08 .kt ) .
539
+ > You can get the full code [ here] ( ../guide/example/example-formats-09 .kt ) .
491
540
492
541
As a result, we got all the primitives values in our object graph visited and put into a list
493
542
in a _ serial_ order.
@@ -589,7 +638,7 @@ fun main() {
589
638
}
590
639
```
591
640
592
- > You can get the full code [ here] ( ../guide/example/example-formats-09 .kt ) .
641
+ > You can get the full code [ here] ( ../guide/example/example-formats-10 .kt ) .
593
642
594
643
Now can convert a list of primitives back to an object tree.
595
644
@@ -680,7 +729,7 @@ fun main() {
680
729
}
681
730
-->
682
731
683
- > You can get the full code [ here] ( ../guide/example/example-formats-10 .kt ) .
732
+ > You can get the full code [ here] ( ../guide/example/example-formats-11 .kt ) .
684
733
685
734
<!-- - TEST
686
735
[kotlinx.serialization, kotlin, 9000]
@@ -787,7 +836,7 @@ fun main() {
787
836
}
788
837
```
789
838
790
- > You can get the full code [ here] ( ../guide/example/example-formats-11 .kt ) .
839
+ > You can get the full code [ here] ( ../guide/example/example-formats-12 .kt ) .
791
840
792
841
We see the size of the list added to the result, letting decoder know where to stop.
793
842
@@ -899,7 +948,7 @@ fun main() {
899
948
900
949
```
901
950
902
- > You can get the full code [ here] ( ../guide/example/example-formats-12 .kt ) .
951
+ > You can get the full code [ here] ( ../guide/example/example-formats-13 .kt ) .
903
952
904
953
In the output we see how not-null` !! ` and ` NULL ` marks are used.
905
954
@@ -1027,7 +1076,7 @@ fun main() {
1027
1076
}
1028
1077
```
1029
1078
1030
- > You can get the full code [ here] ( ../guide/example/example-formats-13 .kt ) .
1079
+ > You can get the full code [ here] ( ../guide/example/example-formats-14 .kt ) .
1031
1080
1032
1081
As we can see, the result is the dense binary format that only contains the data that is being serialized.
1033
1082
It can be easily tweaked for any kind of domain-specific compact encoding.
@@ -1221,7 +1270,7 @@ fun main() {
1221
1270
}
1222
1271
```
1223
1272
1224
- > You can get the full code [ here] ( ../guide/example/example-formats-14 .kt ) .
1273
+ > You can get the full code [ here] ( ../guide/example/example-formats-15 .kt ) .
1225
1274
1226
1275
As we can see, our custom byte array format is being used, with compact encoding of its size in one byte.
1227
1276
@@ -1239,6 +1288,7 @@ This chapter concludes [Kotlin Serialization Guide](serialization-guide.md).
1239
1288
1240
1289
<!-- references -->
1241
1290
[ RFC 7049 ] : https://tools.ietf.org/html/rfc7049
1291
+ [ IoT ] : https://en.wikipedia.org/wiki/Internet_of_things
1242
1292
[ RFC 7049 Major Types ] : https://tools.ietf.org/html/rfc7049#section-2.1
1243
1293
1244
1294
<!-- Java references -->
@@ -1286,5 +1336,6 @@ This chapter concludes [Kotlin Serialization Guide](serialization-guide.md).
1286
1336
[ Cbor ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-cbor/kotlinx-serialization-cbor/kotlinx.serialization.cbor/-cbor/index.html
1287
1337
[ Cbor.encodeToByteArray ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-cbor/kotlinx-serialization-cbor/kotlinx.serialization.cbor/-cbor/encode-to-byte-array.html
1288
1338
[ Cbor.decodeFromByteArray ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-cbor/kotlinx-serialization-cbor/kotlinx.serialization.cbor/-cbor/decode-from-byte-array.html
1339
+ [ CborBuilder.ignoreUnknownKeys ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-cbor/kotlinx-serialization-cbor/kotlinx.serialization.cbor/-cbor-builder/index.html#kotlinx.serialization.cbor%2FCborBuilder%2FignoreUnknownKeys%2F%23%2FPointingToDeclaration%2F
1289
1340
[ ByteString ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-cbor/kotlinx-serialization-cbor/kotlinx.serialization.cbor/-byte-string/index.html
1290
1341
<!-- - END -->
0 commit comments