Skip to content

Commit 29fdbc3

Browse files
add serialize_method parameter in jsonline io methods
1 parent a507ec3 commit 29fdbc3

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

pysenal/io/file.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ def read_jsonline_lazy(filename, encoding=_ENCODING_UTF8, default=None):
198198
file.close()
199199

200200

201-
def write_jsonline(filename, items, encoding=_ENCODING_UTF8):
201+
def write_jsonline(filename, items, encoding=_ENCODING_UTF8, serialize_method=None):
202202
"""
203203
write items to file with json line format
204204
:param filename: destination file path
205205
:param items: items to be saved line by line
206206
:param encoding: file encoding
207+
:param serialize_method: serialization method to process object
207208
:return: None
208209
"""
209210
if isinstance(items, str):
@@ -216,7 +217,7 @@ def write_jsonline(filename, items, encoding=_ENCODING_UTF8):
216217
raise TypeError('items can\'t be iterable')
217218
file = open(filename, 'w', encoding=encoding)
218219
for item in items:
219-
file.write(json.dumps(item, ensure_ascii=False) + '\n')
220+
file.write(json.dumps(item, ensure_ascii=False, default=serialize_method) + '\n')
220221
file.close()
221222

222223

@@ -274,29 +275,31 @@ def append_lines(filename, lines, remove_file=False, encoding=_ENCODING_UTF8):
274275
append_line(filename, line, encoding)
275276

276277

277-
def append_jsonline(filename, item, encoding=_ENCODING_UTF8):
278+
def append_jsonline(filename, item, encoding=_ENCODING_UTF8, serialize_method=None):
278279
"""
279280
append item as a line of json string to file
280281
:param filename: destination file
281282
:param item: item to be saved
282283
:param encoding: file encoding
284+
:param serialize_method: serialization method to process object
283285
:return: None
284286
"""
285287
with open(filename, 'a', encoding=encoding) as f:
286-
f.write(json.dumps(item, ensure_ascii=False) + '\n')
288+
f.write(json.dumps(item, ensure_ascii=False, default=serialize_method) + '\n')
287289

288290

289-
def append_jsonlines(filename, items, encoding=_ENCODING_UTF8):
291+
def append_jsonlines(filename, items, encoding=_ENCODING_UTF8, serialize_method=None):
290292
"""
291293
append item as some lines of json string to file
292294
:param filename: destination file
293295
:param items: items to be saved
294296
:param encoding: file encoding
297+
:param serialize_method: serialization method to process object
295298
:return: None
296299
"""
297300
with open(filename, 'a', encoding=encoding) as f:
298301
for item in items:
299-
f.write(json.dumps(item, ensure_ascii=False) + '\n')
302+
f.write(json.dumps(item, ensure_ascii=False, default=serialize_method) + '\n')
300303

301304

302305
class __BaseFile(object):

tests/io/test_file.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# -*- coding: UTF-8 -*-
22
import tempfile
33
import pytest
4+
from json import JSONDecodeError
5+
from decimal import Decimal
46
from pysenal.io.file import *
7+
from pysenal.utils import json_serialize
58
from tests import TEST_DATA_DIR
69

710

@@ -86,6 +89,8 @@ def test_write_lines(example_lines):
8689
def test_jsonline(example_json):
8790
dirname = tempfile.gettempdir() + '/'
8891
filename = dirname + 'a.jsonl'
92+
if os.path.exists(filename):
93+
os.remove(filename)
8994
write_jsonline(filename, example_json)
9095
data = [json.loads(line) for line in read_lines(filename)]
9196
assert example_json == data
@@ -107,6 +112,14 @@ def test_jsonline(example_json):
107112
if os.path.exists(filename):
108113
os.remove(filename)
109114

115+
obj = {'n': Decimal('11.1'), 'm': Decimal('11')}
116+
with pytest.raises(TypeError):
117+
append_jsonline(filename, obj)
118+
append_jsonline(filename, obj, serialize_method=json_serialize)
119+
assert read_json(filename) == {'n': '11.1', 'm': '11'}
120+
if os.path.exists(filename):
121+
os.remove(filename)
122+
110123

111124
def test_text_file(example_lines):
112125
true_text = '\n'.join(example_lines)

0 commit comments

Comments
 (0)