Issue #11 new

Valid JSON not being loaded

Adam Pritchard
created an issue

My understanding is that YAML is a superset of JSON. I have encountered an example of valid JSON causing an error for yaml.load() and yaml.safe_load():

>>> x = json.loads('{"key": "hi\\/there"}')
... print x
{u'key': u'hi/there'}
>>> x = yaml.load('{"key": "hi\\/there"}')
... print x
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    x = yaml.load('{"key": "hi\\/there"}')
  File "C:\Python27\lib\site-packages\yaml\__init__.py", line 71, in load
    return loader.get_single_data()
  File "C:\Python27\lib\site-packages\yaml\constructor.py", line 37, in get_single_data
    node = self.get_single_node()
  File "C:\Python27\lib\site-packages\yaml\composer.py", line 35, in get_single_node
    if not self.check_event(StreamEndEvent):
  File "C:\Python27\lib\site-packages\yaml\parser.py", line 98, in check_event
    self.current_event = self.state()
  File "C:\Python27\lib\site-packages\yaml\parser.py", line 143, in parse_implicit_document_start
    StreamEndToken):
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 248, in fetch_more_tokens
    return self.fetch_double()
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 656, in fetch_double
    self.fetch_flow_scalar(style='"')
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 667, in fetch_flow_scalar
    self.tokens.append(self.scan_flow_scalar(style))
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 1156, in scan_flow_scalar
    chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
  File "C:\Python27\lib\site-packages\yaml\scanner.py", line 1230, in scan_flow_scalar_non_spaces
    "found unknown escape character %r" % ch.encode('utf-8'), self.get_mark())
ScannerError: while scanning a double-quoted scalar
  in "<string>", line 1, column 9:
    {"key": "hi\/there"}
            ^
found unknown escape character '/'
  in "<string>", line 1, column 13:
    {"key": "hi\/there"}

(This may seem artificial, but it actually happened to us in the wild. A Java/Android client sends JSON to the server, which decodes with PyYAML (because some clients also send YAML). A client send a file-path, which encoded the forward-slashes as above, causing our server to reject the data.)

Comments (1)

  1. Log in to comment