import { JsonStringifyStream } from "https://deno.land/std@0.181.0/encoding/json/stream.ts";
Convert each chunk to JSON string.
This can be used to stringify JSON lines, NDJSON, JSON Text Sequences, and Concatenated JSON. You can optionally specify a prefix and suffix for each chunk. The default prefix is "" and the default suffix is "\n".
Examples
Example 1
Example 1
import { readableStreamFromIterable } from "https://deno.land/std@0.181.0/streams/readable_stream_from_iterable.ts";
import { JsonStringifyStream } from "https://deno.land/std@0.181.0/json/json_stringify_stream.ts";
const file = await Deno.open("./tmp.jsonl", { create: true, write: true });
readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
.pipeThrough(new JsonStringifyStream()) // convert to JSON lines (ndjson)
.pipeThrough(new TextEncoderStream()) // convert a string to a Uint8Array
.pipeTo(file.writable)
.then(() => console.log("write success"));
To convert to JSON Text Sequences, set the
prefix to the delimiter "\x1E" as options.
To convert to JSON Text Sequences, set the prefix to the delimiter "\x1E" as options.
import { readableStreamFromIterable } from "https://deno.land/std@0.181.0/streams/readable_stream_from_iterable.ts";
import { JsonStringifyStream } from "https://deno.land/std@0.181.0/json/json_stringify_stream.ts";
const file = await Deno.open("./tmp.jsonl", { create: true, write: true });
readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
.pipeThrough(new JsonStringifyStream({ prefix: "\x1E", suffix: "\n" })) // convert to JSON Text Sequences
.pipeThrough(new TextEncoderStream())
.pipeTo(file.writable)
.then(() => console.log("write success"));
If you want to stream JSON lines from the server:
If you want to stream JSON lines from the server:
import { serve } from "https://deno.land/std@0.181.0/http/server.ts";
import { JsonStringifyStream } from "https://deno.land/std@0.181.0/json/json_stringify_stream.ts";
// A server that streams one line of JSON every second
serve(() => {
let intervalId: number | undefined;
const readable = new ReadableStream({
start(controller) {
// enqueue data once per second
intervalId = setInterval(() => {
controller.enqueue({ now: new Date() });
}, 1000);
},
cancel() {
clearInterval(intervalId);
},
});
const body = readable
.pipeThrough(new JsonStringifyStream()) // convert data to JSON lines
.pipeThrough(new TextEncoderStream()); // convert a string to a Uint8Array
return new Response(body);
});
Constructors
new
JsonStringifyStream(unnamed 0?: StringifyStreamOptions)