mjalkio mjalkio -3 years ago 188
YAML Question

Latest version of ruamel.yaml fails to parse simple flow mapping

Posting this here instead of Bitbucket because I'm really unsure about whether or not this is user error. Something simple like this broke after upgrading to the latest version:

>>> from ruamel import yaml
>>> test_str = '{"in":{},"out":{}}'
>>> yaml.safe_load(test_str)

ruamel.yaml.parser.ParserError: while parsing a flow mapping
in "<byte string>", line 1, column 1:
{"in":{},"out":{}}
^ (line: 1)
expected ',' or '}', but got '<scalar>'
in "<byte string>", line 1, column 6:
{"in":{},"out":{}}
^ (line: 1)


Putting spaces after
"in"
and
"out"
resolves the issue.

Answer Source

This was a bug in ruamel.yaml<0.15.30.

The token scanner had been changed a few micro versions earlier, in order to allow :: and ?to occur in plain scalars (as required by the 1.2 specification, see example 7.10), and those changes affected this, "compact JSON", syntax.

One workaround, if you cannot upgrade ruamel.yaml, which may be easier than changing all your YAML input, is loading this explicitly as YAML 1.1:

from ruamel import yaml
test_str = '{"in":{},"out":{}}'
yaml.safe_load(test_str, version="1.1")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download