-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: JSONDecodeError with valid string #57
Comments
A little exploration... https://repl.it/@altendky/OriginalMoccasinVendor-2 import json
import json_tricks
s = r'\"#'
j = json.dumps(s)
print(repr(j))
jt = json_tricks.dumps(s)
print(repr(jt))
print('json and json_tricks encoding match: {}'.format(j == jt))
print('--- encoded json')
print(j)
print('---')
print('--- json.loads(j)')
json.loads(j)
print('--- json_tricks.loads(j, ignore_comments=False)')
json_tricks.loads(j, ignore_comments=False)
print('--- json_tricks.loads(j)')
json_tricks.loads(j) '"\\\\\\"#"'
'"\\\\\\"#"'
json and json_tricks encoding match: True
--- encoded json
"\\\"#"
---
--- json.loads(j)
--- json_tricks.loads(j, ignore_comments=False)
--- json_tricks.loads(j)
Traceback (most recent call last):
File "main.py", line 25, in <module>
json_tricks.loads(j)
File "/home/runner/.local/lib/python3.6/site-packages/json_tricks/nonp.py", line 213, in loads
return json_loads(string, object_pairs_hook=hook, **jsonkwargs)
File "/usr/local/lib/python3.6/json/__init__.py", line 367, in loads
return cls(**kw).decode(s)
File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0) |
Thanks for the report and the analysis! The comment parsing currently happens with regular expressions, which aren't quite powerful enough to understand all of json. But perhaps they could be expanded to work for these cases. I'm not sure this can be solved completely generally without making a complete json parser that also understands comments. That would also help with some other issues regarding primitives, but it's a big step. As you've found you can work around it with |
@mverleg I've posted this question in #python@freenode, that's why @altendky very gently has helped here, that said, main reason why I'd like to solve this issue is because in some of the items I'm serializing/deserializing (items used in pyqt stuff) I'm storing python code as strings, please see below a little example about it:
So for my particular case I'm not really sure how I'm gonna serialize/deserialize these type of objects :/ |
@brupelo Would it help as a workaround to change
to
You won't have any comments in the json if you're dumping the data yourself, so it should be okay to ignore them, and would even be a bit faster. It's just a workaround, but I'm not sure if/when I can fix this issue. |
@mverleg Great, it seems that workaround works... :O/ Just to be extra careful, right now I've decided before serializing/dumping the state of my pyqt software to disk I'll check if Anyway, I'll leave the issue open but so far the workaround is good for me... About your previous comment:
Some solution that come to my mind... https://github.com/dmeranda/demjson, I've used this in some projects and it handles a more general format of json (like SublimeText)... pretty handy library, hope that helps |
Yeah better leave it open, it should ideally still work even when ignoring comments. In general there's no guarantee that things are encodeable and decodeable, or that those return it to the same type. For example, a numpy float gets encoded to just a number, and then there's no way to know it was a numpy type, so it gets decoded to a float. Json also doesn't view lists and tuples as different. But where possible the aim is for the build-in json-tricks types to be exactly the same after encoding and decoding. Unless The 'primitives' like lists, maps, numbers, texts and booleans encodable and decodable. Most of the extra types in this library are too. |
@brupelo By the way, after you add the |
Although this is technically a valid problem, I'm going to close it because
If someone has a good solution and is willing to do most of the work, feel free to re-open. |
@mverleg Hi Mark, nice to meet you, first of all, thanks for creating this little library, it's quite handy one... today I've found a little bug.
Could you please take a look & advice?
Using
json-tricks==3.13.1
+ python 3.6.2 + win7 over here.Ps. you can see the same data is encoded/decoded perfectly using
json
but it crashes when usingjson_tricks
The text was updated successfully, but these errors were encountered: