// HACKER NEWS — CYBERSECURITY
Packing Binary Is Fun
I saw someone on twitter arguing that saving data in JSON was apparently not what Real™ developers do.
“How hard could it be to make my own binary format?”
I ended up building an entire binary schema language that can shrink JSON payloads by 80%.
Since these ASCII values fit within 8 bits, each character takes up 1 byte.
Obviously, we could just write 104 as ASCII characters:
But that’s 3 bytes for a number that only needs 1 byte.
So if we want to save those 2 bytes, we need some way of telling the decoder, “hey, this is an integer, not a string.”
You could add a header, you would only be adding an additional byte (well depends on how many types you got.. hopefully you don’t have more than 128 types…if you do you got bigger issues.
great! lets just use the first byte to represent our type and second to represent our data.
we need a way to represent different lengths of data. welp lets just get another byte. that should represent the length of our string.
so now our binary becomes