Skip to content

Instantly share code, notes, and snippets.

@culttm
Created April 28, 2025 18:24
Show Gist options
  • Save culttm/0d6b5397d6a4b0243a983f258df9c0fe to your computer and use it in GitHub Desktop.
Save culttm/0d6b5397d6a4b0243a983f258df9c0fe to your computer and use it in GitHub Desktop.
inline fun <reified T : Message> protoFromJson(json: String): T {
val builder = T::class.java.getDeclaredMethod("newBuilder").invoke(null) as Message.Builder
JsonFormat.parser().merge(json, builder)
return builder.build() as T
}
inline fun <reified T : Message> protoToJson(message: T): String {
val builder = T::class.java.getDeclaredMethod("newBuilder", message::class.java).invoke(null, message) as Message.Builder
return JsonFormat.printer().print(builder)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment