Skip to content

Commit 3dc525f

Browse files
fix: make DocList.to_json() return str instead of bytes (#1769)
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
1 parent cc2339d commit 3dc525f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

docarray/array/doc_list/io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def _write_bytes(
183183
elif protocol == 'pickle-array':
184184
f.write(pickle.dumps(self))
185185
elif protocol == 'json-array':
186-
f.write(self.to_json())
186+
f.write(self.to_json().encode())
187187
elif protocol in SINGLE_PROTOCOLS:
188188
f.write(
189189
b''.join(
@@ -327,11 +327,11 @@ def from_json(
327327
json_docs = orjson.loads(file)
328328
return cls([cls.doc_type(**v) for v in json_docs])
329329

330-
def to_json(self) -> bytes:
330+
def to_json(self) -> str:
331331
"""Convert the object into JSON bytes. Can be loaded via `.from_json`.
332332
:return: JSON serialization of `DocList`
333333
"""
334-
return orjson_dumps(self)
334+
return orjson_dumps(self).decode('UTF-8')
335335

336336
@classmethod
337337
def from_csv(

docs/user_guide/sending/serialization.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ dl = DocList[SimpleDoc]([SimpleDoc(text=f'doc {i}') for i in range(2)])
7575
with open('simple-dl.json', 'wb') as f:
7676
json_dl = dl.to_json()
7777
print(json_dl)
78-
f.write(json_dl)
78+
f.write(json_dl.encode())
7979

8080
with open('simple-dl.json', 'r') as f:
8181
dl_load_from_json = DocList[SimpleDoc].from_json(f.read())
8282
print(dl_load_from_json)
8383
```
8484

8585
```output
86-
b'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]'
86+
'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]'
8787
```
8888

8989
### Protobuf
@@ -277,15 +277,15 @@ dv = DocVec[SimpleDoc](
277277
with open('simple-dv.json', 'wb') as f:
278278
json_dv = dv.to_json()
279279
print(json_dv)
280-
f.write(json_dv)
280+
f.write(json_dv.encode())
281281

282282
with open('simple-dv.json', 'r') as f:
283283
dv_load_from_json = DocVec[SimpleDoc].from_json(f.read(), tensor_type=TorchTensor)
284284
print(dv_load_from_json)
285285
```
286286

287287
```output
288-
b'{"tensor_columns":{},"doc_columns":{},"docs_vec_columns":{},"any_columns":{"id":["005a208a0a9a368c16bf77913b710433","31d65f02cb94fc9756c57b0dbaac3a2c"],"text":["doc 0","doc 1"]}}'
288+
'{"tensor_columns":{},"doc_columns":{},"docs_vec_columns":{},"any_columns":{"id":["005a208a0a9a368c16bf77913b710433","31d65f02cb94fc9756c57b0dbaac3a2c"],"text":["doc 0","doc 1"]}}'
289289
<DocVec[SimpleDoc] (length=2)>
290290
```
291291

tests/units/array/test_array_from_to_json.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def _rand_vec_gen(tensor_type):
7878
return vec
7979

8080
v = generate_docs(tensor_type)
81-
bytes_ = v.to_json()
81+
json_str = v.to_json()
8282

83-
v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=tensor_type)
83+
v_after = DocVec[v.doc_type].from_json(json_str, tensor_type=tensor_type)
8484

8585
assert v_after.tensor_type == v.tensor_type
8686
assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys())
@@ -125,9 +125,9 @@ class MyDoc(BaseDoc):
125125
return vec
126126

127127
v = generate_docs()
128-
bytes_ = v.to_json()
128+
json_str = v.to_json()
129129

130-
v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=TensorFlowTensor)
130+
v_after = DocVec[v.doc_type].from_json(json_str, tensor_type=TensorFlowTensor)
131131

132132
assert v_after.tensor_type == v.tensor_type
133133
assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys())

0 commit comments

Comments
 (0)