diff --git a/docarray/array/doc_list/io.py b/docarray/array/doc_list/io.py index 9ddc308641..90b645cdad 100644 --- a/docarray/array/doc_list/io.py +++ b/docarray/array/doc_list/io.py @@ -183,7 +183,7 @@ def _write_bytes( elif protocol == 'pickle-array': f.write(pickle.dumps(self)) elif protocol == 'json-array': - f.write(self.to_json()) + f.write(self.to_json().encode()) elif protocol in SINGLE_PROTOCOLS: f.write( b''.join( @@ -327,11 +327,11 @@ def from_json( json_docs = orjson.loads(file) return cls([cls.doc_type(**v) for v in json_docs]) - def to_json(self) -> bytes: + def to_json(self) -> str: """Convert the object into JSON bytes. Can be loaded via `.from_json`. :return: JSON serialization of `DocList` """ - return orjson_dumps(self) + return orjson_dumps(self).decode('UTF-8') @classmethod def from_csv( diff --git a/docs/user_guide/sending/serialization.md b/docs/user_guide/sending/serialization.md index 145dd6befd..ddc7f827eb 100644 --- a/docs/user_guide/sending/serialization.md +++ b/docs/user_guide/sending/serialization.md @@ -75,7 +75,7 @@ dl = DocList[SimpleDoc]([SimpleDoc(text=f'doc {i}') for i in range(2)]) with open('simple-dl.json', 'wb') as f: json_dl = dl.to_json() print(json_dl) - f.write(json_dl) + f.write(json_dl.encode()) with open('simple-dl.json', 'r') as f: dl_load_from_json = DocList[SimpleDoc].from_json(f.read()) @@ -83,7 +83,7 @@ with open('simple-dl.json', 'r') as f: ``` ```output -b'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]' +'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]' ``` ### Protobuf @@ -277,7 +277,7 @@ dv = DocVec[SimpleDoc]( with open('simple-dv.json', 'wb') as f: json_dv = dv.to_json() print(json_dv) - f.write(json_dv) + f.write(json_dv.encode()) with open('simple-dv.json', 'r') as f: dv_load_from_json = DocVec[SimpleDoc].from_json(f.read(), tensor_type=TorchTensor) @@ -285,7 +285,7 @@ with open('simple-dv.json', 'r') as f: ``` ```output -b'{"tensor_columns":{},"doc_columns":{},"docs_vec_columns":{},"any_columns":{"id":["005a208a0a9a368c16bf77913b710433","31d65f02cb94fc9756c57b0dbaac3a2c"],"text":["doc 0","doc 1"]}}' +'{"tensor_columns":{},"doc_columns":{},"docs_vec_columns":{},"any_columns":{"id":["005a208a0a9a368c16bf77913b710433","31d65f02cb94fc9756c57b0dbaac3a2c"],"text":["doc 0","doc 1"]}}' ``` diff --git a/tests/units/array/test_array_from_to_json.py b/tests/units/array/test_array_from_to_json.py index 0569a56677..d1b28a6b5d 100644 --- a/tests/units/array/test_array_from_to_json.py +++ b/tests/units/array/test_array_from_to_json.py @@ -78,9 +78,9 @@ def _rand_vec_gen(tensor_type): return vec v = generate_docs(tensor_type) - bytes_ = v.to_json() + json_str = v.to_json() - v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=tensor_type) + v_after = DocVec[v.doc_type].from_json(json_str, tensor_type=tensor_type) assert v_after.tensor_type == v.tensor_type assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys()) @@ -125,9 +125,9 @@ class MyDoc(BaseDoc): return vec v = generate_docs() - bytes_ = v.to_json() + json_str = v.to_json() - v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=TensorFlowTensor) + v_after = DocVec[v.doc_type].from_json(json_str, tensor_type=TensorFlowTensor) assert v_after.tensor_type == v.tensor_type assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys()) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy