5 ms·
The biggest advantage of JSON and YAML is that it has a very straight-forward object model consisting of literals, lists and dicts. All of these constructs map
by kanox 6y ago
The biggest advantage of JSON and YAML is that it has a very straight-forward object model consisting of literals, lists and dicts. All of these constructs map very easily to native data structures in most languages: you can just "load" and "save" an arbitrary json or yaml document.
The XML DOM is very different and does not map in an obvious way to objects in any programming language: you either use a DomDocument or object serialization for a subset of XML.
This alien object model makes programming with XML painful and verbose for no gain.
Sure: standards for xml schema and xpath are pretty nice but there are json equivalents which don't have to deal with elements and attributes. If standardization around YAML and XML was equally good would anyone pick XML?
> way too easy to forget what level of indentation you're currently at
This is easily solvable by tools. How is it worse than getting lost in braces or open/close tags?
> I think more projects (at least larger ones) should adopt their own DSL for configuration.
Please don't. JSON, YAML and XML config files can be easily and safely manipulated in all programming languages, for custom DSLs people end up using brittle regexps. It's not much better than putting config consts in a code file.
- Tainnor 6y ago> This is easily solvable by tools. Maybe, but I want it to work with standard tools. Most editors seem to arbitrarily jump back a couple of indentation levels e.g. if you add newlines somewhere. This obviously doesn't happen with JSON, since the editor won't have to guess whether you wanted to de-dent or not, it's all explicit. And I'm not making this problem up, it's literally a problem almost every time I have to edit some larger YAML file. I'm not a fan of whitespace sensitivity in programming languages either, but at least Python etc. make a habit of discouraging 15 layers of indentation. > Please don't. JSON, YAML and XML config files can be easily and safely manipulated in all programming languages, for custom DSLs people end up using brittle regexps. It's not much better than putting config consts in a code file. What? Why would you parse a DSL with regex? I'm not saying people should write crappy DSLs. You can either write your own proper parser or you just use an embedded DSL, since there are multiple languages out there who are quite good at providing DSL support. The bonus is that it's often even type-safe, without having to be as mind-numbingly obtuse as XML.
- Pako 6y agoOn the note of type safety I can always suggest people to take a look at Dhall. https://dhall-lang.org https://dhall-lang.org
- Tainnor 6y agoYes, this is something I've wanted to check out for a while now, I just don't think I could convince other people on any team to look at it. :D
- thinkharderdev 6y agoI've had the same experience. Different editors and IDEs seem to handle YAML in wildly different ways. If you copy/paste a YAML stanza into a different document there is no telling how the editor is going to indent it. And trying to fix it can be something of a nightmare, especially when the editor indents different lines in the pasted text differently.
- kanox 6y ago> What? Why would you parse a DSL with regex? Let's say I want to process a nginx or apache config file programmatically to change a flag. Am I going to parse the full syntax? No, I'm going to hack something together with regexps.
- ithkuil 6y ago> Sure: standards for xml schema and xpath are pretty nice but there are json equivalents which don't have to deal with elements and attributes. Not quite there yet. Jsonpath is still not standardized (although a colleague of mine is working on it) and the many implementations out there disagree with each other. (That said, I do agree with the general sentiment against XML)
- GoblinSlayer 6y agoThere's DomDocument for json: https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsondocument https://docs.microsoft.com/en-us/dotnet/api/system.text.json... For yaml: https://github.com/jbeder/yaml-cpp/blob/master/docs/Tutorial.md https://github.com/jbeder/yaml-cpp/blob/master/docs/Tutorial...
- barefootliam 6y ago"The biggest advantage of JSON and YAML" should be put in a context of, "when working as a programmer with config files". When we designed XML, we were working on things like a million pages of Unix documentation, or the flight manuals for aircraft, or research journal articles, or transcriptions of inscriptions found on fragments of ancient Greek pottery - lots of small similar documents, or very complex documents. XML lets people who are not primarily programmers do sophisticated text processing and information design without depending on outside resources. People who complain about XML being brittle are saying they want a system that forgives syntax errors, in the same way that JavaScript... doesn't... or that JAVA... doesn't... or that C... doesn't...or that Python... doesn't. XML is unbeaten for the things it's good at. It was (still is, often) a problem that people used if in places where it wasn't so good. But that doesn't make it bad - it means you need to understand where it's good and where it isn't.
- adamretter 6y agoAs the old saying goes - Use the right tool for the right the job!