Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert protobuf to JSON in Kotlin

Is there some easy way to convert protobuf message into JSON when using Kotlin? I have found multiple ways using JsonFormat in Java but this package does not seem to be available in Kotlin.

val myProto = MyProto.newBuilder().setId("id").build()
val json: String = ???

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Perform like this:

    val myProto = MyProto.newBuilder().setId("id").build()
    val json: String = JsonFormat.printer().print(myProto)

    println(json)

Ensure you have the protobuf and protobuf-java-util dependencies

    implementation("com.google.protobuf:protobuf-java:3.21.12")
    implementation("com.google.protobuf:protobuf-java-util:3.21.12")

Do check the version of the dependencies. It was working in mine.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading