I built my own binary serializer and a custom lang. heres how I did it
17 comments
- a magic header
- a version number
- a crc or data corruption check
Additionally I would argue one would be better off writing a custom text parser instead of parsing a binary format in this case, this is similar to the debate about unixlike config vs windows regedit. I would prefer something other then json but sill readable as txt.Even the json example provided at the bottom can be minified from 418 characters to 166 by replacing the field names with single characters and removing the spaces. Almost all of the savings in this format come from not including the the field names and having a position dependent layout. You can choose to have delimiters or arrange for a byte to indicate the type(+size) for example the following string encodes the example data almost (85bytes vs 80bytes) as efficient but is still readable and supports utf-8 interpretation.
123456789;LeroyJenkins;60;alliance;p,100,200,300;i,999,1,1;i,45,100,0;a,s,120;a,a,45;
You may optimize it further by allowing recurring entries and allowing assumed values from a defined default and only sending delta's can be dependent on the type of data you are expecting. { "itemId": 999, "quantity": 1, "isSoulbound": false }
i,999,1,0
could become: default = { "itemId": 0, "quantity": 1, "isSoulbound": false }
{ "itemId": 999}
i,999You should just use protobuf, far before you get to this step
When one's uncompressed RINEX files are 200 GB/day, it's worth some effort to shrink the files, especially if it means they are faster to read.
Also gzip adds something like a dozen unnecessary bytes.
Of course you can get the same kind of thing in binary. I wrote a drop-in binary JSON replacement because it's easy to write a binary one that's twice as fast as simjson and yyjson. The important thing is to never give the drop-in replacement any extras that break roundtrip compatibility.
Was a fun little project to write, and quite useful for me: https://github.com/kstenerud/bonjson
Of course that’s orthogonal to using a text v binary representation, you can have a schema’d text format, and a self-describing binary format.
BSON, UBJSON, MessagePack and CBOR are binary but self-describing.
Should the system design have required all this of a device with 4MB of memory? Probably not. But it worked, and I had a great time
Read the full thread on Hacker News →
Related stories
- Packing Binary Is Fun, Actuallyhereticpleb.vercel.appHacker News · 2 points · 3 days ago
- Packing Binary Is Fun, Actuallyhereticpleb.vercel.appLobsters · 9 points · 3 days ago
- Show HN: Binary Code Translator – free browser-based text↔binary converterbinarycodetranslator.orgHacker News · 2 points · 1 day ago
- Lobsters · 45 points · about 3 years ago
- Ars Technica · 0 points · 13 days ago
- Lobsters · 34 points · about 1 year ago