Function toJson
- to
Json (stream): Promise<unknown> Parameters
- stream: ReadableStream<Uint8Array> | ReadableStream<string>
A
ReadableStreamwhose chunks compose a JSON.
Returns Promise<unknown>
A promise that resolves to the parsed JSON.
Example: Usage with a stream of strings
import { toJson } from "@std/streams/to-json";
import { assertEquals } from "@std/assert";
const stream = ReadableStream.from([
"[1, true",
', [], {}, "hello',
'", null]',
]);
assertEquals(await toJson(stream), [1, true, [], {}, "hello", null]);Example: Usage with a stream of `Uint8Array`s
import { toJson } from "@std/streams/to-json";
import { assertEquals } from "@std/assert";
const stream = ReadableStream.from([
"[1, true",
', [], {}, "hello',
'", null]',
]).pipeThrough(new TextEncoderStream());
assertEquals(await toJson(stream), [1, true, [], {}, "hello", null]);- stream: ReadableStream<Uint8Array> | ReadableStream<string>
Converts a https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON-formatted ReadableSteam of strings or Uint8Arrays to an object. Works the same as Response.json and Request.json, but also extends to support streams of strings.