346 points · by rohitpaulk · 4y ago · 201 comments
(I am an absolutist on this matter. To be a superset, all, that's A L L valid JSON strings must also be valid YAML to be a superset. A single failure makes it not a superset. At scale, any difference will eventually occur, which is why even small deviations matter.)
According to https://yaml.org/spec/1.2.2/, YAML 1.2 (from 2009) is a strict superset of JSON. Earlier versions were an _almost_ superset. Hence the confusion in this thread. It depends on the version…
> Addendum/2009: the YAML 1.2 spec is still incompatible with JSON, even though the incompatibilities have been documented (and are known to Brian) for many years and the spec makes explicit claims that YAML is a superset of JSON. It would be so easy to fix, but apparently, bullying people and corrupting userdata is so much easier.
I assume these are something related to non-ascii string encoding / escapes?
> Addendum/2009: the YAML 1.2 spec is still incompatible with JSON
The author also details their issues in, ah, getting some of the authors of the YAML specification to agree.
> An implementation may set limits on the length and character contents of strings.
So this length limit is not a source of incompatibility with JSON.
Then we said it's too verbose. We named some subsets XML, HTML, XLSX.
Then we said it's still too long. So we named some subsets Markdown, and YML.
Then we said it's still too long, and made JSON.
What's wrong with subsets? Ambiguity in naming things.
https://martinfowler.com/bliki/TwoHardThings.html
Is JSON the same as YML?
NO.
Norwegian?
If anything, XML as an SGML subset is more verbose than SGML proper; in fact, getting rid of markup declarations to yield canonical markup without omitted/inferred tags, shortforms, etc. was the entire point of XML. Of course, XML suffered as an authoring format due to verbosity, which led to the Cambrian explosion of Wiki languages (MediaWiki, Markdown, etc.).
Also, HTML was conceived as an SGML vocabulary/application [1], and for the most part still is [2] (save for mechanisms to smuggle CSS and JavaScript into HTML without the installed base of browsers displaying these as content at the time, plus HTML5's ad-hoc error recovery).
> Then we said it's still too long, and made JSON.
JSON is older than markdown and yaml.
SGML and its descendants are okay for document markup.
XML for data (as opposed to markup) is either evil or clown-shoes-for-a-hat insane — I can’t figure out which.
JSON is simultaneously under- and over-specified, leading to systems where everything works right up until it doesn't. It shares a lot with C and Unix in this respect.
{
"list": [
{},
{}
]
}
(tested on Python)edit: whitespace didn't quite make it through HN, here:
json.loads('{\n "list": [\n {},\n\t{}\n ]\n}')
yaml.load ('{\n "list": [\n {},\n\t{}\n ]\n}')It's 2022 and we can't even get a plaintext configuration format from 1980 right.
To me, it's more depressing that we've been at this for 50-60 years and still seemingly don't have an unambiguously good plaintext configuration format at all.
https://github.com/python/cpython/blame/d75a51bea3c2442f81d3...
Oh, maybe it’s this issue:
https://bugs.python.org/issue34132
If I’ve read it correctly, there was a regression from Python 2.x to 3.x such that you now need to format comments:
#like this
Instead of: # like this
(A space after the # isn’t accepted by the parser.) try:
try:
import orjson as json
except:
try:
import rapidjson as json
except:
try:
import fast_json as json
except:
import json
foo = json.loads(string)
except:
try:
import yaml
except:
# try harder
import os
try:
assert(os.system("pip3 install yaml") == 0)
except:
# try even harder
try:
assert(os.system("sudo apt install python3-pip && pip3 install yaml") == 0)
except:
assert(os.system("sudo yum install python3-pip && pip3 install yaml") == 0)
import yaml
try:
foo = yaml.loads(string)
except:
try:
.... pip install --user yaml
increases the chances it will workI've seen that kind of approach cause a ton of issues the moment that the software was used in a different environment than the author expected.
It's much better IMO to fail with a message about how to install the missing dependency.
PATH=$HOMEBREW_PREFIX/opt/ansible/libexec/bin:$PATH
pip list | grep -i yaml
python -V
python <<'DOIT'
from io import StringIO
import yaml
print(yaml.safe_load(StringIO(
'''
{
"list": [
{},
{}
]
}
''')))
DOIT
produces PyYAML 6.0
Python 3.10.1
{'list': [{}, {}]} $ sed 's/\t/--->/g' break-yaml.json
--->{
--->--->"list": [
--->--->--->{},
--->--->--->{}
--->--->]
--->}
$ jq -c . break-yaml.json
{"list":[{},{}]}
$ yaml-to-json.py break-yaml.json
ERROR: break-yaml.json could not be parsed
while scanning for the next token
found character '\t' that cannot start any token
in "break-yaml.json", line 1, column 1
$ sed 's/\t/ /g' break-yaml.json | yaml-to-json.py
{"list": [{}, {}]}The error from PyYaml 5.3.1:
yaml.scanner.ScannerError: while scanning for the next token
found character '\t' that cannot start any token
in "<unicode string>", line 4, column 1> This makes it easy to migrate from JSON to YAML if/when the additional features are required.
If JSON interop is provided solely as a short-term solution that eases the transition to YAML, then I applaud the YAML designers for making a great choice.
My impression was JSON came years after YAML, and it was somehow coincidental that YAML was almost a superset of JSON.
(Shockingly wikipedia tells me they both came out within a month of each other in 2001).
C++ is not a strict superset of C, but the ability to include C headers is very valuable.
I read "YAML is a superset of JSON" not as a logical statement, but as instructions to humans writing YAML. If you know JSON, you can use that syntax to write YAML. Just like, if you know JavaScript or Python (or to some extent PHP) object syntax, you can write JSON.
If you get a parse error, no biggie, you Alt+Tab to the editor where you are editing the config file and correct it. It is not like you are serving this over the net to some other program.
Does such code count as valid TypeScript though? It sounds more as if the compiler has an option to accept certain invalid programs.
You could build a C++ compiler with a flag to warn, rather than error, on encountering implicit conversions that are forbidden by the C++ standard. The language the compiler is accepting would then no longer be standard C++, but a superset. (Same for all compiler-specific extensions of course.)
Personally I'm inclined to agree with this StackOverflow comment. [0] It's an interesting edge-case though.
[0] https://stackoverflow.com/questions/29918324/is-typescript-r...
Always read documentation or you will get burned by something completely innocuous. For example, that’s how you get Norway missing in your config file with YAML:
NI: Nicaragua
NL: Netherlands
NO: Norway
Oops, “NO” evaluates to false like 10 other reserve words.Software/specs/formats have edge-cases. Theses exists usually because of a tradeoff in usability. That's why there is YAML, JSON, TOML, etc. Choose the one that best fits your use-case and the strictness you need.
This feels like an attempt at victim blaming. If you have to read an entire spec from top to bottom to avoid a pitfall in a relatively common operation, maybe something is wrong.
FWIW, I couldn't even find the relevant section in that spec from a quick glance. I probably would have to read a significant portion of that spec just to figure out where it went wrong.
I don't think I understand what this means in context of my comment. Are you referring to the parent of my comment?
Someone wrote an article about an interesting thing they discovered about a well-known spec and OP's response is "Are people not even reading about what they are using?" Did the author of the article do something wrong?
[1] https://www.json.org/json-en.html
Saying that all formats have edge cases as an excuse for YAML's glaring faults is, frankly, a cop out. Like if a bridge collapses when a leaf lands on it and saying, well, all bridges have some maximum load. Yes, but in this case it's so bad it's just not useful for anything.
What does the length of the JSON spec have to do with my comment? The parent comment says if you don’t read all your docs you will be bit by an innocuous bug. You linked to a short spec, but that doesn’t mean anything in this context.
> Like if a bridge collapses when a leaf lands on it and saying, well, all bridges have some maximum load.
Is that what you got from reading my comment or the article? Is that what yaml is like?
A couple of years ago I looked at tutorials and found it very confusing, but the spec is just great.
Those are forgivable. I watched a team get burned by doing software HMAC and it turns out the underlying native function in the kernel is not thread safe. Would have caught me too.
JSON being a subset of YAML is a core feature. It was to help with adoption.
There's a difference in not reading all pages of every document and not reading anything at all. Not even a well researched blog post.
If this individual is a junior, then awesome, they learned a lot of valuable ideas and solutions.
If they're a senior, yikes. Don't use technology you can't explain to the junior discovering core features and writing blog posts about it
No, not the entire spec, but I glanced through the wiki page and a few tutorials to understand it.
>Choose the one that best fits your use-case and the strictness you need.
Well, how can one choose what fits best if they don't even research the topic?
Then how are you protecting yourself from “innocuous bugs” you mention without reading all of the spec?
1. Significant whitespace in a data storage file doesn't scale. Yes, eventually someone wants to dump a giant graph of data and the library breaks.
2. Intermixed context on quoteless strings. Intermixing code and data doesn't work reliably. Every where I see it tried I see it break. If you don't want quotes on your strings, then you have to put something on your keywords. A simple @no would have stopped this and other situations.
As an aside, I find it stupid to have multiple names for true, false, and null.
1 : 1,0
2: 01
3: 1.0
4: 1O
5: 0b1
6: 0x1
7: 0i1
8: hey,
8.0: oh,
version: [3.1, "3.1", 3.10, "3.10"]
Gives you: {
"1": "1,0",
"2": 1,
"3": 1.0,
"4": "1O",
"5": 1,
"6": 1,
"7": "0i1",
"8": "oh,",
"version": [
3.1,
"3.1",
3.1,
"3.10"
]
}
YAML turns raw literals into strings, except when the string matches a certain format. Then it may turn it into other things, like int or float, and you better know all the rules by heart and be attentive. And not introduce any typo, which of course no human ever does.(This issue was recognised and since yaml 1.2 (2009) the spec says "no" is a string, packages like https://yaml.readthedocs.io/ have migrated a while ago)
If config file is autogenerated then it's a different matter.
What is this strange concept you bring up? Googling saved me an entire day of reading dry documents. I may not know how the code works, but I go around telling everyone how easy coding is because of copy+paste.
Hate:
- human-wise ambiguity of its syntax (If I understand correctly, you "can" indent array items, but you don't have to. And than one guys says "OK, I'm gonna indent", and another guy says "Nah. I'm not gonna indent")
- still no support for datetime as a first class citizen
- strings usually don't need quotes, until they do (I prefer to always quote)
Note that two of the above points are about allowing inconsistent styles, which is a thing I hate.
Love:
- it supports comments whereas JSON does not. If the IETF ever officially updated JSON to support C-style inline (and maybe block) comments, I would absolutely ditch YAML.
In particular:
* How to deal with unrepresentable values: https://github.com/kstenerud/concise-encoding/blob/master/ce...
* Mandatory limits and security considerations: https://github.com/kstenerud/concise-encoding/blob/master/ce...
* Consistent error classification and processing: https://github.com/kstenerud/concise-encoding/blob/master/ce...
It still doesn't support / standardize dates, though.
But realistically, it's also all about the ecosystem. VSCode for example doesn't come with JSON5 support out of the box. GitHub and many other tools / renderers and supports it at least in syntax highlighting.
Also, OpenAPI specs (also known colloquially as Swagger) can only be written in JSON or YAML.
To note, JSON specifically doesn’t have comments to close the door to annotations and other kind of meta use.
In the cases where you have a tool that require json or yaml, you could use something like cue, dhall, or jsonnet, and convert it into json (or yaml). Unfortunately, that's a little tricky when your build cinfiguration itself has to be in yaml, as is the case for github actions, travis-ci, etc.
Every time I use YAML, I got bitten by some edge case. For this reason, I wouldn't count of JSON compatibility, except if the implementation also passes a comprehensive set of JSON tests (which the few I used in the past did not).
1: https://yaml.org/spec/1.2.2/#chapter-5-character-productions
Relevant (open) PR: https://github.com/yaml/pyyaml/pull/555
> Addendum/2009: the YAML 1.2 spec is still incompatible with JSON, even though the incompatibilities have been documented (and are known to Brian) for many years and the spec makes explicit claims that YAML is a superset of JSON. It would be so easy to fix, but apparently, bullying people and corrupting userdata is so much easier.
To date we honestly have not identified a case where where valid JSON is not valid YAML 1.2.
If anyone can point out a case where this is true, please file an issue here: https://github.com/yaml/yaml-spec/issues/
It's not a feature, it's a bug. Regardless of what Yaml group says.
https://docs.ruby-lang.org/en/3.0/YAML.html#module-YAML-labe...
> Do not use YAML to load untrusted data. Doing so is unsafe and could allow malicious input to execute arbitrary code inside your application.
;^)
I do believe there are likely some incompatibility bombs hiding in either the monster specification, or undoubtedly in the various implementations, but it was not my experience that one is the one to bring to yaml's court case
Of those, "NO" is fixed, 0 causing octal is fixed, to me that SQL syntax doesn't look any worse than the low bar set by normal SQL, and the CI providers using different schemas doesn't really have anything to do with YAML.
So that leaves two complaints.
I'm not immediately offended by the clock thing, like I am by NO and octal, but I don't really have the right experience to say how bad it is.
And I was going to say it's bad that nesting escaped string is hard, and it's a shame when languages don't have better quoting mechanisms... then I remembered that YAML has block quotes with no need to escape inside, so that example is just wrong. And they even link later to the stackoverflow post talking about YAML block quotes.
There are problems with YAML but these examples are not good ones.
((By the way, I've seen that "There are 63 different ways to write multi-line strings in YAML" link before but only took the time to fully understand it just now, and that's a gross exaggeration that makes me question the author more than it reinforces their point.
There's a reason the original link says -5- -6- NINE (or 63*, depending on how you count).
Block quotes have 1 or 2 characters to say what to do with newlines, then might have a digit to indicate indentation. That's "60" of the "63". I suppose if it allowed multi-digit numbers there it would be "billions" of ways to write multi-line strings in YAML? That number isn't a real criticism.))
(Ok not really)
My parser just outputs things to json
Just like python, it's just more readable.
Readability is so important for a language, it's more important than feature or correctness.
continue?