https://github.com/scalapb-json/scalapb-circe
Json/Protobuf convertors for ScalaPB use circe
https://github.com/scalapb-json/scalapb-circe
circe json protobuf scala scala-js scalapb
Last synced: 5 months ago
JSON representation
Json/Protobuf convertors for ScalaPB use circe
- Host: GitHub
- URL: https://github.com/scalapb-json/scalapb-circe
- Owner: scalapb-json
- License: mit
- Created: 2018-01-03T01:14:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-12-23T21:55:32.000Z (6 months ago)
- Last Synced: 2025-12-25T11:40:59.990Z (6 months ago)
- Topics: circe, json, protobuf, scala, scala-js, scalapb
- Language: Scala
- Homepage:
- Size: 423 KB
- Stars: 47
- Watchers: 3
- Forks: 11
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# scalapb-circe
[](https://javadoc.io/doc/io.github.scalapb-json/scalapb-circe_3)
The structure of this project is hugely inspired by [scalapb-json4s](https://github.com/scalapb/scalapb-json4s)
## Dependency
Include in your `build.sbt` file
### core
```scala
libraryDependencies += "io.github.scalapb-json" %% "scalapb-circe" % "0.16.0"
```
for scala-js or scala-native
```scala
libraryDependencies += "io.github.scalapb-json" %%% "scalapb-circe" % "0.16.0"
```
### macros
```scala
libraryDependencies += "io.github.scalapb-json" %% "scalapb-circe-macros" % "0.16.0"
```
## Usage
### JsonFormat
There are four functions you can use directly to serialize/deserialize your messages:
```scala
JsonFormat.toJsonString(msg) // returns String
JsonFormat.toJson(msg) // returns Json
JsonFormat.fromJsonString(str) // return MessageType
JsonFormat.fromJson(json) // return MessageType
```
### Implicit Circe Codecs
You can also import codecs to support Circe's implicit syntax for objects of type `GeneratedMessage` and `GeneratedEnum`.
Assume a proto message:
```scala
message Guitar {
int32 number_of_strings = 1;
}
```
```scala
import io.circe.syntax._
import io.circe.parser._
import scalapb_circe.codec._
Guitar(42).asJson.noSpaces // returns {"numberOfStrings":42}
decode[Guitar]("""{"numberOfStrings": 42}""") // returns Right(Guitar(42))
Json.obj("numberOfStrings" -> Json.fromInt(42)).as[Guitar] // returns Right(Guitar(42))
```
You can define an implicit `scalapb_circe.Printer` and/or `scalapb_circe.Parser` to control printing and parsing settings.
For example, to include default values in Json:
```scala
import io.circe.syntax._
import io.circe.parser._
import scalapb_circe.codec._
import scalapb_circe.Printer
implicit val p: Printer = new Printer(includingDefaultValueFields = true)
Guitar(0).asJson.noSpaces // returns {"numberOfStrings": 0}
```
Finally, you can include scalapb `GeneratedMessage` and `GeneratedEnum`s in regular case classes with semi-auto derivation:
```scala
import io.circe.generic.semiauto._
import io.circe.syntax._
import io.circe._
import scalapb_circe.codec._ // IntelliJ might say this is unused.
case class Band(guitars: Seq[Guitar])
object Band {
implicit val dec: Decoder[Band] = deriveDecoder[Band]
implicit val enc: Encoder[Band] = deriveEncoder[Band]
}
Band(Seq(Guitar(42))).asJson.noSpaces // returns {"guitars":[{"numberOfStrings":42}]}
```
### Credits
- https://github.com/whisklabs/scalapb-playjson
- https://github.com/scalapb/scalapb-json4s