JsonFormat

scalapb.ujson.JsonFormat
See theJsonFormat companion object
class JsonFormat(val preserveProtoFieldNames: Boolean, val includeDefaultValueFields: Boolean, val formatEnumsAsNumbers: Boolean)

Utility for writing and reading ScalaPB-generated messages to and from JSON via the ujson library.

This utility is designed around ujson's visitors. This means that intermediate data structures are avoided as much as reasonably possible, and therefore memory usage is kept low and performance should be very good for conversions. For example, if you write a ScalaPB message to a JSON string, the message will be transformed into a string directly, without passing through an intermediate JSON tree structure. Or, if you write a message to a stream, the message is written on demand and no full JSON string needs to be generated beforehand.

Attributes

formatEnumsAsNumbers

Default false. Use enum values' numbers instead of their names.

includeDefaultValueFields

Default false. If set, then fields of messages are always included in JSON, even if they correspond to the default protobuf value.

preserveProtoFieldNames

Default true. If set, then the field names of protobuf messages are used as-is (this means usually snake_case). Otherwise, they are converted to camelCase.

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object JsonFormat.type

Members list

Concise view

Type members

Classlikes

object Writer extends Transformer[GeneratedMessage]

Attributes

Graph
Supertypes
trait Transformer[GeneratedMessage]
class Object
trait Matchable
class Any
Self type
Writer.type

Value members

Concrete methods

def read[A <: GeneratedMessage](json: Readable)(using companion: GeneratedMessageCompanion[A]): A

Read JSON and convert it to a ScalaPB-generated message type.

Read JSON and convert it to a ScalaPB-generated message type.

The source of JSON can be anything than can be converted to a ujson.Readable type. Examples include:

  • String
  • java.io.InputStream
  • ujson.Value
  • geny.Readable

See the ujson.Readable companion object fort a list of all available implicit conversions.

Note: this method accepts input from any instance of JsonFormat, regardless of the customizations given as parameters. As such, it could technically only be defined as a global method, or part of the JsonFormat companion object. However, to keep symmetry with write methods and for usability, it is defined per-instance but calls out to the same global objects implementing the actual read functionality.

Attributes

Throws:
JsonReadException

if something went wrong reading or parsing the input JSON. The exception contains a position field, which can be used to retrieve the position in the input at which the error occurred.

def write(message: GeneratedMessage, indent: Int, escapeUnicode: Boolean): String

Convert a ScalaPB-generated message to its canonical JSON representation, as a string.

Convert a ScalaPB-generated message to its canonical JSON representation, as a string.

Attributes

def writeTo(message: GeneratedMessage, out: Writer, indent: Int, escapeUnicode: Boolean): Unit

Convert a ScalaPB-generated message to its canonical JSON representation and write it to some output writer.

Convert a ScalaPB-generated message to its canonical JSON representation and write it to some output writer.

This is faster than first creating a string and then writing it, and also materializes less intermediate datastructures in memory.

Attributes

def writeToByteArray(message: GeneratedMessage, indent: Int, escapeUnicode: Boolean): Array[Byte]

Convert a ScalaPB-generated message to its canonical JSON representation, as an array of bytes equivalent to its string representation.

Convert a ScalaPB-generated message to its canonical JSON representation, as an array of bytes equivalent to its string representation.

Attributes

def writeToJson(message: GeneratedMessage, indent: Int, escapeUnicode: Boolean): Value

Convert a ScalaPB-generated message to its canonical JSON representation, as a ujson.Value object.

Convert a ScalaPB-generated message to its canonical JSON representation, as a ujson.Value object.

Attributes

def writeToOutputStream(message: GeneratedMessage, out: OutputStream, indent: Int, escapeUnicode: Boolean): Unit

Convert a ScalaPB-generated message to its canonical JSON representation and write it as utf-8 bytes to an output stream.

Convert a ScalaPB-generated message to its canonical JSON representation and write it as utf-8 bytes to an output stream.

This is faster than first creating a string and then writing it, and also materializes less intermediate datastructures in memory.

Attributes

Concrete fields