2019-03-13 12:06:40 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import * as path from "./path/mod.ts";
|
|
|
|
|
2019-03-14 10:24:54 -04:00
|
|
|
/** Reads a JSON file and then parses it into an object */
|
2019-03-13 12:06:40 -04:00
|
|
|
export async function readJson(filePath: string): Promise<any> {
|
|
|
|
filePath = path.resolve(filePath);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
|
|
const content = decoder.decode(await Deno.readFile(filePath));
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(content);
|
|
|
|
} catch (err) {
|
|
|
|
err.message = `${filePath}: ${err.message}`;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:24:54 -04:00
|
|
|
/** Reads a JSON file and then parses it into an object */
|
2019-03-13 12:06:40 -04:00
|
|
|
export function readJsonSync(filePath: string): any {
|
|
|
|
filePath = path.resolve(filePath);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
|
|
const content = decoder.decode(Deno.readFileSync(filePath));
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(content);
|
|
|
|
} catch (err) {
|
|
|
|
err.message = `${filePath}: ${err.message}`;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|