scalapb-json / scalapb-playjson   0.18.0

MIT License GitHub

Json/Protobuf convertors for ScalaPB use play-json

Scala versions: 3.x 2.13 2.12
Scala.js versions: 1.x
Scala Native versions: 0.4

scalapb-playjson

scaladoc

The structure of this project is hugely inspired by scalapb-json4s

Dependency

Include in your build.sbt file

core

libraryDependencies += "io.github.scalapb-json" %% "scalapb-playjson" % "0.18.0"

for scala-js or scala-native

libraryDependencies += "io.github.scalapb-json" %%% "scalapb-playjson" % "0.18.0"

macros

libraryDependencies += "io.github.scalapb-json" %% "scalapb-playjson-macros" % "0.18.0"

Usage

There are four functions you can use directly to serialize/deserialize your messages:

JsonFormat.toJsonString(msg) // returns String
JsonFormat.toJson(msg) // returns JsObject

JsonFormat.fromJsonString(str) // return MessageType
JsonFormat.fromJson(json) // return MessageType

Or you can use Reads/Writes/Format implicitly:

implicit val myMsgWrites: Writes[MyMsg] = JsonFormat.protoToWriter[MyMsg]

implicit val myMsgReads: Reads[MyMsg] = JsonFormat.protoToReads[MyMsg]

implicit val myMsgFormat: Format[MyMsg] = JsonFormat.protoToFormat[MyMsg]

Credits

fork from https://github.com/whisklabs/scalapb-playjson