Commit f9e1f112 authored by Petri Lehtinen's avatar Petri Lehtinen

#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions

parent 20101871
...@@ -127,11 +127,14 @@ Basic Usage ...@@ -127,11 +127,14 @@ Basic Usage
:class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
:exc:`TypeError`. :exc:`TypeError`.
If *ensure_ascii* is ``False`` (default: ``True``), then some chunks written If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
to *fp* may be :class:`unicode` instances, subject to normal Python output are escaped with ``\uXXXX`` sequences, and the result is a
:class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()`` :class:`str` instance consisting of ASCII characters only. If
explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this *ensure_ascii* is ``False``, some chunks written to *fp* may be
is likely to cause an error. :class:`unicode` instances. This usually happens because the input contains
unicode strings or the *encoding* parameter is used. Unless ``fp.write()``
explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`)
this is likely to cause an error.
If *check_circular* is ``False`` (default: ``True``), then the circular If *check_circular* is ``False`` (default: ``True``), then the circular
reference check for container types will be skipped and a circular reference reference check for container types will be skipped and a circular reference
...@@ -168,11 +171,11 @@ Basic Usage ...@@ -168,11 +171,11 @@ Basic Usage
.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]]) .. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
Serialize *obj* to a JSON formatted :class:`str`. Serialize *obj* to a JSON formatted :class:`str`. If *ensure_ascii* is
``False``, the result may contain non-ASCII characters and the return value
may be a :class:`unicode` instance.
If *ensure_ascii* is ``False``, then the return value will be a The arguments have the same meaning as in :func:`dump`.
:class:`unicode` instance. The other arguments have the same meaning as in
:func:`dump`.
.. note:: .. note::
...@@ -371,9 +374,12 @@ Encoders and Decoders ...@@ -371,9 +374,12 @@ Encoders and Decoders
attempt encoding of keys that are not str, int, long, float or None. If attempt encoding of keys that are not str, int, long, float or None. If
*skipkeys* is ``True``, such items are simply skipped. *skipkeys* is ``True``, such items are simply skipped.
If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
:class:`str` objects with all incoming unicode characters escaped. If output are escaped with ``\uXXXX`` sequences, and the results are
*ensure_ascii* is ``False``, the output will be a unicode object. :class:`str` instances consisting of ASCII characters only. If
*ensure_ascii* is ``False``, a result may be a :class:`unicode`
instance. This usually happens if the input contains unicode strings or the
*encoding* parameter is used.
If *check_circular* is ``True`` (the default), then lists, dicts, and custom If *check_circular* is ``True`` (the default), then lists, dicts, and custom
encoded objects will be checked for circular references during encoding to encoded objects will be checked for circular references during encoding to
......
...@@ -129,11 +129,14 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, ...@@ -129,11 +129,14 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
(``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
will be skipped instead of raising a ``TypeError``. will be skipped instead of raising a ``TypeError``.
If ``ensure_ascii`` is false, then the some chunks written to ``fp`` If ``ensure_ascii`` is true (the default), all non-ASCII characters in the
may be ``unicode`` instances, subject to normal Python ``str`` to output are escaped with ``\uXXXX`` sequences, and the result is a ``str``
``unicode`` coercion rules. Unless ``fp.write()`` explicitly instance consisting of ASCII characters only. If ``ensure_ascii`` is
understands ``unicode`` (as in ``codecs.getwriter()``) this is likely ``False``, some chunks written to ``fp`` may be ``unicode`` instances.
to cause an error. This usually happens because the input contains unicode strings or the
``encoding`` parameter is used. Unless ``fp.write()`` explicitly
understands ``unicode`` (as in ``codecs.getwriter``) this is likely to
cause an error.
If ``check_circular`` is false, then the circular reference check If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will for container types will be skipped and a circular reference will
...@@ -191,9 +194,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, ...@@ -191,9 +194,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
(``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
will be skipped instead of raising a ``TypeError``. will be skipped instead of raising a ``TypeError``.
If ``ensure_ascii`` is false, then the return value will be a If ``ensure_ascii`` is false, all non-ASCII characters are not escaped, and
``unicode`` instance subject to normal Python ``str`` to ``unicode`` the return value may be a ``unicode`` instance. See ``dump`` for details.
coercion rules instead of being escaped to an ASCII ``str``.
If ``check_circular`` is false, then the circular reference check If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will for container types will be skipped and a circular reference will
......
...@@ -107,9 +107,12 @@ class JSONEncoder(object): ...@@ -107,9 +107,12 @@ class JSONEncoder(object):
encoding of keys that are not str, int, long, float or None. If encoding of keys that are not str, int, long, float or None. If
skipkeys is True, such items are simply skipped. skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str If *ensure_ascii* is true (the default), all non-ASCII
objects with all incoming unicode characters escaped. If characters in the output are escaped with \uXXXX sequences,
ensure_ascii is false, the output will be unicode object. and the results are str instances consisting of ASCII
characters only. If ensure_ascii is False, a result may be a
unicode instance. This usually happens if the input contains
unicode strings or the *encoding* parameter is used.
If check_circular is true, then lists, dicts, and custom encoded If check_circular is true, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to objects will be checked for circular references during encoding to
......
...@@ -393,6 +393,9 @@ Build ...@@ -393,6 +393,9 @@ Build
Documentation Documentation
------------- -------------
- Issue #13769: Document the effect of ensure_ascii to the return type
of JSON decoding functions.
- Issue #14880: Fix kwargs notation in csv.reader, .writer & .register_dialect. - Issue #14880: Fix kwargs notation in csv.reader, .writer & .register_dialect.
Patch by Chris Rebert. Patch by Chris Rebert.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment