A Protobuf Wrapper Is a Field That Can Hold Zero (and Pays Four Bytes for It)

Proto3 has a hole in its data model: a missing integer and a real zero are the same bytes, which is zero bytes, so a message cannot say the counter was counted and came out at zero. The wrapper types exist to close that hole. Int32Value is a message with exactly one field, value, at field number one, and that is the entire type. BoolValue, StringValue, Int64Value, all of them, same shape. I put one in a message next to a plain int32 and watched what the wire did. With the plain field at zero the field wrote nothing, as always. With the wrapper set to zero the wire had four bytes: a tag, a length, and the two-byte inner 08 00. The zero survived. That is the whole feature, and the price is that a zero you cannot distinguish from an absence costs four bytes where the absence costs none.

The wire does not mark a wrapper as a wrapper. The inner 08 00 is the same two bytes a plain int32 zero would be if it were on the wire at all, and I encoded a proto3 explicit-optional int32 and an Int32Value with the same value and got identical bytes out. A decoder without your schema cannot tell which meaning it is reading; the meaning is a label in the file, not something in the stream. That is the usual protobuf shape, and it means the wrapper's presence semantics are a promise between the two ends, enforced by the schema both sides agree to hold.

The JSON side is where the wrapper behaves differently from its WKT cousins, and it is the good behavior. Timestamp and Duration reject their official string forms. Any and Struct accept their official forms and lose the content without complaining. The wrappers take a scalar where they expect an object and throw: feed count a bare 0 and the encoder says object expected, and a bare string into a StringValue gets the same answer. No silent door, no value quietly gone; the mismatch is loud, which is the one of the WKT family that fails the way you want a mismatch to fail. The output side is the one gap: the tool hands you the object form, count as {"value":0}, not the unwrapped {"count":0} the mapping specifies, same gap the Timestamp post covers.

Two details worth keeping. A null where a wrapper is expected writes the field as absent, so the JSON null and the missing field land on the same wire state, which is the feature working as intended. And Int64Value is the one wrapper that carries the int64 story. 9007199254740993 encodes as an eight-byte varint under its tag, nine bytes for the field, and the JSON comes back as the decimal string, exact. The value field sits at number one, exactly where a plain int64 field would sit, so a big number costs the same bytes with or without the wrapper; the wrapper is not a size play, it is the presence a plain int64 cannot give you, and that is the whole reason the type exists.

All of it runs in the browser with no protoc. Paste a file that imports wrappers.proto and the types resolve on every tab, encode and watch the zero stay on the wire where a plain field would let it disappear, or decode something you did not make on the raw decode tab at proto.datajutsu.com and read the inner bytes of any wrapper as the plain field they are. The sister post on Timestamp is the Timestamp post, the other WKT this one keeps comparing against, and the one on Struct is the Struct post, the WKT where the silent loss is biggest.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe