std/streams/to_json.ts
Yoshiya Hinosawa 0ce9c2bf7e
experiment
2024-01-31 18:10:15 +09:00

24 lines
660 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { toText } from "./to_text.ts";
/**
* Converts a JSON-formatted {@linkcode ReadableSteam} of strings or
* {@linkcode Uint8Array}s to an object. Works the same as
* {@linkcode Response.json}.
*
* @example
* ```ts
* import { toJson } from "@std/streams/to_json";
*
* const stream = ReadableStream.from([JSON.stringify({ hello: "world" })]);
* await toJson(stream); // { hello: "world" }
* ```
*/
export function toJson(
readableStream: ReadableStream,
): Promise<unknown> {
return toText(readableStream).then(JSON.parse);
}