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

2014-10-31 Thread Michael Kuss

Michael Kuss added the comment:

Pardon the delay - this json dump function is embedded in a much larger script, 
so it took some untangling to get it running on Python 3.3, and scrub some 
personal identifying info from it. This script also does not work in Python 3.3:


  File C:/Users/mkuss/PycharmProjects/TestJSON\dump_list_to_json_file.py, 
line 319, in dump_list_to_json_file
json.dump(addresses, outfile, indent=4, separators=(',', ': '))
  File C:\Python33\lib\json\__init__.py, line 184, in dump
fp.write(chunk)
TypeError: 'str' does not support the buffer interface



In python 2.7, I still get escaped unicode when I try writing this dictionary 
using json.dump, so the work-around that I pasted originally is how I'm 
choosing to accomplish the task for now.

I'd you'd like, I can spend more time debugging this issue I'm running into 
running the script in python 3.3, but it maybe be til next week when I have 
sufficient time to solve. THANKS  --mike

--
status: pending - open

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22701
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2014-10-22 Thread Michael Kuss

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 rep...@bugs.python.org
http://bugs.python.org/issue22701
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com