Class ProtobufCodec
- All Implemented Interfaces:
Codec
Like Avro, and for the same reason, there is no asProtobuf(). Protobuf bytes say
nothing about which message they are: field numbers and wire types, and no names or type. A
reader must be told what to parse them as, so this is reached through
publisher.as(ProtobufCodec.of(OrderPlaced.parser())).
One codec per message type, which is not a limitation so much as a description of how
protobuf is used: a destination carries one kind of message, and that is what makes the bytes
interpretable at all. A destination that needs to carry several has to say which is which, and
the protobuf answer to that is a wrapper message with a oneof — a decision about the
schema rather than about the transport, so it belongs in the .proto file.
Evolution is the format's own affair and works without anything here: readers ignore fields they do not know and use defaults for fields that are absent, provided nobody reuses a field number. Nothing in this codec can protect against a reused number, and nothing can detect it either — the bytes are valid and mean something else. Reserve retired numbers in the schema.
-
Method Summary
Modifier and TypeMethodDescriptionboolean<T> Tbyte[]static <T extends com.google.protobuf.Message>
ProtobufCodecof(com.google.protobuf.Parser<T> parser) static <T extends com.google.protobuf.Message>
ProtobufCodecThe same thing, given the class instead of its parser.toString()
-
Method Details
-
of
public static <T extends com.google.protobuf.Message> ProtobufCodec of(com.google.protobuf.Parser<T> parser) - Type Parameters:
T- message type- Parameters:
parser- the generated type's parser, as inOrderPlaced.parser()- Returns:
- a codec for that one message type
-
of
The same thing, given the class instead of its parser.- Type Parameters:
T- message type- Parameters:
type- a class generated by protoc- Returns:
- a codec for that one message type
-
contentType
- Specified by:
contentTypein interfaceCodec- Returns:
- the content type this codec produces, for example
application/json
-
encode
-
decode
-
canDecode
-
toString
-