File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,11 @@ export default {
2121 */
2222 canParse : ".json" ,
2323
24+ /**
25+ * Allow JSON files with byte order marks (BOM)
26+ */
27+ allowBOM : true ,
28+
2429 /**
2530 * Parses the given file as JSON
2631 */
@@ -37,6 +42,17 @@ export default {
3742 try {
3843 return JSON . parse ( data ) ;
3944 } catch ( e : any ) {
45+ if ( this . allowBOM ) {
46+ try {
47+ // find the first curly brace
48+ const firstCurlyBrace = data . indexOf ( "{" ) ;
49+ // remove any characters before the first curly brace
50+ data = data . slice ( firstCurlyBrace ) ;
51+ return JSON . parse ( data ) ;
52+ } catch ( e : any ) {
53+ throw new ParserError ( e . message , file . url ) ;
54+ }
55+ }
4056 throw new ParserError ( e . message , file . url ) ;
4157 }
4258 }
Original file line number Diff line number Diff line change @@ -88,6 +88,13 @@ export interface Plugin {
8888 */
8989 allowEmpty ?: boolean ;
9090
91+ /**
92+ * Specifies whether a Byte Order Mark (BOM) is allowed or not. Only applies to JSON parsing
93+ *
94+ * @type {boolean } @default true
95+ */
96+ allowBOM ?: boolean ;
97+
9198 /**
9299 * The encoding that the text is expected to be in.
93100 */
You can’t perform that action at this time.
0 commit comments