Skip to content

Commit 993536b

Browse files
committed
use io
1 parent 468f285 commit 993536b

3 files changed

Lines changed: 23 additions & 22 deletions

File tree

jsoncsv/jsontool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def restore(expobj, separator='.', safe=False):
135135

136136

137137
def convert_json(fin, fout, func, separator=".", safe=False, json_array=False):
138+
'''
139+
ensure fin/fout is TextIO
140+
'''
141+
138142
if func not in [expand, restore]:
139143
raise ValueError("unknow convert_json type")
140144

@@ -159,9 +163,5 @@ def gen_objs_from_array():
159163
for obj in objs:
160164
new = func(obj, separator=separator, safe=safe)
161165
content = json.dumps(new, ensure_ascii=False)
162-
if PY2:
163-
fout.write(content.encode('utf-8'))
164-
fout.write('\n'.encode('utf-8'))
165-
else:
166-
fout.write(content.encode('utf-8'))
167-
fout.write(str('\n').encode('utf-8'))
166+
fout.write(content)
167+
fout.write('\n')

jsoncsv/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ def separator_type(sep):
4646
'--expand',
4747
'expand',
4848
is_flag=True,
49-
help='expand json')
49+
help='expand json (default True)')
5050
@click.argument(
5151
'input',
5252
type=click.File('r', encoding='utf-8'),
5353
default='-')
5454
@click.argument(
5555
'output',
56-
type=click.File('wb'),
56+
type=click.File('w', encoding='utf-8'),
5757
default='-')
5858
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
5959
if expand and restore:
6060
raise click.UsageError('can not choose both, default is `-e`')
6161

62-
func = jsontool.expand
63-
if restore:
62+
if not restore:
63+
func = jsontool.expand
64+
else:
6465
func = jsontool.restore
6566

6667
convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)

tests/test_jsontool.py

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

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

110110
convert_json(fin, fout, expand)
111111

112-
self.assertEqual(b'{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
112+
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
113113

114114
fin.close()
115115
fout.close()
116116

117117
def test_convert_with_unicode(self):
118-
fin = io.BytesIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n'.encode('utf-8'))
119-
fout = io.BytesIO()
118+
fin = io.StringIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n')
119+
fout = io.StringIO()
120120

121121
convert_json(fin, fout, expand)
122122

123-
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n'.encode('utf-8'), fout.getvalue())
123+
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n', fout.getvalue())
124124

125125
fin.close()
126126
fout.close()
127127

128128
def test_convert_restore(self):
129-
fin = io.BytesIO(u'{"a.b": 3}\n{"a.c": 4}\n'.encode('utf-8'))
130-
fout = io.BytesIO()
129+
fin = io.StringIO(u'{"a.b": 3}\n{"a.c": 4}\n')
130+
fout = io.StringIO()
131131

132132
convert_json(fin, fout, restore)
133133

134-
self.assertEqual(b'{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
134+
self.assertEqual('{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
135135

136136
fin.close()
137137
fout.close()
138138

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

143143
convert_json(fin, fout, expand, json_array=True)
144144

145-
self.assertEqual(b'{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
145+
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
146146

147147
fin.close()
148148
fout.close()

0 commit comments

Comments
 (0)