This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Specialize exceptions thrown by JSON parser
Type: enhancement Stage: resolved
Components: Extension Modules, Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Arfrever, bob.ippolito, cvrebert, ezio.melotti, musically_ut, pitrou, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: needs review, patch

Created on 2013-10-23 11:22 by musically_ut, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
json_JSONDecodeError.patch serhiy.storchaka, 2014-10-05 10:16 review
json_JSONDecodeError_2.patch serhiy.storchaka, 2014-11-30 22:20 review
Messages (9)
msg201015 - (view) Author: Utkarsh Upadhyay (musically_ut) * Date: 2013-10-23 11:22
The JSON parser currently throws exceptions which contain information about where the error happened but this information is encoded in a string and not available to the programmer. This leads to issues like this: http://stackoverflow.com/questions/19519409/how-to-get-error-location-from-json-loads-in-python

Would it be a problem if the errors thrown were specialized exceptions, e.g. JSONParsingError, which has ValueError as a base class with these details (line number, char number, etc.) exposed as public members? The changes required seem to be limited to changing http://hg.python.org/cpython/file/4c4f31a1b706/Lib/json/decoder.py and the related documentation.
msg201025 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-23 13:56
Simplejson already have JSONDecodeError.

class JSONDecodeError(ValueError):
    """Subclass of ValueError with the following additional properties:

    msg: The unformatted error message
    doc: The JSON document being parsed
    pos: The start index of doc where parsing failed
    end: The end index of doc where parsing failed (may be None)
    lineno: The line corresponding to pos
    colno: The column corresponding to pos
    endlineno: The line corresponding to end (may be None)
    endcolno: The column corresponding to end (may be None)

    """

Adding it to the stdlib is only matter of time.
msg201028 - (view) Author: Utkarsh Upadhyay (musically_ut) * Date: 2013-10-23 14:06
Excellent!

Is there an issue for merging Simplejson into stdlib?
If so, can this issue be linked to that issue?
msg218930 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-22 22:58
> Adding it to the stdlib is only matter of time.

I think we can go forward with it right now.
msg218935 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2014-05-22 23:20
simplejson v3.5.1 is probably a good target, let me know if there's anything that you'd like me to merge back in from stdlib to make maintenance easier. I haven't been tracking stdlib except when I've been added to issues.
msg228555 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-05 10:15
Here is a patch which adopts JSONDecodeError from simplejson.

Unlike to simplejson it is located in the json.decoder module (json uses different way to pass an error from scanner to decoder).
msg231501 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-21 21:17
While we here may be change error messages?

The only error which contains end position is "Extra data". And it doesn't look clean to me why end position should be mentioned at all (it is always len(data) here). I suggest to drop it, this will make error message cleaner and JSONDecodeError simpler.

May be mention line and column in error message only if JSON data is multiline? Most machine generated data has one line only. Instead of "Expecting ',' delimiter: line 1 column 123 (char 124)" I suggest "Expecting ',' delimiter at position 124". Same as in re.error.
msg231907 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-30 22:20
Updated patch provides JSONDecodeError without end* attributes and with changed message for "Extra data".
msg234734 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-01-26 11:17
New changeset 07af9847dbec by Serhiy Storchaka in branch 'default':
Issue #19361: JSON decoder now raises JSONDecodeError instead of ValueError.
https://hg.python.org/cpython/rev/07af9847dbec
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63560
2015-01-26 11:17:42python-devsetnosy: + python-dev
messages: + msg234734
2015-01-26 11:17:41serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-12-02 17:04:35Arfreversetnosy: + Arfrever
2014-11-30 22:20:31serhiy.storchakasetfiles: + json_JSONDecodeError_2.patch

messages: + msg231907
2014-11-21 21:17:59serhiy.storchakasetmessages: + msg231501
2014-10-05 10:16:33serhiy.storchakasetfiles: + json_JSONDecodeError.patch
keywords: + patch
2014-10-05 10:15:47serhiy.storchakasetversions: + Python 3.5, - Python 3.4
messages: + msg228555

assignee: serhiy.storchaka
keywords: + needs review
stage: patch review
2014-05-22 23:20:37bob.ippolitosetmessages: + msg218935
2014-05-22 22:58:39rhettingersetnosy: + bob.ippolito
messages: + msg218930
2014-05-16 04:32:14cvrebertsetnosy: + cvrebert
2013-10-23 14:06:43musically_utsetmessages: + msg201028
2013-10-23 13:56:31serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg201025
2013-10-23 11:25:09ezio.melottisetnosy: + rhettinger, pitrou, ezio.melotti

components: + Library (Lib)
versions: + Python 3.4
2013-10-23 11:22:14musically_utcreate