std/streams/to_json.ts

33 lines
928 B
TypeScript
Raw Normal View History

// 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}.
*
* @param readableStream A `ReadableStream` whose chunks compose a JSON.
* @returns A promise that resolves to the parsed JSON.
*
* @example Basic usage
* ```ts
* import { toJson } from "@std/streams/to-json";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "[1, true",
* ', [], {}, "hello',
* '", null]',
* ]);
* const json = await toJson(stream);
* assertEquals(json, [1, true, [], {}, "hello", null]);
* ```
*/
export function toJson(
readableStream: ReadableStream,
): Promise<unknown> {
return toText(readableStream).then(JSON.parse);
}