Valid JSON not being loaded

Issue #11 new
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\", line 71, in load
    return loader.get_single_data()
  File "C:\Python27\lib\site-packages\yaml\", line 37, in get_single_data
    node = self.get_single_node()
  File "C:\Python27\lib\site-packages\yaml\", line 35, in get_single_node
    if not self.check_event(StreamEndEvent):
  File "C:\Python27\lib\site-packages\yaml\", line 98, in check_event
    self.current_event = self.state()
  File "C:\Python27\lib\site-packages\yaml\", line 143, in parse_implicit_document_start
  File "C:\Python27\lib\site-packages\yaml\", line 116, in check_token
  File "C:\Python27\lib\site-packages\yaml\", line 248, in fetch_more_tokens
    return self.fetch_double()
  File "C:\Python27\lib\site-packages\yaml\", line 656, in fetch_double
  File "C:\Python27\lib\site-packages\yaml\", line 667, in fetch_flow_scalar
  File "C:\Python27\lib\site-packages\yaml\", line 1156, in scan_flow_scalar
    chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
  File "C:\Python27\lib\site-packages\yaml\", 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