Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6b607d

Browse files
committed
port simplejson upgrade from the trunk #4136
json also now works only with unicode strings Patch by Antoine Pitrou; updated by me
1 parent 7255f18 commit c6b607d

15 files changed

Lines changed: 1986 additions & 934 deletions

Doc/library/json.rst

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Using json.tool from the shell to validate and pretty-print::
112112
Basic Usage
113113
-----------
114114

115-
.. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
115+
.. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, default[, **kw]]]]]]]]]])
116116

117117
Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
118118
file-like object).
@@ -122,11 +122,10 @@ Basic Usage
122122
:class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
123123
:exc:`TypeError`.
124124

125-
If *ensure_ascii* is ``False`` (default: ``True``), then some chunks written
126-
to *fp* may be :class:`unicode` instances, subject to normal Python
127-
:class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()``
128-
explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this
129-
is likely to cause an error.
125+
The :mod:`json` module always produces :class:`str` objects, not
126+
:class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
127+
input.
128+
130129

131130
If *check_circular* is ``False`` (default: ``True``), then the circular
132131
reference check for container types will be skipped and a circular reference
@@ -146,8 +145,6 @@ Basic Usage
146145
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
147146
':')`` is the most compact JSON representation.
148147

149-
*encoding* is the character encoding for str instances, default is UTF-8.
150-
151148
*default(obj)* is a function that should return a serializable version of
152149
*obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
153150

@@ -156,26 +153,17 @@ Basic Usage
156153
*cls* kwarg.
157154

158155

159-
.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
156+
.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, default[, **kw]]]]]]]]]])
160157

161-
Serialize *obj* to a JSON formatted :class:`str`.
158+
Serialize *obj* to a JSON formatted :class:`str`. The arguments have the
159+
same meaning as in :func:`dump`.
162160

163-
If *ensure_ascii* is ``False``, then the return value will be a
164-
:class:`unicode` instance. The other arguments have the same meaning as in
165-
:func:`dump`.
166161

167-
168-
.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
162+
.. function:: load(fp[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
169163

170164
Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
171165
document) to a Python object.
172166

173-
If the contents of *fp* are encoded with an ASCII based encoding other than
174-
UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
175-
Encodings that are not ASCII based (such as UCS-2) are not allowed, and
176-
should be wrapped with ``codecs.getreader(encoding)(fp)``, or simply decoded
177-
to a :class:`unicode` object and passed to :func:`loads`.
178-
179167
*object_hook* is an optional function that will be called with the result of
180168
any object literal decode (a :class:`dict`). The return value of
181169
*object_hook* will be used instead of the :class:`dict`. This feature can be used
@@ -241,7 +229,7 @@ Encoders and decoders
241229
+---------------+-------------------+
242230
| array | list |
243231
+---------------+-------------------+
244-
| string | unicode |
232+
| string | str |
245233
+---------------+-------------------+
246234
| number (int) | int |
247235
+---------------+-------------------+
@@ -257,13 +245,6 @@ Encoders and decoders
257245
It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
258246
corresponding ``float`` values, which is outside the JSON spec.
259247

260-
*encoding* determines the encoding used to interpret any :class:`str` objects
261-
decoded by this instance (UTF-8 by default). It has no effect when decoding
262-
:class:`unicode` objects.
263-
264-
Note that currently only encodings that are a superset of ASCII work, strings
265-
of other encodings should be passed in as :class:`unicode`.
266-
267248
*object_hook*, if specified, will be called with the result of every JSON
268249
object decoded and its return value will be used in place of the given
269250
:class:`dict`. This can be used to provide custom deserializations (e.g. to
@@ -298,20 +279,20 @@ Encoders and decoders
298279

299280
.. method:: decode(s)
300281

301-
Return the Python representation of *s* (a :class:`str` or
302-
:class:`unicode` instance containing a JSON document)
282+
Return the Python representation of *s* (a :class:`str` instance
283+
containing a JSON document)
303284

304285
.. method:: raw_decode(s)
305286

306-
Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
307-
beginning with a JSON document) and return a 2-tuple of the Python
308-
representation and the index in *s* where the document ended.
287+
Decode a JSON document from *s* (a :class:`str` beginning with a
288+
JSON document) and return a 2-tuple of the Python representation
289+
and the index in *s* where the document ended.
309290

310291
This can be used to decode a JSON document from a string that may have
311292
extraneous data at the end.
312293

313294

314-
.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
295+
.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, default]]]]]]]])
315296

316297
Extensible JSON encoder for Python data structures.
317298

@@ -324,7 +305,7 @@ Encoders and decoders
324305
+-------------------+---------------+
325306
| list, tuple | array |
326307
+-------------------+---------------+
327-
| str, unicode | string |
308+
| str | string |
328309
+-------------------+---------------+
329310
| int, float | number |
330311
+-------------------+---------------+
@@ -344,9 +325,9 @@ Encoders and decoders
344325
attempt encoding of keys that are not str, int, float or None. If
345326
*skipkeys* is ``True``, such items are simply skipped.
346327

347-
If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be
348-
:class:`str` objects with all incoming unicode characters escaped. If
349-
*ensure_ascii* is ``False``, the output will be a unicode object.
328+
If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
329+
have all incoming non-ASCII characters escaped. If *ensure_ascii* is
330+
``False``, these characters will be output as-is.
350331

351332
If *check_circular* is ``True`` (the default), then lists, dicts, and custom
352333
encoded objects will be checked for circular references during encoding to
@@ -376,10 +357,6 @@ Encoders and decoders
376357
otherwise be serialized. It should return a JSON encodable version of the
377358
object or raise a :exc:`TypeError`.
378359

379-
If *encoding* is not ``None``, then all input strings will be transformed
380-
into unicode using that encoding prior to JSON-encoding. The default is
381-
UTF-8.
382-
383360

384361
.. method:: default(o)
385362

0 commit comments

Comments
 (0)