7 ms·
For example, this valid JSON doesn't parse as YAML: { "list": [ {}, {} ] } (tested on Python) edit: white
by remram 5y ago
For example, this valid JSON doesn't parse as YAML:
{
"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}')
- dheera 5y agotry: 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: ....
- tomjakubowski 5y agoGreat idea. pip install --user yaml increases the chances it will work
- imoverclocked 5y agoBrilliant! What license is this published under?
- dheera 5y agoFree Art License
- nrclark 5y agoA note to readers: it's not always a good idea to put automated software installation in a place that users don't expect it. I'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.
- dheera 5y agoThis is why there should be a way to automatically install software into a sandboxed location, e.g. a virtualenv. Considering we are having software drive cars today it should be trivial and I would say even arguably expected that software should be able to autonomously "figure out" how to run itself and avoid conflicts with other software since that's a trivial task in comparison to navigating city streets.
- 0xbadcafebee 5y agoPython's .netrc library also hasn't supported comments correctly for like 5 years. The bug was reported, it was never fixed. If I want to use my .netrc file with Python programs, I have to remove all comments (that work with every other .netrc-using program). It's 2022 and we can't even get a plaintext configuration format from 1980 right.
- macintux 5y ago> 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.
- zarzavat 5y agoWe do, it’s called TOML. The future is here it’s just not equally distributed.
- Tesl 5y agoIt's perfect until you do a lot of nesting..
- donio 5y ago...or any nesting. TOML sucks for anything non-trivial.
- 41b696ef1113 5y agoIt makes me sad every time I see a newly announced tool that went for YAML instead of TOML.
- SPBS 5y agoTOML sucks for list of tables simply because they intentionally crippled inline tables to only be able to occupy one line. For ideology reasons ("we don't need to add a pseudo-JSON"). Unless your table is small, it's going to look absolutely terrible being all crammed into one line. https://github.com/toml-lang/toml/issues/516 https://github.com/toml-lang/toml/issues/516 The official way to do list of tables is (look at how much duplication there is) [[main_app.general_settings.logging.handlers]] name = "default" output = "stdout" level = "info" [[main_app.general_settings.logging.handlers]] name = "stderr" output = "stderr" level = "error" [[main_app.general_settings.logging.handlers]] name = "access" output = "/var/log/access.log" level = "info" vs handlers = [ { name = "default", output = "stdout", level = "info", }, { name = "stderr", output = "stderr", level = "error", }, { name = "access", output = "/var/log/access.log", level = "info", }, ] I would still reach for TOML first if I only needed simple key-value configuration (never YAML), but for anything requiring list-of-tables I would seriously consider JSON with trailing commas instead.
- mdaniel 5y agoTested on python what? I was curious to see what error that produced, figuring it would be some whitespace due to the difference between the list items, but using the yamlized python that I had lying around, it did the sane thing: 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': [{}, {}]}
- remram 5y agoEdited with string escapes, the tab didn't make it through HN. 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
- notreallyserio 5y agoIf it continues to be hard to share, I suggest encoding it as a base64 string so folks can decode it into a file with exactly the right contents.
- CameronNemo 5y agoThis is, unwittingly, the most YAML-relevant comment in this thread.
- jwilk 5y agoNot base64, but this should be easy to reproduce: $ printf '{\n\t"list": [\n\t\t{},\n\t\t{}\n\t]\n}\n' > test.json $ jq < test.json { "list": [ {}, {} ] } $ yamllint test.json test.json 2:1 error syntax error: found character '\t' that cannot start any token (syntax)
- ak217 5y agoThanks, I'm finally able to reproduce this. It would be great if instead of the histrionic message on CPAN (which amusingly accuses others of "mass hysteria"), the author would just say "YAML documents can't start with a tab while JSON documents can, making JSON not a strict subset of YAML". The YAML spec should be updated to reflect this, but I wonder if a simple practical workaround in YAML parsers (like replacing each tab at the beginning of the document with two spaces before feeding it to the tokenizer) would be sufficient in the short term.
- ak217 5y agoThis parses fine as YAML in all the tools I've tried. Can you provide the specific versions of the libraries you're using?