-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
json.ts
64 lines (58 loc) · 1.82 KB
/
json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ParserError } from "../util/errors.js";
import type { FileInfo } from "../types/index.js";
import type { Plugin } from "../types/index.js";
export default {
/**
* The order that this parser will run, in relation to other parsers.
*/
order: 100,
/**
* Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
*/
allowEmpty: true,
/**
* Determines whether this parser can parse a given file reference.
* Parsers that match will be tried, in order, until one successfully parses the file.
* Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
* every parser will be tried.
*/
canParse: ".json",
/**
* Allow JSON files with byte order marks (BOM)
*/
allowBOM: true,
/**
* Parses the given file as JSON
*/
async parse(file: FileInfo): Promise<object | undefined> {
let data = file.data;
if (Buffer.isBuffer(data)) {
data = data.toString();
}
if (typeof data === "string") {
if (data.trim().length === 0) {
return; // This mirrors the YAML behavior
} else {
try {
return JSON.parse(data);
} catch (e: any) {
if (this.allowBOM) {
try {
// find the first curly brace
const firstCurlyBrace = data.indexOf("{");
// remove any characters before the first curly brace
data = data.slice(firstCurlyBrace);
return JSON.parse(data);
} catch (e: any) {
throw new ParserError(e.message, file.url);
}
}
throw new ParserError(e.message, file.url);
}
}
} else {
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
return data as object;
}
},
} as Plugin;