Skip to content

Commit 05529f6

Browse files
authored
Use JSON5 to parse non-standard JSON (#2750)
1 parent 8539d34 commit 05529f6

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,7 @@
15411541
"execa": "5.1.1",
15421542
"fs-extra": "10.1.0",
15431543
"js-yaml": "4.1.0",
1544+
"json5": "^2.2.1",
15441545
"lodash.clonedeep": "4.5.0",
15451546
"lodash.get": "4.4.2",
15461547
"lodash.isempty": "4.4.0",

extension/src/cli/dvc/reader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getOptions } from './options'
1919
import { typeCheckCommands } from '..'
2020
import { MaybeConsoleError } from '../error'
2121
import { Logger } from '../../common/logger'
22+
import { parseNonStandardJson } from '../../util/json'
2223

2324
const defaultExperimentsOutput: ExperimentsOutput = {
2425
workspace: { baseline: {} }
@@ -122,7 +123,7 @@ export class DvcReader extends DvcCli {
122123
try {
123124
const output =
124125
(await this.executeDvcProcess(cwd, ...args)) || defaultValue
125-
return JSON.parse(output) as T
126+
return parseNonStandardJson(output) as T
126127
} catch (error: unknown) {
127128
const msg =
128129
(error as MaybeConsoleError).stderr || (error as Error).message

extension/src/util/json.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { parseNonStandardJson } from './json'
2+
3+
describe('parseNonStandardJson', () => {
4+
it('should parse NaN', () => {
5+
expect(parseNonStandardJson('{NotANumber:NaN}')).toStrictEqual({
6+
NotANumber: Number.NaN
7+
})
8+
})
9+
10+
it('should parse Infinity', () => {
11+
expect(
12+
parseNonStandardJson(
13+
'{negativeInfinity:-Infinity, positiveInfinity: Infinity}'
14+
)
15+
).toStrictEqual({
16+
negativeInfinity: Number.NEGATIVE_INFINITY,
17+
positiveInfinity: Number.POSITIVE_INFINITY
18+
})
19+
})
20+
})

extension/src/util/json.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import JSON5 from 'json5'
2+
3+
export const parseNonStandardJson = (str: string) => JSON5.parse(str)

0 commit comments

Comments
 (0)