The library is designed around 4 Type Classes:
- Message Decoder (aka Decoder)
- Message Encoder (aka Encoder)
- Field Decoder
- Field Encoder
Message encoder & decoder:
Decoder type classes for encoding and decoding. An
Encoder[A] instance provides
a function that will convert any
A to a binary, and a
Decoding[A] takes a binary type to either an exception (
DecodingFailure) or an
The binary type can be an
ByteBuffer, or a
The distinctive feature of Protobuf compared to Json is that fields are referenced by a field number (index) rather than
by their name. Consequently, encoders and decoders require to know
number of each field you want to read/write.
Several implementations are available to fit multiple scenarios:
- Decode all fields of the protobuf message, or just some of them.
- Fields are numbered consecutively, or their numbering must be specified.
- Fields must be transformed or validated after decoding
- Protobuf numbering is known at compile-time or not
Field encoder & decoder:
Field decoders/encoders allow to read a specific type at a specific position in a protobuf message.
protoless provides implicit instances of these type classes for many types from the Scala standard library, including
UUID, and others. It also provides instances for
Option[A] and other generic types, but
A has an encoder/decoder instance.
The implicit instance for types
A has a
Decoder[A] instance is also provided, in order to support protobuf
For performance reasons protobuf messages are processed in a streaming fashion.
As a consequence, reading the same fields several times, trying to read a field with one type and retrying to read it with another type, or reading in reverse order is not allowed.