avakar / pycson

A Coffescript Object Notation (CSON) parser for Python 2 and Python 3.
Other
55 stars 7 forks source link

ValueError: Expecting , delimiter #3

Closed idleberg closed 8 years ago

idleberg commented 8 years ago

I've been trying to convert this multi-line CSON to JSON. The CSON used in the example is a snippet for Atom editor. Since Atom lints all snippets in use on startup, I don't think there's anything wrong with it.

>>> cson = """
... '.text.html':
...   'cc-by':
...     'prefix': 'cc-by'
...     'body': '<${1:i} class="cc cc-by$2"></${1:i}>'
...   'cc-cc':
...     'prefix': 'cc-cc'
...     'body': '<${1:i} class="cc cc-cc$2"></${1:i}>'
... """
>>> cson.loads(cson)

Oddly enough, the same error occurs when using python-cson instead. Any ideas?

avakar commented 8 years ago

I assume it it a copy-paste error, since you override cson module with the string (i.e. in cson.loads(cson), you mention cson twice). In any case, the following works for me.

>>> import cson
>>> t = """
... '.text.html':
...   'cc-by':
...     'prefix': 'cc-by'
...     'body': '<${1:i} class="cc cc-by$2"></${1:i}>'
...   'cc-cc':
...     'prefix': 'cc-cc'
...     'body': '<${1:i} class="cc cc-cc$2"></${1:i}>'
... """
>>> cson.loads(t)
{u'.text.html': {u'cc-cc': {u'body': u'<${1:i} class="cc cc-cc$2"></${1:i}>', u'prefix': u'cc-cc'}, u'cc-by': {u'body': u'<${1:i} class="cc cc-by$2"></${1:i}>', u'prefix': u'cc-by'}}}

If it doesn't for you, please update the cson package and if it doesn't help, post the exact error here and I'll look into it further.

idleberg commented 8 years ago

No, I only renamed my variable for this issue and didn't think of the conflicting name. Here's the full log:

>>> import cson
>>> t = """
... '.text.html':
...   'cc-by':
...     'prefix': 'cc-by'
...     'body': '<${1:i} class="cc cc-by$2"></${1:i}>'
...   'cc-cc':
...     'prefix': 'cc-cc'
...     'body': '<${1:i} class="cc cc-cc$2"></${1:i}>'
... """
>>> cson.loads(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/python_cson-1.0.9-py2.7.egg/cson.py", line 318, in csons2py
    return json.loads(toJSON(csonString))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 22 (char 21)
idleberg commented 8 years ago

Obviously, it's importing the wrong package. Sorry!

avakar commented 8 years ago

Ok, good. Note that python-cson does not actually parse Coffescript, but rather some weird extension of JSON. You should probably steer clear of it.

Let me know if I can help you further.