vaidik / commentjson

Add JavaScript or Python style comments in JSON.
MIT License
104 stars 25 forks source link

A quote as the last character in a comment crashes the parser: #10

Closed gonvaled closed 5 years ago

gonvaled commented 9 years ago

A quote as the last character in a comment (maybe followed by a space) will crash the parser:

{
    "a" : "b" # this is a quote: "
}
Traceback (most recent call last):
  File "xxx.py", line 8, in <module>
    print commentjson.loads(a)
  File ".../local/lib/python2.7/site-packages/commentjson/commentjson.py", line 63, in loads
    raise JSONLibraryException(e.message)
commentjson.commentjson.JSONLibraryException: JSON Library Exception

Exception thrown by JSON library (json): Expecting , delimiter: line 2 column 15 (char 16)

    Traceback (most recent call last):
      File ".../local/lib/python2.7/site-packages/commentjson/commentjson.py", line 61, in loads
        return json.loads('\n'.join(lines), **kwargs)
      File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Expecting , delimiter: line 2 column 15 (char 16)

This was difficult to find, specially because the line numbers reported do not make sense (they are line numbers after removing the comments, which does not correlate in the general case with the source file, specially if - like I do - you have lots of blank lines or lines with comments-only)

paskal commented 9 years ago

Stepped onto it too.

vaidik commented 5 years ago

Sorry for getting around to this so late. I was not maintaining commentjson in between. Got really busy with my day job. I am trying to maintain this now regularly. So expect faster responses.

This is fixed. You can try the latest version of commentjson which addresses this and a lot of other unhandled cases like this.