msg80564 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2009-01-26 06:24 |
Using the wrong sort of quotes in json gives unhelpful error messages: >>> json.loads("{'test':'test'}") Traceback (most recent call last): ... ValueError: Expecting property name: line 1 column 1 (char 1) Unless you know that strings in JSON must be delimited with double-quotes and not single (a very surprising fact to those used to Python) this error message is perplexing. I suggest something like: Single-quoted strings are invalid property names: line 1 column 1 (char 1) or Parse error, invalid char: line 1, column 1 (char 1) |
|
|
msg80604 - (view) |
Author: Gabriel Genellina (ggenellina) |
Date: 2009-01-27 01:05 |
This patch provides a better error message for this case:: json.loads("""{'test': "test"}""") but still doesn't help in this one:: json.loads("""{"test": 'test'}""") 'test' looks like garbage to JSON (it *is* garbage!), exactly the same as:: json.loads("""{"test": @?&%%}""") so it's hard to provide a better message when the parser expects a generic object. |
|
|
msg82949 - (view) |
Author: Cherniavsky Beni (cben) * |
Date: 2009-03-01 00:52 |
Perhaps it should not be an error at all? The default should probably stay strict to the spec, but IMHO the module should provide an optional lenient parsing mode that also accepts single quotes. Why support single quotes and not any other imaginable deviation from the spec? Because single quotes are the only way (AFAIK) in which Python's repr() produces invalid JSON (from JSONable combinations of types). |
|
|
msg82951 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2009-03-01 01:48 |
+1 on Steven's request for a better error message. +1 on Beni's request for looser input requirements for better interoperability with Python's repr. OTOH, I've never found it hard to write: s.replace("'", '"'). |
|
|
msg82970 - (view) |
Author: Bob Ippolito (bob.ippolito) *  |
Date: 2009-03-01 18:00 |
I don't really want to see looser input requirements, making a JSON parser that is compatible with a subset of Python repr output isn't a design goal of mine. This is absolutely false: "Because single quotes are the only way (AFAIK) in which Python's repr() produces invalid JSON (from JSONable combinations of types)." >>> repr(object) "<type 'object'>" If you don't know JSON, I'm not sure throwing random input at the JSON parser is going to help you. Is that how you learned XML? There's plenty of info in the JSON documentation and a link to json.org if you need help. |
|
|
msg82971 - (view) |
Author: Bob Ippolito (bob.ippolito) *  |
Date: 2009-03-01 18:04 |
Er, sorry, missed "(from JSONable combinations of types)". It's early. Anyway, I can change the error message, but I will not make it special- case single quotes for its own error message. I will have to think about what the message could say instead. Note that the same sort of person who throws random input at parsers might even expect {test: 'test'} to work since that is valid JavaScript but not valid JSON. |
|
|
msg162878 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2012-06-15 09:30 |
Patch adapted for Python 3.3. Consistently changed messages in C code, docs and docstrings. |
|
|
msg163581 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2012-06-23 11:25 |
Any chance to commit the patch today and to get this feature in Python 3.3? |
|
|
msg163841 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2012-06-24 20:16 |
I would say this is a bugfix, so it can go in after the beta. Georg? |
|
|
msg163845 - (view) |
Author: Georg Brandl (georg.brandl) *  |
Date: 2012-06-24 20:21 |
Agreed. |
|
|
msg164304 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2012-06-29 00:03 |
New changeset 9854520c8200 by Antoine Pitrou in branch '3.2': Issue #5067: improve some json error messages. http://hg.python.org/cpython/rev/9854520c8200 New changeset 7523ab4e6e06 by Antoine Pitrou in branch 'default': Issue #5067: improve some json error messages. http://hg.python.org/cpython/rev/7523ab4e6e06 New changeset 7762816e3fcd by Antoine Pitrou in branch '2.7': Issue #5067: improve some json error messages. http://hg.python.org/cpython/rev/7762816e3fcd |
|
|
msg164305 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2012-06-29 00:04 |
Committed now. Thanks Serhiy for the patch! |
|
|