Skip to content

Commit 2e6a949

Browse files
authored
Merge pull request #225 from bids-standard/enh/raise_error_on_non_object_json
Raise error when json files are parsed and their root value is an array instead of object.
2 parents 54eeee3 + 77d1696 commit 2e6a949

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Added
10+
11+
- A bullet item for the Added category.
12+
13+
-->
14+
### Changed
15+
16+
- Raise error when JSON files are parsed and their root value is anything other than an object
17+
18+
<!--
19+
### Fixed
20+
21+
- A bullet item for the Fixed category.
22+
23+
-->
24+
<!--
25+
### Deprecated
26+
27+
- A bullet item for the Deprecated category.
28+
29+
-->
30+
<!--
31+
### Removed
32+
33+
- A bullet item for the Removed category.
34+
35+
-->
36+
<!--
37+
### Security
38+
39+
- A bullet item for the Security category.
40+
41+
-->
42+
<!--
43+
### Infrastructure
44+
45+
- A bullet item for the Infrastructure category.
46+
47+
-->

src/files/json.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ async function readJSONText(file: BIDSFile): Promise<string> {
2222

2323
export async function loadJSON(file: BIDSFile): Promise<Record<string, unknown>> {
2424
const text = await readJSONText(file) // Raise encoding errors
25+
let parsedText;
2526
try {
26-
return JSON.parse(text)
27+
parsedText = JSON.parse(text)
2728
} catch (error) {
2829
throw { key: 'JSON_INVALID' } // Raise syntax errors
2930
}
31+
if (Array.isArray(parsedText) || typeof parsedText !== "object") {
32+
throw { key: 'JSON_NOT_AN_OBJECT', evidence: text.substring(0, 10) + (text.length > 10 ? '...' : '') }
33+
}
34+
return parsedText
3035
}

src/issues/list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const bidsIssues: IssueDefinitionRecord = {
99
severity: 'error',
1010
reason: 'Not a valid JSON file.',
1111
},
12+
JSON_NOT_AN_OBJECT: {
13+
severity: 'error',
14+
reason: 'Parsed JSON file does not contain an object.',
15+
},
1216
MISSING_DATASET_DESCRIPTION: {
1317
severity: 'error',
1418
reason: 'A dataset_description.json file is required in the root of the dataset',

0 commit comments

Comments
 (0)