8 ms·
My only problem with json files is that you can't add comments to them in a persistent way (especially if changes in the data are written by a program). Unless
by amelius 6y ago
My only problem with json files is that you can't add comments to them in a persistent way (especially if changes in the data are written by a program).
Unless of course you'd put the comments in the data itself, but that's kind of ugly.
- k__ 6y agoAlso: streaming
- amelius 6y agoCan't you implement streaming by just putting everything in a giant JSON list? Or do you want to send different parts of e.g. a map in separate streams?
- k__ 6y agoHow do you parse a JSON list before you get to the end?
- speedgoose 6y agoYou can use a JSON parser for streams. Instead of returning a full document once everything is parsed, it can process elements one by one.
- Jtsummers 6y agoPresumably similar to how you would stream XML. I guess my question would be, isn't this already possible? You just need a reader that knows that the JSON source is a stream and blocks reads until data is available. Then use a concurrency mechanism to coordinate between the stream reader and the consumer of the stream data to keep things running smoothly (or if you don't mind blocking just use the blocking reader on the same thread/process as the consumer).
- k__ 6y agoHow does this work? If I got multiple items, they're in an array. If such a reader blocks reads to an element until it's finished, I've won nothing if I have to wait until that array finished.
- Jtsummers 6y agoAs I said, you'd need some measure of concurrency. Or perhaps a non-blocking reader. Try to get new stuff, if it's not there continue on, I just don't like getting "null" responses like that as it's too close to a busy-loop which is, well, poor form. I generally write code that interacts with the world in its own thread/process/whatever so it doesn't block the rest of the system unless appropriate. The utility here is if you can process each element one at a time. If you need everything to get a meaningful result it offers no benefit to you.
- im3w1l 6y agoFirst you check for a beginning [ Then you parse elements one by one. While your particular library may not support it, it's not a very hard thing to implement ["a", 1, "b A parser could at this point give us the "a" and the 1, but not the "b" since the string could have more content. You probably want the parser to give you an object that behaves as a collection you can for-each loop over, and that blocks when there is no more data available.
- hombre_fatal 6y agoA streaming JSON parser is a parser that emits {"[","]","{","}",string,number,bool,null} without holding anything in memory. Where do you think it's different from streaming XML (SAX)?
- mpfundstein 6y agothats essentially what we do in embedded systems.
- switch007 6y agoThere’s jsonlines but it is not widely supported. Eg PowerBI has to be told to treat it as CSV then convert each row in to JSON rather than a native jsonl type.
- thwarted 6y agoNo worse than putting data in comments. The CDDB format was notorious for putting track length and some other details in comments, and a fully supported parser then had to be smart enough to extract data from them
- crooked-v 6y agoYou might like JSON5 (https://json5.org https://json5.org), which allows comments and has direct support in various tooling.
- amelius 6y agoYes, thanks, but are the comments stripped out when you read the JSON? Or are they still accessible, so they can be written back when a program modifies the JSON slightly?
- kroltan 6y agoThat's a software problem moreso than a parsing problem. Plenty of software that uses regular JSON also strips unused data when writing back to a file it read because internally it transforms the document into a data structure with no space dedicated for unknown data. It would be up to the software, or a very strict standard or library, to preserve unknown/unused data.
- ehsankia 6y agoThere are many json alts, but at the end of the day unless it's supported everywhere, it kinda defeats the point, because what makes JSON great is that it's supported almost everywhere.
- hajile 6y agoOnce upon a time (a mere decade ago) JSON support was nowhere.
- dmurray 6y ago90% of when I use JSON is when I control both sides of the interaction. E.g. a web app where I have no expectation of third party clients, or a config file that will only reasonably be read by one program. JSON is normally "good enough" as a general wire interchange format, and it's human-readable and whoever comes after me will already be familiar with it. But if there's a time when it's not good enough and its failures are in expressiveness, I'd totally consider JSON5 or some other alternative instead.
- benhoyt 6y agoIt's true, but I've used patterns like this to good effect: { "_COMMENT": "default; override with --s3-bucket", "s3_bucket": "foo-bar" }
- tandav 6y agomy personal pain is that you can't add trailing comma (PAIN!!!)
- aaron-santos 6y agoPersonal opinion: putting comments in the data is a smaller sin than putting data in comments. More information @ https://news.ycombinator.com/item?id=3912149 https://news.ycombinator.com/item?id=3912149
- amelius 6y agoOk, but would you prefer: [1, 2, 3, 3.14159, "comment: Pi!!", 4, 5, 6, 7, 8] over: [1, 2, 3, 3.14159 /* Pi!! */, 4, 5, 6, 7, 8]
- aaron-santos 6y agoI'm more interested in the implications of picking these examples than the examples themselves. Hope that makes sense.
- amelius 6y agoHow about: [1, 2, 3.5 /* faster, use 3 for more stability */, 4]
- aaron-santos 6y agoI don't want all the baggage that comes with accepting that option including people who then put parsing directives inside comments. You seem like an intelligent person. Maybe you could come up with a JSON-compatible way to include comments.
- throw0101a 6y ago> My only problem with json files is that you can't add comments to them in a persistent way JSON was first designed as a data exchange format between computers (that was lighter weight than XML (which was lighter weight that SGML)), so that fact that it's being used in persistent fashions is getting away from its primary purpose.
- rumanator 6y agoI would argue that any human-readable format that relied on defining blocks by opening/closing symbols is a format that's not suited for config files that are expected to be manually edited by humans with a text editor.