@@ -17,6 +17,7 @@ stable, these are currently experimental features of Kotlin Serialization.
1717 * [ Field numbers] ( #field-numbers )
1818 * [ Integer types] ( #integer-types )
1919 * [ Lists as repeated fields] ( #lists-as-repeated-fields )
20+ * [ ProtoBuf schema generator (experimental)] ( #protobuf-schema-generator-experimental )
2021* [ Properties (experimental)] ( #properties-experimental )
2122* [ Custom formats (experimental)] ( #custom-formats-experimental )
2223 * [ Basic encoder] ( #basic-encoder )
@@ -428,6 +429,62 @@ Field #1: 08 Varint Value = 2, Hex = 02
428429Field #1: 08 Varint Value = 3, Hex = 03
429430```
430431
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+
431488## Properties (experimental)
432489
433490Kotlin Serialization can serialize a class into a flat map with ` String ` keys via
@@ -456,7 +513,7 @@ fun main() {
456513}
457514```
458515
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 ) .
460517
461518The resulting map has dot-separated keys representing keys of the nested objects.
462519
@@ -536,7 +593,7 @@ fun main() {
536593}
537594```
538595
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 ) .
540597
541598As a result, we got all the primitive values in our object graph visited and put into a list
542599in _ serial_ order.
@@ -638,7 +695,7 @@ fun main() {
638695}
639696```
640697
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 ) .
642699
643700Now we can convert a list of primitives back to an object tree.
644701
@@ -729,7 +786,7 @@ fun main() {
729786}
730787-->
731788
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 ) .
733790
734791<!-- - TEST
735792[kotlinx.serialization, kotlin, 9000]
@@ -836,7 +893,7 @@ fun main() {
836893}
837894```
838895
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 ) .
840897
841898We see the size of the list added to the result, letting the decoder know where to stop.
842899
@@ -948,7 +1005,7 @@ fun main() {
9481005
9491006```
9501007
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 ) .
9521009
9531010In the output we see how not-null` !! ` and ` NULL ` marks are used.
9541011
@@ -1076,7 +1133,7 @@ fun main() {
10761133}
10771134```
10781135
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 ) .
10801137
10811138As we can see, the result is a dense binary format that only contains the data that is being serialized.
10821139It can be easily tweaked for any kind of domain-specific compact encoding.
@@ -1270,7 +1327,7 @@ fun main() {
12701327}
12711328```
12721329
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 ) .
12741331
12751332As we can see, our custom byte array format is being used, with the compact encoding of its size in one byte.
12761333
@@ -1341,6 +1398,10 @@ This chapter concludes [Kotlin Serialization Guide](serialization-guide.md).
13411398[ 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
13421399[ ProtoIntegerType.FIXED ] : https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-protobuf/kotlinx.serialization.protobuf/-proto-integer-type/-f-i-x-e-d/index.html
13431400
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+
13441405<!-- - MODULE /kotlinx-serialization-cbor -->
13451406<!-- - INDEX kotlinx-serialization-cbor/kotlinx.serialization.cbor -->
13461407
0 commit comments