Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(26410)

Side by Side Diff: Lib/json/__init__.py

Issue 19361: Specialize exceptions thrown by JSON parser
Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/json/decoder.py ('k') | Lib/test/test_json/__init__.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of 1 r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of
2 JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data 2 JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
3 interchange format. 3 interchange format.
4 4
5 :mod:`json` exposes an API familiar to users of the standard library 5 :mod:`json` exposes an API familiar to users of the standard library
6 :mod:`marshal` and :mod:`pickle` modules. It is derived from a 6 :mod:`marshal` and :mod:`pickle` modules. It is derived from a
7 version of the externally maintained simplejson library. 7 version of the externally maintained simplejson library.
8 8
9 Encoding basic Python object hierarchies:: 9 Encoding basic Python object hierarchies::
10 10
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 $ echo '{"json":"obj"}' | python -m json.tool 91 $ echo '{"json":"obj"}' | python -m json.tool
92 { 92 {
93 "json": "obj" 93 "json": "obj"
94 } 94 }
95 $ echo '{ 1.2:3.4}' | python -m json.tool 95 $ echo '{ 1.2:3.4}' | python -m json.tool
96 Expecting property name enclosed in double quotes: line 1 column 3 (char 2) 96 Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
97 """ 97 """
98 __version__ = '2.0.9' 98 __version__ = '2.0.9'
99 __all__ = [ 99 __all__ = [
100 'dump', 'dumps', 'load', 'loads', 100 'dump', 'dumps', 'load', 'loads',
101 'JSONDecoder', 'JSONEncoder', 101 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
102 ] 102 ]
103 103
104 __author__ = 'Bob Ippolito <bob@redivi.com>' 104 __author__ = 'Bob Ippolito <bob@redivi.com>'
105 105
106 from .decoder import JSONDecoder 106 from .decoder import JSONDecoder, JSONDecodeError
107 from .encoder import JSONEncoder 107 from .encoder import JSONEncoder
108 108
109 _default_encoder = JSONEncoder( 109 _default_encoder = JSONEncoder(
110 skipkeys=False, 110 skipkeys=False,
111 ensure_ascii=True, 111 ensure_ascii=True,
112 check_circular=True, 112 check_circular=True,
113 allow_nan=True, 113 allow_nan=True,
114 indent=None, 114 indent=None,
115 separators=None, 115 separators=None,
116 default=None, 116 default=None,
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` 304 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
305 kwarg; otherwise ``JSONDecoder`` is used. 305 kwarg; otherwise ``JSONDecoder`` is used.
306 306
307 The ``encoding`` argument is ignored and deprecated. 307 The ``encoding`` argument is ignored and deprecated.
308 308
309 """ 309 """
310 if not isinstance(s, str): 310 if not isinstance(s, str):
311 raise TypeError('the JSON object must be str, not {!r}'.format( 311 raise TypeError('the JSON object must be str, not {!r}'.format(
312 s.__class__.__name__)) 312 s.__class__.__name__))
313 if s.startswith(u'\ufeff'): 313 if s.startswith(u'\ufeff'):
314 raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)") 314 raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
315 s, 0)
315 if (cls is None and object_hook is None and 316 if (cls is None and object_hook is None and
316 parse_int is None and parse_float is None and 317 parse_int is None and parse_float is None and
317 parse_constant is None and object_pairs_hook is None and not kw): 318 parse_constant is None and object_pairs_hook is None and not kw):
318 return _default_decoder.decode(s) 319 return _default_decoder.decode(s)
319 if cls is None: 320 if cls is None:
320 cls = JSONDecoder 321 cls = JSONDecoder
321 if object_hook is not None: 322 if object_hook is not None:
322 kw['object_hook'] = object_hook 323 kw['object_hook'] = object_hook
323 if object_pairs_hook is not None: 324 if object_pairs_hook is not None:
324 kw['object_pairs_hook'] = object_pairs_hook 325 kw['object_pairs_hook'] = object_pairs_hook
325 if parse_float is not None: 326 if parse_float is not None:
326 kw['parse_float'] = parse_float 327 kw['parse_float'] = parse_float
327 if parse_int is not None: 328 if parse_int is not None:
328 kw['parse_int'] = parse_int 329 kw['parse_int'] = parse_int
329 if parse_constant is not None: 330 if parse_constant is not None:
330 kw['parse_constant'] = parse_constant 331 kw['parse_constant'] = parse_constant
331 return cls(**kw).decode(s) 332 return cls(**kw).decode(s)
OLDNEW
« no previous file with comments | « Lib/json/decoder.py ('k') | Lib/test/test_json/__init__.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+