Skip to content
Snippets Groups Projects
Commit a55a39e0 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Merge branch 'rf/strict-json' into 'master'

Rf/strict json

See merge request fsl/fslpy!312
parents 48984e01 8aa30d0e
No related branches found
No related tags found
No related merge requests found
Pipeline #11387 canceled
......@@ -171,6 +171,12 @@ test:3.9:
<<: *test_template
test:3.10:
stage: test
image: pauldmccarthy/fsleyes-py310-wxpy4-gtk3
<<: *test_template
test:build-pypi-dist:
stage: test
image: pauldmccarthy/fsleyes-py37-wxpy4-gtk3
......
......@@ -2,6 +2,18 @@ This document contains the ``fslpy`` release history in reverse chronological
order.
3.8.0 (Under development)
-------------------------
Changed
^^^^^^^
* BIDS and ``dcm2niix`` ``.json`` sidecar files with control characters
are now accepted.
3.7.0 (Friday 20th August 2021)
-------------------------------
......
......@@ -19,7 +19,7 @@ programming library written in Python. It is used by `FSLeyes
<https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/>`_.
``fslpy`` is tested against Python versions 3.7, 3.8 and 3.9.
``fslpy`` is tested against Python versions 3.7, 3.8, 3.9, and 3.10.
Installation
......
......@@ -1569,7 +1569,7 @@ def loadMetadata(image):
jsonfile = op.join(dirname, '{}.json'.format(basename))
if op.exists(jsonfile):
with open(jsonfile, 'rt') as f:
return json.load(f)
return json.load(f, strict=False)
return {}
......
......@@ -187,7 +187,7 @@ def isBIDSFile(filename, strict=True):
def loadMetadataFile(filename):
"""Load ``filename`` (assumed to be JSON), returning its contents. """
with open(filename, 'rt') as f:
return json.load(f)
return json.load(f, strict=False)
def loadMetadata(filename):
......
......@@ -110,6 +110,21 @@ def test_loadMetadata():
assert fslbids.loadMetadata(t1) == {**meta4, **meta2, **meta1}
def test_loadMetadata_control_characters():
dd = Path('dataset_description.json')
t1 = Path('sub-01/func/sub-01_task-stim_bold.nii.gz')
json1 = Path('sub-01/func/sub-01_task-stim_bold.json')
meta1 = {"a" : "1", "b" : "2\x19\x20"}
smeta1 = '{"a" : "1", "b" : "2\x19\x20"}'
with tempdir():
dd.touch()
Path(op.dirname(t1)).mkdir(parents=True)
t1.touch()
assert fslbids.loadMetadata(t1) == {}
json1.write_text(smeta1)
assert fslbids.loadMetadata(t1) == meta1
def test_loadMetadata_symlinked():
ddreal = Path('a')
......
......@@ -1484,6 +1484,21 @@ def test_loadMeta_badJSON():
assert list(img.metaKeys()) == []
def test_loadMeta_control_characters():
with tempdir():
make_image('image.nii.gz')
with open('image.json', 'wt') as f:
f.write('{"a" : 1, "b" : "abc\x19\x1b"}')
# bad json should not cause failure
img = fslimage.Image('image.nii.gz', loadMeta=True)
assert list(img.metaKeys()) == ['a', 'b']
assert img.getMeta('a') == 1
assert img.getMeta('b') == 'abc\x19\x1b'
def test_loadMetadata():
with tempdir():
make_image('image.nii.gz')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment