The structure of this project is hugely inspired by scalapb-json4s
Include in your build.sbt
file
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"
libraryDependencies += "io.github.scalapb-json" %% "scalapb-playjson-macros" % "0.18.0"
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]