Skip to content

Commit 98674cd

Browse files
committed
use bytes
1 parent d8b15f7 commit 98674cd

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

jsoncsv/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def separator_type(sep):
4949
help='expand json')
5050
@click.argument(
5151
'input',
52-
type=click.File('r'),
52+
type=click.File('r', encoding='utf-8'),
5353
default='-')
5454
@click.argument(
5555
'output',

tests/test_jsontool.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_expand_and_restore(self):
104104
class TestConvertJSON(unittest.TestCase):
105105

106106
def test_convert_expand(self):
107-
fin = io.StringIO('{"a":{"b":3}}\n{"a":{"c":4}}\n')
107+
fin = io.BytesIO(u'{"a":{"b":3}}\n{"a":{"c":4}}\n'.encode('utf-8'))
108108
fout = io.BytesIO()
109109

110110
convert_json(fin, fout, expand)
@@ -114,8 +114,19 @@ def test_convert_expand(self):
114114
fin.close()
115115
fout.close()
116116

117+
def test_convert_with_unicode(self):
118+
fin = io.BytesIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n'.encode('utf-8'))
119+
fout = io.BytesIO()
120+
121+
convert_json(fin, fout, expand)
122+
123+
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n'.encode('utf-8'), fout.getvalue())
124+
125+
fin.close()
126+
fout.close()
127+
117128
def test_convert_restore(self):
118-
fin = io.StringIO('{"a.b": 3}\n{"a.c": 4}\n')
129+
fin = io.BytesIO(u'{"a.b": 3}\n{"a.c": 4}\n'.encode('utf-8'))
119130
fout = io.BytesIO()
120131

121132
convert_json(fin, fout, restore)
@@ -126,7 +137,7 @@ def test_convert_restore(self):
126137
fout.close()
127138

128139
def test_convert_expand_json_array(self):
129-
fin = io.StringIO('[{"a":{"b":3}},{"a":{"c":4}}]')
140+
fin = io.BytesIO(u'[{"a":{"b":3}},{"a":{"c":4}}]'.encode('utf-8'))
130141
fout = io.BytesIO()
131142

132143
convert_json(fin, fout, expand, json_array=True)

0 commit comments

Comments
 (0)