59 points · by ingve · 1y ago · 81 comments
I wrote a more detailed comparison on: https://www.nayuki.io/page/bittorrent-bencode-format-tools
Netstrings was proposed in 1997.
https://cr.yp.to/proto/netstrings.txt
Whether bencoding's remarkable similarity to netstrings is purely coincidence is left as a question for the reader.
Perhaps there is a "natural bias" to view netstrings through the lens of bencoding even though bencoding came much later.
"Oddly enough, BitTorrent's bencode format is a significant subset of JSON but is much easier to parse as a binary format."
Curious what makes it "odd".
JSON assumed that memory is an unlimited resource.^1 It is like having to read an entire file into memory before processing it. Hence we see revisions such as "line-delimited JSON". Netstrings is even more memory-efficient than line-oriented processing; there is no need to read in an entire line.
1. This makes sense if one agrees that JSON was designed for graphical web browsers, programs notorious for excessive memory usage.
I liked it, but alas it never seemed to really take off.
https://en.wikipedia.org/wiki/CBOR
It is very popular in embedded systems and IoT. It is basically binary JSON, but supports numbers as key, variable-sized ints/floats, utf-8 strings. CBOR binary blobs can be converted to JSON keeping similar semantics (number keys will be converted to strings) and parsed with `jq` for human-readability/debugging.
It can also be read in "pull parsing" mode so you don't need to keep the whole message in memory. This is the opposite of "DOM parsing" although I don't know if these terms are applicable in non XML contexts. When you only have 256KB of RAM this is quite important.
I want to say server side JavaScript plays a role in this too, but I’m not a JS developer.
There's some decent cookbooks/recipes but they're still 1/5th as big as they could be.
A bash script might need to execute it and you want something without lots of funny characters or whitespace as it's going straight in to the terminal.
That necessary terseness makes it the opposite of readable.
JSON is just a modern flavor of XML, and in a few years we'll likely mock both of them as if they were the same silly thing. They are functionally equivalent: not human-writable, cumbersome, crufty, with unclear semantics, with unclear semantic usage, and all around ugly.
Yml goes too far in the brevity side - I find the 2 space indent, use of “-“ as a list delimiter, white space sensitivity and confusing behaviour with quoted vs unquoted strings incredibly hard to work with.
But for that case you don't need json. A dockerfile-like text file with lines of the form
STRING other stuff
is trivial to parse in any language and without requiring any library. And it's actually human-editable.Using json for such trivial stuff is like using a linear algebra library to compute b/a (the quotient of two numbers) by calling linalg.solve([[a]],[b]). Of course it will work. Of course it is more general. But it feels silly.
With your approach, I have to either not use these features, do a lot of manual testing to figure out the corner cases of your implementation, or spend a non-trivial amount of time to implement them because they weren't considered from the start. This hardly seems better than just using JSON.
I don't think that would solve any of your listed problems with json. The key thing is that it's represented by:
struct JsonObject {
Dictionary<string, JsonObject> entries;
}
and that's the entire data structure. Your dockerfile example handles the absolute most basic of cases, e.g. {
"foo": ["bar", "baz"]
}
would be FOO ["bar", "baz"]
But when you nest objects .e.g. {
"foo": {
"bar": "baz"
}
}
The dockerfile-style example is FOO { BAR baz }
or FOO
BAR baz # Now we're whitespace sensitive
Or maybe we're: Foo # What do we put here?
Foo.bar baz
> Using json for such trivial stuff is like using a linear algebra libraryI disagree. I think using xml for that is like using a linear algebra library, as you can have:
<Foo>Bar</Foo>
<Foo value="Bar"></Foo>
<Foo><Bar>Baz</Bar></Foo>
<Foo bar="Baz"></Foo>
<Foo><Bar value="Baz"></Bar></Foo>
and end up with the same thing. But with json, it's just: { "Foo": "Bar"}
and {
"Foo": {
"Bar: "Baz"
}
}
E: FormattingYAML, JSON and XML have very well defined specs and the encoders/parsers have been proven to be very reliable across OS/language/libraries. JSON in special tends to be the least problematic because it is simpler than the others and the browsers implementation is kind of the canonical implementation.
JSON is trivial as it is, don’t try to reinvent the wheel badly.
As his site no longer allows me to view with Firefox-115 ESR, it being deemed as "too old" despite still being a supported release.
> You're using a suspiciously old browser
> You're probably reading this page because you've attempted to access some part of my blog (Wandering Thoughts) or CSpace, the wiki thing it's part of. Unfortunately you're using a browser version that my anti-crawler precautions consider suspicious, most often because it's too old (most often this applies to versions of Chrome). Unfortunately, as of early 2025 there's a plague of high volume crawlers (apparently in part to gather data for LLM training) that use a variety of old browser user agents, especially Chrome user agents. To reduce the load on Wandering Thoughts I'm experimenting with (attempting to) block all of them, and you've run into this.
> If this is in error and you're using a current version of your browser of choice, you can contact me at my current place at the university (you should be able to work out the email address from that). If possible, please let me know what browser you're using and so on, ideally with its exactl User-Agent string. Chris Siebenmann, 2025-02-17
Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0
however if I override it to the following, the site lets be through: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0Can somebody please shed some light at this? Will gawk get JSON support. Or is is already there and I just need to get a recent version?
[0] https://www.gnu.org/software/gawk/manual/html_node/gawkextli...
That's why I've made a 1:1 binary format
The reasoning behind forbidding them is spurious.
(And there are pretty much no formats that a human hasn't learned to read, up to and including binary)
I parse things usually at a rate of 2-20 million tokens per second, on a local computer. Never hallucinates and it is always perfect.
Don't get me wrong. I use LLMs a lot, but they are good at what they are good at.
JSON is ALSO easy for machines to read. I know because I made several JSON parsers myself. I have also made parsers for computer languages(with things like PEG) and natural languages and there is a world of difference.
If you want something like JSON that is designed for humans take a look at YAML
JSON is not fit for purpose as a configuration format. Use YAML or don’t, use properties if you like. JSON is first and foremost for m2m- it’s even in the name “Object Notation”
I have always preferred (even without thinking) to use configuration files as Yaml and kept Json for interprocess communication