-
Notifications
You must be signed in to change notification settings - Fork 633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(json): lint @std/json
docs
#4798
Changes from 13 commits
7bc9c23
15d9a6a
1e1218b
094da27
457f76d
c77b469
5d2ca8c
caf0d6f
493fa1b
6a610e5
6736ee8
0e60d0a
15c0bac
ce2d9e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,32 +12,90 @@ const primitives = new Map( | |
); | ||
|
||
/** | ||
* Stream to parse {@link https://en.wikipedia.org/wiki/JSON_streaming#Concatenated_JSON|Concatenated JSON}. | ||
* Stream to parse | ||
* {@link https://en.wikipedia.org/wiki/JSON_streaming#Concatenated_JSON | Concatenated JSON}. | ||
* | ||
* @example Usage | ||
* | ||
* @example | ||
* ```ts | ||
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream"; | ||
* import { assertEquals } from "@std/assert/assert-equals"; | ||
* | ||
* const url = "@std/json/testdata/test.concatenated-json"; | ||
* const { body } = await fetch(url); | ||
* | ||
* const readable = body! | ||
* .pipeThrough(new TextDecoderStream()) // convert Uint8Array to string | ||
* .pipeThrough(new ConcatenatedJsonParseStream()); // parse Concatenated JSON | ||
* const stream = ReadableStream.from([ | ||
* `{"foo":"bar"}`, | ||
* `{"baz":100}`, | ||
* ]).pipeThrough(new ConcatenatedJsonParseStream()); | ||
* | ||
* for await (const data of readable) { | ||
* console.log(data); | ||
* } | ||
* assertEquals(await Array.fromAsync(stream), [ | ||
* { foo: "bar" }, | ||
* { baz: 100 }, | ||
* ]); | ||
* ``` | ||
*/ | ||
export class ConcatenatedJsonParseStream | ||
implements TransformStream<string, JsonValue> { | ||
/** A writable stream of byte data. */ | ||
// TODO(iuioiua): Investigate why this class is implemented differently to the other JSON streams. | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with this for now. I didn't bother with a proper example because this, as a public property, seems like a mistake/fault, and may get removed in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To my understanding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just meant how this implements |
||
* A writable stream of byte data. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream"; | ||
* import { assertEquals } from "@std/assert/assert-equals"; | ||
* | ||
* const stream = ReadableStream.from([ | ||
* `{"foo":"bar"}`, | ||
* `{"baz":100}`, | ||
* ]).pipeThrough(new ConcatenatedJsonParseStream()); | ||
* | ||
* assertEquals(await Array.fromAsync(stream), [ | ||
* { foo: "bar" }, | ||
* { baz: 100 }, | ||
* ]); | ||
* ``` | ||
*/ | ||
readonly writable: WritableStream<string>; | ||
/** A readable stream of byte data. */ | ||
// TODO(iuioiua): Investigate why this class is implemented differently to the other JSON streams. | ||
/** | ||
* A readable stream of byte data. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream"; | ||
* import { assertEquals } from "@std/assert/assert-equals"; | ||
* | ||
* const stream = ReadableStream.from([ | ||
* `{"foo":"bar"}`, | ||
* `{"baz":100}`, | ||
* ]).pipeThrough(new ConcatenatedJsonParseStream()); | ||
* | ||
* assertEquals(await Array.fromAsync(stream), [ | ||
* { foo: "bar" }, | ||
* { baz: 100 }, | ||
* ]); | ||
* ``` | ||
*/ | ||
readonly readable: ReadableStream<JsonValue>; | ||
|
||
/** Constructs a new instance. */ | ||
/** | ||
* Constructs a new instance. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream"; | ||
* import { assertEquals } from "@std/assert/assert-equals"; | ||
* | ||
* const stream = ReadableStream.from([ | ||
* `{"foo":"bar"}`, | ||
* `{"baz":100}`, | ||
* ]).pipeThrough(new ConcatenatedJsonParseStream()); | ||
* | ||
* assertEquals(await Array.fromAsync(stream), [ | ||
* { foo: "bar" }, | ||
* { baz: 100 }, | ||
* ]); | ||
* ``` | ||
*/ | ||
constructor({ writableStrategy, readableStrategy }: ParseStreamOptions = {}) { | ||
const { writable, readable } = toTransformStream( | ||
this.#concatenatedJSONIterator, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSONParseStream
supposes every chunk is valid JSON string which represents a single JSON object.ConcatenatedJsonParseStream
handles stream of strings where each chunk can be incomplete fragments of JSON strings.This difference exists because in NDJSON or JSONLines, each line contains single JSON object. So we can use
TextLineStream
to split them and pass the result toJSONParseStream
. On the other hand, in Concatenated JSON, there's no explicit delimiter of JSONs. So the class needs to maintain the internal parsing state of JSONs