Formally: no.

Practically: almost always

You shouldn't **demand** it.

Basically, it goes like this:

- the spec asks that writers *should* write fields in order
- the spec asks that readers *must* allow fields in any order
- data can be concatenated as a merge, meaning fields can appear out of
order
- how extension / unexpected fields are handled is often... inconsistent
- but in most cases: it'll probably work in the way you expect - just: it
isn't **required** to be identical

I'm aware of cases even in "protoc" (so: the C++ version) where it writes
fields out of order (relating to extension fields).

Additionally, the format itself allows for the same value to be expressed
in different ways - in particular as varint. Example: the value 1 is
usually expressed as the byte { 1 }, but actually { 129, 0 } and { 129,
128, 0 } should (if I have my math right) evaluate as 1. I am not aware of
anything that makes this alternative byte sequence formally invalid,
although I'd love to see an explicit / official answer on that.

Does that help any?

On 25 Jul 2017 9:26 p.m., <a...@thoughtmachine.net> wrote:

(Attempt 2 - I'm not sure if messages to this list and moderated or whether
my first one just got lost in the ether)

Are protobufs serialised canonically?  By that I mean is the same message
with the same fields populated with the same data guaranteed to serialise
to the same sequence of bytes, regardless of language/implementation
version?

The reason I ask is we're using serialised single-field protos as keys in a
Kafka topic, and I'm now wondering if this is safe.  The idea was to avoid
ambuiguity about how to interpret the contents of the key.  However, a
critical requirement of a key is that the same key must serialise to the
same bytes (Kafka is only aware of bytes - nothing else).

Is what I'm doing safe?  Is it safe for some subset of proto field types
(for example, I'm already pretty sure it's not safe for map types because
there is no canonical ordering, right?)?

Thanks,
Alex

-- 
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to