Skip to content

Commit

Permalink
reading mmJSON: don't crash on empty array (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojdyr committed Sep 19, 2024
1 parent 2fa3b93 commit 2963d1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void fill_document_from_sajson(Document& d, const sajson::document& s) {
std::to_string(arr.get_length()));
if (cif_rows == 1) {
items.emplace_back(tag, as_cif_value(arr.get_array_element(0)));
} else {
} else if (cif_rows != 0) {
Loop& loop = items.back().loop;
loop.tags.emplace_back(std::move(tag));
for (size_t k = 0; k != cif_rows; ++k)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_mol.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,21 @@ def test_previous_next_residue(self):
def test_read_1pfe_cif(self):
st = gemmi.read_structure(full_path('1pfe.cif.gz'))
self.check_1pfe(st)
mmcif_doc = st.make_mmcif_document()

# write structure to cif and read it back
# write structure to cif file and read it back
out_name = get_path_for_tempfile(suffix='.cif')
st.make_mmcif_document().write_file(out_name)
mmcif_doc.write_file(out_name)
st2 = gemmi.read_structure(out_name)
os.remove(out_name)
self.check_1pfe(st2)

# write structure to mmJSON string and read it back
json_str = mmcif_doc.as_json(mmjson=True)
doc = gemmi.cif.read_mmjson_string(json_str)
st3 = gemmi.make_structure_from_block(doc[0])
self.check_1pfe(st3)

def test_read_1pfe_json(self):
st = gemmi.read_structure(full_path('1pfe.json'))
self.check_1pfe(st)
Expand Down

0 comments on commit 2963d1f

Please sign in to comment.