From 058a65c4bd833c0c97a39667f4c154131e5c5eaa Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Fri, 1 Sep 2023 13:35:48 +0200 Subject: [PATCH 1/4] fix: make doclist to_json return str instead of bytes Signed-off-by: Johannes Messner --- docarray/array/doc_list/io.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docarray/array/doc_list/io.py b/docarray/array/doc_list/io.py index 9ddc308641..1ebd954cf1 100644 --- a/docarray/array/doc_list/io.py +++ b/docarray/array/doc_list/io.py @@ -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( From 95fc952e8f2df046991640686af227b2ef5683d4 Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Fri, 1 Sep 2023 13:36:07 +0200 Subject: [PATCH 2/4] test: adapt tests Signed-off-by: Johannes Messner --- tests/units/array/test_array_from_to_json.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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()) From 6b1e82e576a5735ccf9bbdc72a79ffb3eabc7c49 Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Fri, 1 Sep 2023 13:43:01 +0200 Subject: [PATCH 3/4] fix: convert json str to bytes where it is now necessary Signed-off-by: Johannes Messner --- docarray/array/doc_list/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docarray/array/doc_list/io.py b/docarray/array/doc_list/io.py index 1ebd954cf1..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( From 04a37977ae0101f714a24eed5b68599fa8ca2c99 Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Fri, 1 Sep 2023 14:08:34 +0200 Subject: [PATCH 4/4] docs: adapt documentation Signed-off-by: Johannes Messner --- docs/user_guide/sending/serialization.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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"]}}' ``` 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