Fields tags are numbers that serve as unique IDs for each fields in a binary format. They are important because Protocol Buffer do not store names to save space and instead, it uses those IDs. Essentially it:
- Encodes only field numbers and values
- Allows efficient binary serialisation (don’t have to convert the field name to binary)
Rules
- Numbers must be unique per message. You cannot use the same number for two different fields.
- Use small numbers that only take 1 byte (1-15)
- 16-2047 Take 2 bytes
- 19000-19999 - are reserved for internal use, so you cannot use it
- Do not change field numbers, because it will break compatibility. If you change
age = 3toage = 5, old clients will read the wrong data.
Example
syntax = "proto3"
message User {
string id = 1;
string name = 2;
int32 age = 3;
} When the User message is serialised the resulting binary data would look as follows
08 96 01 12 05 4a 6f 68 6e | Field | ProtoBuf Binary Representation | Explanation |
|---|---|---|
id = 150 | 08 96 01 | 08 = Field 1 + Varint, 96 01 = 150 |
name = "Joe" | 12 03 4A 6F 65 | 12 = Field 2 + String, 03 = length, "Joe" = 4A 6F 65 |
age = 30 | 18 1E | 18 = Field 3 + Varint, 1E = 30 |