ScalaPB and JSON
ScalaPB can convert protocol buffers to and from JSON, using json4s.
Setting up your project
Make sure that you are using ScalaPB 0.5.x or later.
build.sbt add a dependency on
In your code, you can now convert to JSON:
Parse JSON back to a protocol buffer:
There are lower-level functions
fromJson() that convert from
protos to json4s’s
Finally, in JsonFormat there are two implicit methods that instantiate
More printing and parsing options
There are a few more options available to customize the format used to print
and parse JSON. To take advantage of that, instantiate
fromJson() as usual.
false): should fields that are set to their default value be included in the output.
false): by default, field names are mapped to lowerCamelCase and become JSON object keys. Setting this option to
truewould make the parser and the printer use the original field names as specified in the proto file (normally, in snake_case)
See the list of constructor paramerters here