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 {
21
21
*/
22
22
canParse : ".json" ,
23
23
24
+ /**
25
+ * Allow JSON files with byte order marks (BOM)
26
+ */
27
+ allowBOM : true ,
28
+
24
29
/**
25
30
* Parses the given file as JSON
26
31
*/
@@ -37,6 +42,17 @@ export default {
37
42
try {
38
43
return JSON . parse ( data ) ;
39
44
} 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
+ }
40
56
throw new ParserError ( e . message , file . url ) ;
41
57
}
42
58
}
Original file line number Diff line number Diff line change @@ -88,6 +88,13 @@ export interface Plugin {
88
88
*/
89
89
allowEmpty ?: boolean ;
90
90
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
+
91
98
/**
92
99
* The encoding that the text is expected to be in.
93
100
*/
You can’t perform that action at this time.
0 commit comments