A Protobuf Struct Is a JSON Document (and the Bytes Are Bigger Than the Document)

There is a protobuf message designed specifically to carry JSON, and I encoded some of it to see what it costs. Struct is a map from strings to Value, and Value is a oneof with six members: null, number, string, boolean, struct, and list. Every JSON value is one of those, so the type system for dynamic data is a six-way oneof, and the number member is a double. I took the seven-character JSON {"a":1} and put it in a Struct inside a message that also held a title. The Struct came out as sixteen bytes on the wire: a three-byte entry for the key, and the value is the eight-byte double 000000000000f03f under a tag that says member two. A number in this format is eight bytes whatever it is. Zero takes eight bytes, 1.7e300 takes eight bytes, and for a payload full of numbers the wire is bigger than the JSON it was built to carry.

The oneof is visible in the stream, which is unusual for a oneof and useful here. Each member of Value is a field number from one to six, and the raw view of the bytes tells you the JSON type before it tells you the value. A number is the only member that uses the fixed64 wire type, so eight bytes after tag 0x11 is always a double, no ambiguity. The null member signs as 08 00, a boolean as 20 01, the string "hi" as 1a 02 68 69, a nested struct as tag 0x2a, and a list as 0x32. So a Struct that you did not make and have no schema for can still be read as a shape: walk the entries, read each Value's member number, and you know the outline of the document, which keys are strings and which are numbers, before any schema gets a vote.

The JSON side is where the door opens, and this is the one WKT where the official mapping is JSON itself. The spec says a Struct holding a number serializes as the bare value, {"a":1}, because that is what the type means to be. The converter in the tool does not do that. It hands you the oneof-keyed form, {"a":{"numberValue":1}}, and the null member comes out as the string "NULL_VALUE" because null is an enum with one member. The input side is worse than the rejection you got from Timestamp and Duration: feed the encoder the official inline form and it accepts it, writes an empty Struct, the field present with zero inner bytes, 12 00, and the decode comes back as an empty object. No error, the round trip completes, and the value that was in the JSON is not in the bytes. It is the fifth face of the pattern this series keeps finding: the library and the spec disagree on a shape, and the disagreement does not throw, it loses.

The eight-byte number is not an accident, and it is the one honest part of the type. The official JSON mapping says numbers are IEEE 754 doubles, which is what a JSON parser already gives you, so 9007199254740993 not surviving the trip is a fact about doubles, not something protobuf added. The cost is that an integer you could have written in one or two bytes as a varint now costs eight, always, and a message of counters is a message of eight-byte numbers in this form. That is the price of being able to hold any JSON value: you pay the double on every number, and the format does not get smaller when the data turns out to be simple.

All of it runs in the browser with no protoc. Paste a file that imports struct.proto, encode a Struct, and the bytes show the doubles in the clear, eight bytes a number, with the entry tags around the keys. Decode something you did not make on the raw decode tab at proto.datajutsu.com and read the member number of every Value, which is the shape of the document before you have a schema. The sister post on what a oneof is on the wire is the oneof post, which is the mechanism this type is built on, and the one on Any is the Any post, the other half of protobuf's answer to holding unknown data: Any wraps bytes you must unpack, Struct is the unpacking already done, at eight bytes a number.

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