mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
BREAKING(std/fs): remove readJson and readJsonSync (#7255)
This commit is contained in:
parent
1cd2267500
commit
d4b6b25def
4 changed files with 0 additions and 151 deletions
|
@ -118,17 +118,6 @@ copySync("./foo", "./existingFolder", { overwrite: true });
|
|||
// Will overwrite existingFolder
|
||||
```
|
||||
|
||||
### readJson
|
||||
|
||||
Reads a JSON file and then parses it into an object
|
||||
|
||||
```ts
|
||||
import { readJson, readJsonSync } from "https://deno.land/std/fs/mod.ts";
|
||||
|
||||
const f = await readJson("./foo.json");
|
||||
const foo = readJsonSync("./foo.json");
|
||||
```
|
||||
|
||||
### writeJson
|
||||
|
||||
Writes an object to a JSON file.
|
||||
|
|
|
@ -8,7 +8,6 @@ export * from "./exists.ts";
|
|||
export * from "./expand_glob.ts";
|
||||
export * from "./move.ts";
|
||||
export * from "./copy.ts";
|
||||
export * from "./read_json.ts";
|
||||
export * from "./write_json.ts";
|
||||
export * from "./walk.ts";
|
||||
export * from "./eol.ts";
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
/** Reads a JSON file and then parses it into an object */
|
||||
export async function readJson(filePath: string): Promise<unknown> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads a JSON file and then parses it into an object */
|
||||
export function readJsonSync(filePath: string): unknown {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import {
|
||||
assertEquals,
|
||||
assertThrowsAsync,
|
||||
assertThrows,
|
||||
} from "../testing/asserts.ts";
|
||||
import * as path from "../path/mod.ts";
|
||||
import { readJson, readJsonSync } from "./read_json.ts";
|
||||
|
||||
const testdataDir = path.resolve("fs", "testdata");
|
||||
|
||||
Deno.test("readJsonFileNotExists", async function (): Promise<void> {
|
||||
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
|
||||
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
await readJson(emptyJsonFile);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("readEmptyJsonFile", async function (): Promise<void> {
|
||||
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
|
||||
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
await readJson(emptyJsonFile);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("readInvalidJsonFile", async function (): Promise<void> {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
|
||||
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
await readJson(invalidJsonFile);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("readValidArrayJsonFile", async function (): Promise<void> {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
|
||||
|
||||
const json = await readJson(invalidJsonFile);
|
||||
|
||||
assertEquals(json, ["1", "2", "3"]);
|
||||
});
|
||||
|
||||
Deno.test("readValidObjJsonFile", async function (): Promise<void> {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
|
||||
|
||||
const json = await readJson(invalidJsonFile);
|
||||
|
||||
assertEquals(json, { key1: "value1", key2: "value2" });
|
||||
});
|
||||
|
||||
Deno.test("readValidObjJsonFileWithRelativePath", async function (): Promise<
|
||||
void
|
||||
> {
|
||||
const json = await readJson("./fs/testdata/json_valid_obj.json");
|
||||
|
||||
assertEquals(json, { key1: "value1", key2: "value2" });
|
||||
});
|
||||
|
||||
Deno.test("readJsonFileNotExistsSync", function (): void {
|
||||
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
|
||||
|
||||
assertThrows((): void => {
|
||||
readJsonSync(emptyJsonFile);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("readEmptyJsonFileSync", function (): void {
|
||||
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
|
||||
|
||||
assertThrows((): void => {
|
||||
readJsonSync(emptyJsonFile);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("readInvalidJsonFile", function (): void {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
|
||||
|
||||
assertThrows((): void => {
|
||||
readJsonSync(invalidJsonFile);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("readValidArrayJsonFileSync", function (): void {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
|
||||
|
||||
const json = readJsonSync(invalidJsonFile);
|
||||
|
||||
assertEquals(json, ["1", "2", "3"]);
|
||||
});
|
||||
|
||||
Deno.test("readValidObjJsonFileSync", function (): void {
|
||||
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
|
||||
|
||||
const json = readJsonSync(invalidJsonFile);
|
||||
|
||||
assertEquals(json, { key1: "value1", key2: "value2" });
|
||||
});
|
||||
|
||||
Deno.test("readValidObjJsonFileSyncWithRelativePath", function (): void {
|
||||
const json = readJsonSync("./fs/testdata/json_valid_obj.json");
|
||||
|
||||
assertEquals(json, { key1: "value1", key2: "value2" });
|
||||
});
|
Loading…
Reference in a new issue