Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions docarray/array/doc_list/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
T = TypeVar('T', bound='IOMixinArray')
T_doc = TypeVar('T_doc', bound=BaseDoc)

ARRAY_PROTOCOLS = {'protobuf-array', 'pickle-array'}
SINGLE_PROTOCOLS = {'pickle', 'protobuf'}
ARRAY_PROTOCOLS = {'protobuf-array', 'pickle-array', 'json-array'}
SINGLE_PROTOCOLS = {'pickle', 'protobuf', 'json'}
ALLOWED_PROTOCOLS = ARRAY_PROTOCOLS.union(SINGLE_PROTOCOLS)
ALLOWED_COMPRESSIONS = {'lz4', 'bz2', 'lzma', 'zlib', 'gzip'}

Expand Down Expand Up @@ -180,6 +180,8 @@ def _write_bytes(
f.write(self.to_protobuf().SerializePartialToString())
elif protocol == 'pickle-array':
f.write(pickle.dumps(self))
elif protocol == 'json-array':
f.write(self.to_json())
elif protocol in SINGLE_PROTOCOLS:
f.write(
b''.join(
Expand Down Expand Up @@ -575,7 +577,11 @@ def _load_binary_all(
else:
d = fp.read()

if protocol is not None and protocol in ('pickle-array', 'protobuf-array'):
if protocol is not None and protocol in (
'pickle-array',
'protobuf-array',
'json-array',
):
if _get_compress_ctx(algorithm=compress) is not None:
d = _decompress_bytes(d, algorithm=compress)
compress = None
Expand All @@ -590,6 +596,9 @@ def _load_binary_all(
elif protocol is not None and protocol == 'pickle-array':
return pickle.loads(d)

elif protocol is not None and protocol == 'json-array':
return cls.from_json(d)

# Binary format for streaming case
else:
from rich import filesize
Expand Down
4 changes: 2 additions & 2 deletions tests/units/array/test_array_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MyDoc(BaseDoc):

@pytest.mark.slow
@pytest.mark.parametrize(
'protocol', ['pickle-array', 'protobuf-array', 'protobuf', 'pickle']
'protocol', ['pickle-array', 'protobuf-array', 'protobuf', 'pickle', 'json-array']
)
@pytest.mark.parametrize('compress', ['lz4', 'bz2', 'lzma', 'zlib', 'gzip', None])
@pytest.mark.parametrize('show_progress', [False, True])
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_array_save_load_binary(protocol, compress, tmp_path, show_progress):

@pytest.mark.slow
@pytest.mark.parametrize(
'protocol', ['pickle-array', 'protobuf-array', 'protobuf', 'pickle']
'protocol', ['pickle-array', 'protobuf-array', 'protobuf', 'pickle', 'json-array']
)
@pytest.mark.parametrize('compress', ['lz4', 'bz2', 'lzma', 'zlib', 'gzip', None])
@pytest.mark.parametrize('show_progress', [False, True])
Expand Down