xref: /aosp_15_r20/external/kotlinx.serialization/guide/example/example-formats-05.kt (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1 // This file was automatically generated from formats.md by Knit tool. Do not edit.
2 package example.exampleFormats05
3 
4 import kotlinx.serialization.*
5 import kotlinx.serialization.protobuf.*
6 
<lambda>null7 fun ByteArray.toAsciiHexString() = joinToString("") {
8     if (it in 32..127) it.toInt().toChar().toString() else
9         "{${it.toUByte().toString(16).padStart(2, '0').uppercase()}}"
10 }
11 
12 @Serializable
13 data class Project(
14     @ProtoNumber(1)
15     val name: String,
16     @ProtoNumber(3)
17     val language: String
18 )
19 
mainnull20 fun main() {
21     val data = Project("kotlinx.serialization", "Kotlin")
22     val bytes = ProtoBuf.encodeToByteArray(data)
23     println(bytes.toAsciiHexString())
24     val obj = ProtoBuf.decodeFromByteArray<Project>(bytes)
25     println(obj)
26 }
27