Skip to content
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

json.JSONDecoder() strict argument undocumented and potentially confusing #49035

Closed
beazley mannequin opened this issue Dec 30, 2008 · 6 comments
Closed

json.JSONDecoder() strict argument undocumented and potentially confusing #49035

beazley mannequin opened this issue Dec 30, 2008 · 6 comments
Assignees
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@beazley
Copy link
Mannequin

beazley mannequin commented Dec 30, 2008

BPO 4785
Nosy @birkenfeld, @taleinat
Files
  • json_docs_py3k.diff: proposed documentation patch
  • json_docs_trunk.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/birkenfeld'
    closed_at = <Date 2010-10-15.17:04:56.049>
    created_at = <Date 2008-12-30.17:42:13.633>
    labels = ['type-bug', 'docs']
    title = 'json.JSONDecoder() strict argument undocumented and potentially confusing'
    updated_at = <Date 2010-10-15.17:04:56.048>
    user = 'https://bugs.python.org/beazley'

    bugs.python.org fields:

    activity = <Date 2010-10-15.17:04:56.048>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2010-10-15.17:04:56.049>
    closer = 'georg.brandl'
    components = ['Documentation']
    creation = <Date 2008-12-30.17:42:13.633>
    creator = 'beazley'
    dependencies = []
    files = ['17560', '17561']
    hgrepos = []
    issue_num = 4785
    keywords = ['patch']
    message_count = 6.0
    messages = ['78550', '107067', '107068', '107125', '107126', '118806']
    nosy_count = 3.0
    nosy_names = ['georg.brandl', 'beazley', 'taleinat']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4785'
    versions = ['Python 2.7', 'Python 3.2']

    @beazley
    Copy link
    Mannequin Author

    beazley mannequin commented Dec 30, 2008

    The strict parameter to JSONDecoder() is undocumented and is confusing
    because someone might assume it has something to do with the encoding
    parameter or the general handling of parsing errors (which it doesn't).

    As far as I can determine by reading the source, strict determines
    whether or not JSON strings are allowed to contain literal newlines in
    them or not. For example (note: loads() passes its parameters to
    JSONDecoder):

    >>> s = '{"test":"Hello\nWorld"}'
    >>> print(s)
    {"test":"Hello
    World"}
    >>> json.loads(s)
    Traceback (most recent call last):
    ...
      File "/tmp/lib/python3.0/json/decoder.py", line 159, in JSONString
        return scanstring(match.string, match.end(), encoding, strict)
    ValueError: Invalid control character at: line 1 column 14 (char 14)
    
    >>> json.loads(s,strict=False)
    {'test': 'Hello\nWorld'}
    >>> 

    Note in this last example how the result has the literal newline
    embedded in it when strict is set False.

    @beazley beazley mannequin assigned birkenfeld Dec 30, 2008
    @beazley beazley mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Dec 30, 2008
    @taleinat
    Copy link
    Contributor

    taleinat commented Jun 4, 2010

    This goes down into _json.scanstring. Looking at the C code for scanstring_unicode, the strict parameter allow control characters inside strings: "if strict is zero then literal control characters are allowed". From the code itself (current py3k head, r81032), it seems this means any character <= 0x1f. See scanstring_unicode in http://svn.python.org/view/python/branches/py3k/Modules/_json.c?revision=81032&view=markup for details.

    Documentation should be updated accordingly.

    @taleinat
    Copy link
    Contributor

    taleinat commented Jun 4, 2010

    This goes down into _json.scanstring. Looking at the C code for scanstring_unicode, strict=False allows control characters inside strings: "if strict is zero then literal control characters are allowed". From the code itself (current py3k head, r81032), it seems this means any character <= 0x1f. See scanstring_unicode in http://svn.python.org/view/python/branches/py3k/Modules/_json.c?revision=81032&view=markup for details.

    Documentation should be updated accordingly.

    @taleinat
    Copy link
    Contributor

    taleinat commented Jun 5, 2010

    Documentation patch attached against py3k branch.

    Changes are:

    • Added to documentation of JSONDecoder:

    If strict is False (True is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including '\t' (tab), '\n', '\r' and '\0'.

    • Added clarification in documentation of json.load and json.dump that unless the cls kwarg is specified, the JSONEncoder/JSONDecoder class will be used.

    • Mirrored these additions in the relevant doc-strings (JSONDecoder.__init__, json.load, json.loads, json.dump, json.dumps).

    • Copied description of the object_pairs_hook kwargs from the documentation to the relevant doc-strings, which otherwise fully mirrored the documentation. (json.load, json.loads, JSONDecoder.__init__)

    @taleinat
    Copy link
    Contributor

    taleinat commented Jun 5, 2010

    Similar patch against trunk; same changes as for the py3k branch.

    @birkenfeld
    Copy link
    Member

    Thanks, applied in r85543 and r85544.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants