Skip to content

Instantly share code, notes, and snippets.

@jviotti
Last active May 2, 2023 23:38
Show Gist options
  • Save jviotti/38c252cfbe5d90b0c8d5d28348f1fb33 to your computer and use it in GitHub Desktop.
Save jviotti/38c252cfbe5d90b0c8d5d28348f1fb33 to your computer and use it in GitHub Desktop.
JSON BinPack Example

Gists don't let me upload binary files directly. To get the output.bin binary file, run:

echo "0x03 0x01 0x01 0x66 0x6f 0x6f 0xfa 0x01" | xxd -r > output.bin
0x03 => Size of the array (3)
0x01 => Boolean "true"
0x01 => Size of the string minus minimum plus 1 (3 = 3 - 3 + 1)
0x66 0x6f 0x6f => String "foo"
0xfa 0x01 => Base-128 variable-length unsigned integer 1000, as (1000 / 4) - ceil(-2 / 4)
Reduction from 17 bytes in minified form to 8 bytes (~53%)
[ true, "foo", 1000 ]
{
"$schema": "https://www.jsonbinpack.org/schemas/encoding/v1.json",
"name": "BOUNDED_8BITS_TYPED_ARRAY",
"options": {
"minimum": 0,
"maximum": 10,
"encoding": {
"name": "FLOOR_MULTIPLE_ENUM_VARINT",
"options": {
"minimum": -2,
"multiplier": 4
}
},
"prefixEncodings": [
{
"name": "BYTE_CHOICE_INDEX",
"options": {
"choices": [ false, true ]
}
},
{
"name": "FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED",
"options": {
"minimum": 3
}
}
]
}
}
0x03 0x01 0x01 0x66 0x6f 0x6f 0xfa 0x01
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"maxItems": 10,
"items": {
"type": "integer",
"minimum": -2,
"multipleOf": 4
},
"prefixItems": [
{ "type": "boolean" },
{
"type": "string",
"minLength": 3
}
]
}
[
{ "type": "start", "path": "/", "offset": 0 },
{ "type": "start", "path": "/options/prefixEncodings/0", "offset": 1 },
{ "type": "end", "path": "/options/prefixEncodings/0", "offset": 1 },
{ "type": "start", "path": "/options/prefixEncodings/1", "offset": 2 },
{ "type": "end", "path": "/options/prefixEncodings/1", "offset": 5 },
{ "type": "start", "path": "/options/encoding", "offset": 6 },
{ "type": "end", "path": "/options/encoding", "offset": 7 },
{ "type": "end", "path": "/", "offset": 7 }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment