Skip to content

Commit a397bde

Browse files
committed
[#71] Output UTF-8 encoded JSON
1 parent 9a0c3b1 commit a397bde

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

flattentool/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flattentool.output import FORMATS_SUFFIX
55
from flattentool.input import FORMATS as INPUT_FORMATS
66
import json
7+
import codecs
78
from decimal import Decimal
89
from collections import OrderedDict
910

@@ -122,6 +123,6 @@ def unflatten(input_name, base_json=None, input_format=None, output_name='releas
122123
else:
123124
base = OrderedDict()
124125
base[main_sheet_name] = list(spreadsheet_input.unflatten())
125-
with open(output_name, 'w') as fp:
126-
json.dump(base, fp, indent=4, default=decimal_default)
126+
with codecs.open(output_name, 'w', encoding='utf-8') as fp:
127+
json.dump(base, fp, indent=4, default=decimal_default, ensure_ascii=False)
127128

flattentool/tests/test_init.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def test_unflatten_csv_utf8(tmpdir):
174174
main_sheet_name='main')
175175
reloaded_json = json.load(tmpdir.join('release.json'))
176176
assert reloaded_json == {'main': [{'ocid': '1', 'id': 'éαГ😼𝒞人'}]}
177+
# The JSON we output should be UTF-8, rather than escaped ASCII
178+
# https://github.com/OpenDataServices/flatten-tool/issues/71
179+
assert 'éαГ😼𝒞人' in tmpdir.join('release.json').read_text(encoding='utf-8')
177180

178181

179182
def test_unflatten_csv_latin1(tmpdir):

0 commit comments

Comments
 (0)