[New-bugs-announce] [issue22701] Write unescaped unicode characters (Japanese, Chinese, etc) in JSON module when "ensure_ascii=False"

Michael Kuss report at bugs.python.org
Wed Oct 22 20:55:23 CEST 2014


New submission from Michael Kuss:

When running the following:

>> json.dump(['name': "港区"], myfile.json, indent=4, separators=(',', ': '), ensure_ascii=False)

the function escapes the unicode, even though I have explicitly asked to not force to ascii:
\u6E2F\u533A

By changing "__init__.py" such that the fp.write call encodes the text as utf-8, the output json file displays the human-readable text required (see below).


OLD (starting line 167):

if (not skipkeys and ensure_ascii and
        check_circular and allow_nan and
        cls is None and indent is None and separators is None and
        encoding == 'utf-8' and default is None and not kw):
        iterable = _default_encoder.iterencode(obj)
    else:
        if cls is None:
            cls = JSONEncoder
        iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
            check_circular=check_circular, allow_nan=allow_nan, indent=indent,
            separators=separators, encoding=encoding,
            default=default, **kw).iterencode(obj)
for chunk in iterable:
    fp.write(chunk)


NEW:

if (not skipkeys and ensure_ascii and
        check_circular and allow_nan and
        cls is None and indent is None and separators is None and
        encoding == 'utf-8' and default is None and not kw):
        iterable = _default_encoder.iterencode(obj)
        for chunk in iterable:
            fp.write(chunk)
    else:
        if cls is None:
            cls = JSONEncoder
        iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
            check_circular=check_circular, allow_nan=allow_nan, indent=indent,
            separators=separators, encoding=encoding,
            default=default, **kw).iterencode(obj)
        for chunk in iterable:
            fp.write(chunk.encode('utf-8'))

----------
components: Extension Modules, Unicode
messages: 229830
nosy: Michael.Kuss, ezio.melotti, haypo
priority: normal
severity: normal
status: open
title: Write unescaped unicode characters (Japanese, Chinese, etc) in JSON module when "ensure_ascii=False"
type: enhancement
versions: Python 2.7, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22701>
_______________________________________


More information about the New-bugs-announce mailing list