A Protobuf Int64 Is Sixty Four Bits (and JavaScript Only Has Fifty Three)
I kept seeing int64 in the decode tab of the tool and something felt off about it. The other numbers came out as numbers, a plain 7, a plain 42, and the int64 came out as a string, a run of decimal digits in quotes, and I wanted to know whether that was a cosmetic choice or a load bearing one. So I built a one field message with an int64 in it and I fed it numbers up and down the size range until something broke. Something did break, and it broke at a specific number, and the number is the whole story.
JavaScript holds its integers inside a 64 bit floating point number, and the floating point part is the limit. The format can hold every whole number exactly up to 9007199254740991, and then it cannot. The next whole number, 9007199254740992 plus one, does not exist as a JS number. I checked it in the engine before I touched any protobuf at all, and the language told me so: the literal 9007199254740993 comes out as 9007199254740992, and adding one to 2 to the 53rd power changes nothing. The number is already gone at the point it is typed, a full step before the wire format ever sees it. Everything below that boundary is exact, everything above it is a guess, and the boundary is a specific integer you can print.
That boundary is where the encoder lies to you, and the lie is silent. I encoded the value 2 to the 53rd power plus one two ways. First as a number, which the language had already rounded to 2 to the 53rd, and the bytes it wrote were 088080808080808010. Then as the string 9007199254740993, and the bytes it wrote were 088180808080808010. Same field, same message, no error, two different payloads on the wire, and the one that came from a number in your JSON is the one that lost a unit of whatever it was counting. The string input is the only input that is guaranteed to mean what it says, because the string never went through the floating point number. That is why the JSON mapping rule makes int64 a string: it is the only spelling of a 64 bit integer that JavaScript can carry without rounding.
The wire itself has no such boundary, and I measured it to be sure. A varint grows in steps, one byte up to 127, two to 16383, and it takes eight payload bytes to hold 2 to the 53rd, nine to hold 2 to the 63rd minus one, and ten to hold anything up to the top of the range. Negative numbers are the long side of the trip, a negative one is ten payload bytes, and the fixed 64 bit type skips all of that by taking exactly eight bytes for every value it holds. The 64 bits also do their own wrap arithmetic that the language does not. I encoded 2 to the 63rd power, which is one above the top of a signed int64, and the bytes it wrote, 0880808080808080808001, are byte identical to the bytes of the smallest negative value, and decoding 2 to the 64th minus one, the top of an unsigned range, prints as negative one. The bits keep the value whole across the signed boundary at 2 to the 63rd, which is thirty two times past where JavaScript has already started rounding at 2 to the 53rd.
This one bit me in my own tool, which is why I know it is real and not theoretical. The first version ran the parser with no 64 bit integer type in the page, and I encoded the string 9007199254740993 and the bytes that came out were the bytes of 2 to the 53rd. The engine rounded the string, which is a worse failure than rounding a number, because the input had spelled out exactly what it meant. I reran the same engine with a 64 bit integer library loaded into the page first, and the same string produced the exact bytes, and the decode side, which had been printing a rounded number for the smallest negative value, printed the full 20 digit string instead. The fix was one extra script tag in the right order, and the tool at proto.datajutsu.com now carries it. The decode tab prints int64 as the exact decimal string, the encode tab takes a string and means it, and the JSON Schema generator emits the 64 bit fields as integer or string, because a conforming consumer has to accept both spellings if it wants to survive a language that rounds.
That is where it lands. The int64 is 64 bits on the wire in every language, and the 64 bits never lie. What differs is how many of them your language can hold in a number before it starts rounding, and JavaScript answers 53. The JSON mapping rule, 64 bit integers as decimal strings, looks like a paperwork decision until you find the boundary and see that it is the only way to cross it. If your bytes came from a language with the same limit, the string is your proof that nothing was rounded. The JSON mapping post covers the string rule as a mapping table, the wire format post covers the varint ladder this one measured, and the sint32 post is the size side of the same coin, where a negative one is one byte instead of ten.