json.dump fails to dump Unicode strings
Demonstration of the problem is below. With CPython this works fine and the result is, as expected "\u00e4".
IronPython 2.7 (2.7.0.40) on .NET 4.0.30319.261
Type "help", "copyright", "credits" or "license" for more information.
import json
from StringIO import StringIO
s = StringIO()
json.dump(u'\xe4', s)
Traceback (most recent call last):
File "C:\Program Files\IronPython 2.7\Lib\json\encoder.py", line 413, in _ iterencode
File "", line 1, in
File "C:\Program Files\IronPython 2.7\Lib\json__init _ _.py", line 181, in dump
File "C:\Program Files\IronPython 2.7\Lib\json\encoder.py", line 48, in py_encode_basestring_ascii
UnicodeDecodeError: ('unknown', u'\xe4', -1, 0, '')
Work Item Details
Original CodePlex Issue: Issue 32331 Status: Active Reason Closed: Unassigned Assigned to: Unassigned Reported on: Feb 29, 2012 at 8:29 AM Reported by: pekkaklarck Updated on: Apr 2 at 11:56 AM Updated by: jdhardy
On 2014-01-06 03:09:44 UTC, paweljasinski commented:
workaround: https://github.com/paweljasinski/IronLanguages/commit/365933a00d1a1b65e7bc06ee6dec660f07d9923d
On 2014-04-01 22:04:43 UTC, robden commented:
I just hit this yesterday. Not being able to easily use unicode strings for json seems like a big usability issue. To get around the bug without modifying the standard library, I encoded all my strings to utf-8 before encoding into json. It's ugly, but it works. But it's ugly...