@@ -17,6 +17,7 @@ stable, these are currently experimental features of Kotlin Serialization.
17
17
* [ Field numbers] ( #field-numbers )
18
18
* [ Integer types] ( #integer-types )
19
19
* [ Lists as repeated fields] ( #lists-as-repeated-fields )
20
+ * [ ProtoBuf schema generator (experimental)] ( #protobuf-schema-generator-experimental )
20
21
* [ Properties (experimental)] ( #properties-experimental )
21
22
* [ Custom formats (experimental)] ( #custom-formats-experimental )
22
23
* [ Basic encoder] ( #basic-encoder )
@@ -428,6 +429,62 @@ Field #1: 08 Varint Value = 2, Hex = 02
428
429
Field #1: 08 Varint Value = 3, Hex = 03
429
430
```
430
431
432
+ ### ProtoBuf schema generator (experimental)
433
+
434
+ As mentioned above, when working with protocol buffers you usually use a ".proto" file and a code generator for your
435
+ language. This includes the code to serialize your message to an output stream and deserialize it from an input stream.
436
+ When using Kotlin Serialization this step is not necessary because your ` @Serializable ` Kotlin data types are used as the
437
+ source for the schema.
438
+
439
+ This is very convenient for Kotlin-to-Kotlin communication, but makes interoperability between languages complicated.
440
+ Fortunately, you can use the ProtoBuf schema generator to output the ".proto" representation of your messages. You can
441
+ keep your Kotlin classes as a source of truth and use traditional protoc compilers for other languages at the same time.
442
+
443
+ As an example, we can display the following data class's ".proto" schema as follows.
444
+
445
+ <!-- - INCLUDE
446
+ import kotlinx.serialization.*
447
+ import kotlinx.serialization.protobuf.*
448
+ import kotlinx.serialization.protobuf.schema.ProtoBufSchemaGenerator
449
+ -->
450
+
451
+ ``` kotlin
452
+ @Serializable
453
+ data class SampleData (
454
+ val amount : Long ,
455
+ val description : String? ,
456
+ val department : String = " QA"
457
+ )
458
+ fun main () {
459
+ val descriptors = listOf (SampleData .serializer().descriptor)
460
+ val schemas = ProtoBufSchemaGenerator .generateSchemaText(descriptors)
461
+ println (schemas)
462
+ }
463
+ ```
464
+ > You can get the full code [ here] ( ../guide/example/example-formats-08.kt ) .
465
+
466
+ Which would output as follows.
467
+
468
+ ``` text
469
+ syntax = "proto2";
470
+
471
+
472
+ // serial name 'example.exampleFormats08.SampleData'
473
+ message SampleData {
474
+ required int64 amount = 1;
475
+ optional string description = 2;
476
+ // WARNING: a default value decoded when value is missing
477
+ optional string department = 3;
478
+ }
479
+
480
+ ```
481
+
482
+ <!-- - TEST -->
483
+
484
+ Note that since default values are not represented in ".proto" files, a warning is generated when one appears in the schema.
485
+
486
+ See the documentation for [ ProtoBufSchemaGenerator] for more information.
487
+
431
488
## Properties (experimental)
432
489
433
490
Kotlin Serialization can serialize a class into a flat map with ` String ` keys via
@@ -456,7 +513,7 @@ fun main() {
456
513
}
457
514
```
458
515
459
- > You can get the full code [ here] ( ../guide/example/example-formats-08 .kt ) .
516
+ > You can get the full code [ here] ( ../guide/example/example-formats-09 .kt ) .
460
517
461
518
The resulting map has dot-separated keys representing keys of the nested objects.
462
519
@@ -536,7 +593,7 @@ fun main() {
536
593
}
537
594
```
538
595
539
- > You can get the full code [ here] ( ../guide/example/example-formats-09 .kt ) .
596
+ > You can get the full code [ here] ( ../guide/example/example-formats-10 .kt ) .
540
597
541
598
As a result, we got all the primitive values in our object graph visited and put into a list
542
599
in _ serial_ order.
@@ -638,7 +695,7 @@ fun main() {
638
695
}
639
696
```
640
697
641
- > You can get the full code [ here] ( ../guide/example/example-formats-10 .kt ) .
698
+ > You can get the full code [ here] ( ../guide/example/example-formats-11 .kt ) .
642
699
643
700
Now we can convert a list of primitives back to an object tree.
644
701
@@ -729,7 +786,7 @@ fun main() {
729
786
}
730
787
-->
731
788
732
- > You can get the full code [ here] ( ../guide/example/example-formats-11 .kt ) .
789
+ > You can get the full code [ here] ( ../guide/example/example-formats-12 .kt ) .
733
790
734
791
<!-- - TEST
735
792
[kotlinx.serialization, kotlin, 9000]
@@ -836,7 +893,7 @@ fun main() {
836
893
}
837
894
```
838
895
839
- > You can get the full code [ here] ( ../guide/example/example-formats-12 .kt ) .
896
+ > You can get the full code [ here] ( ../guide/example/example-formats-13 .kt ) .
840
897
841
898
We see the size of the list added to the result, letting the decoder know where to stop.
842
899
@@ -948,7 +1005,7 @@ fun main() {
948
1005
949
1006
```
950
1007
951
- > You can get the full code [ here] ( ../guide/example/example-formats-13 .kt ) .
1008
+ > You can get the full code [ here] ( ../guide/example/example-formats-14 .kt ) .
952
1009
953
1010
In the output we see how not-null` !! ` and ` NULL ` marks are used.
954
1011
@@ -1076,7 +1133,7 @@ fun main() {
1076
1133
}
1077
1134
```
1078
1135
1079
- > You can get the full code [ here] ( ../guide/example/example-formats-14 .kt ) .
1136
+ > You can get the full code [ here] ( ../guide/example/example-formats-15 .kt ) .
1080
1137
1081
1138
As we can see, the result is a dense binary format that only contains the data that is being serialized.
1082
1139
It can be easily tweaked for any kind of domain-specific compact encoding.
@@ -1270,7 +1327,7 @@ fun main() {
1270
1327
}
1271
1328
```
1272
1329
1273
- > You can get the full code [ here] ( ../guide/example/example-formats-15 .kt ) .
1330
+ > You can get the full code [ here] ( ../guide/example/example-formats-16 .kt ) .
1274
1331
1275
1332
As we can see, our custom byte array format is being used, with the compact encoding of its size in one byte.
1276
1333
@@ -1341,6 +1398,10 @@ This chapter concludes [Kotlin Serialization Guide](serialization-guide.md).
1341
1398
[ ProtoIntegerType.SIGNED ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-protobuf/kotlinx.serialization.protobuf/-proto-integer-type/-s-i-g-n-e-d/index.html
1342
1399
[ ProtoIntegerType.FIXED ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-protobuf/kotlinx.serialization.protobuf/-proto-integer-type/-f-i-x-e-d/index.html
1343
1400
1401
+ <!-- - INDEX kotlinx-serialization-protobuf/kotlinx.serialization.protobuf.schema -->
1402
+
1403
+ [ ProtoBufSchemaGenerator ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-protobuf/kotlinx.serialization.protobuf.schema/-proto-buf-schema-generator/index.html
1404
+
1344
1405
<!-- - MODULE /kotlinx-serialization-cbor -->
1345
1406
<!-- - INDEX kotlinx-serialization-cbor/kotlinx.serialization.cbor -->
1346
1407
0 commit comments