Skip to content

Commit

Permalink
#222 allow trailing commas and comments + log and show jsonc parse er…
Browse files Browse the repository at this point in the history
…ror message
  • Loading branch information
RandomFractals committed Sep 28, 2020
1 parent 607ce9d commit 2857f79
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/data.providers/json.data.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {window} from 'vscode';
import * as fs from 'fs';
import {
parse as jsoncParse,
printParseErrorCode,
ParseError
} from 'jsonc-parser';
import * as config from '../config';
Expand Down Expand Up @@ -38,7 +39,19 @@ export class JsonDataProvider implements IDataProvider {
try {
let content: string = String(await fileUtils.readDataFile(dataUrl, 'utf8'));
let parseErrors: ParseError[] = [];
data = jsoncParse(content, parseErrors, {disallowComments: true}); //JSON.parse(content);
data = jsoncParse(content, parseErrors, {
disallowComments: false,
allowTrailingComma: true
}); //JSON.parse(content);
if (parseErrors && parseErrors.length > 0) {
// log and display json parse errors
let jsonErrors: string = '';
for (const error of parseErrors) {
jsonErrors += `${printParseErrorCode(error.error)} at ${error.offset} of ${error.length} length \n`;
}
this.logger.logMessage(LogLevel.Error, `getData(): Error parsing '${dataUrl}' \n\t Error:`, jsonErrors);
window.showErrorMessage(`Invalid json data file: '${dataUrl}'. \n\t Error(s): \n ${jsonErrors}`);
}
}
catch (error) {
this.logger.logMessage(LogLevel.Error, `getData(): Error parsing '${dataUrl}' \n\t Error:`, error.message);
Expand Down

0 comments on commit 2857f79

Please sign in to comment.