Skip to content

scalapb-json/scalapb-playjson

Repository files navigation

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.17.0"

for scala-js or scala-native

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

macros

libraryDependencies += "io.github.scalapb-json" %% "scalapb-playjson-macros" % "0.17.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