Skip to content
Merged
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
6 changes: 3 additions & 3 deletions docarray/array/doc_list/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions docs/user_guide/sending/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ 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())
print(dl_load_from_json)
```

```output
b'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]'
'[{"id":"5540e72d407ae81abb2390e9249ed066","text":"doc 0"},{"id":"fbe9f80d2fa03571e899a2887af1ac1b","text":"doc 1"}]'
```

### Protobuf
Expand Down Expand Up @@ -277,15 +277,15 @@ 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)
print(dv_load_from_json)
```

```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"]}}'
<DocVec[SimpleDoc] (length=2)>
```

Expand Down
8 changes: 4 additions & 4 deletions tests/units/array/test_array_from_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down