1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 |
 
x5
 
x5
 
x161
x966
x161
 
x5
x70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x5
 
 
 
 
x5
x64
x64
 
x64
x64
x64
 
x5
 
x64
x64
x64
x64
x64
x64
x64
x64
x135
x135
x1396
 
 
x1396
x1575
x1753
 
 
x1753
x1806
x1806
x1806
x1806
x1806
x1806
x1806
x1575
 
x1576
x1576
x1576
x1575
x1575
 
x1396
x1774
x1868
 
 
x1868
x1884
x1884
x1884
x1884
x1884
x1868
x1774
x1774
x1774
 
 
 
 
x1396
x1396
x1396
x1396
x1396
x1403
x1403
x1403
x1403
x1403
x1403
x1403
x1403
 
x2093
x1396
x1490
x1490
x1490
x1396
x1396
x1442
x1442
x1396
x1396
x1441
x1441
x1396
 
x1396
 
x1450
x1450
x1450
 
 
x1396
x1396
x1396
x1396
x1440
x1483
x1483
x1483
x1483
x1483
 
x1396
 
x1527
x1527
x1396
x205
x205
x64
x75
x84
x64
x5 |
|
// Copyright 2018-2025 the Deno authors. MIT license.
import { toTransformStream } from "@std/streams/to-transform-stream";
import type { JsonValue } from "./types.ts";
import { parse } from "./_common.ts";
function isBlankChar(char: string | undefined) {
return char !== undefined && [" ", "\t", "\r", "\n"].includes(char);
}
const primitives = new Map(
(["null", "true", "false"] as const).map((v) => [v[0], v]),
);
/**
* Stream to parse
* {@link https://en.wikipedia.org/wiki/JSON_streaming#Concatenated_JSON | Concatenated JSON}.
*
* @example Usage
*
* ```ts
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* `{"foo":"bar"}`,
* `{"baz":100}`,
* ]).pipeThrough(new ConcatenatedJsonParseStream());
*
* assertEquals(await Array.fromAsync(stream), [
* { foo: "bar" },
* { baz: 100 },
* ]);
* ```
*/
export class ConcatenatedJsonParseStream
implements TransformStream<string, JsonValue> {
/**
* A writable stream of byte data.
*
* @example Usage
* ```ts
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream";
* import { assertEquals } from "@std/assert";
*
* 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.
*
* @example Usage
* ```ts
* import { ConcatenatedJsonParseStream } from "@std/json/concatenated-json-parse-stream";
* import { assertEquals } from "@std/assert";
*
* 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.
*/
constructor() {
const { writable, readable } = toTransformStream(
this.#concatenatedJSONIterator,
);
this.writable = writable;
this.readable = readable;
}
async *#concatenatedJSONIterator(src: AsyncIterable<string>) {
// Counts the number of '{', '}', '[', ']', and when the nesting level reaches 0, concatenates and returns the string.
let targetString = "";
let hasValue = false;
let nestCount = 0;
let readingString = false;
let escapeNext = false;
let readingPrimitive: false | "null" | "true" | "false" = false;
let positionInPrimitive = 0;
for await (const string of src) {
let sliceStart = 0;
for (let i = 0; i < string.length; i++) {
const char = string[i];
// We're reading a primitive at the top level
if (readingPrimitive) {
if (char === readingPrimitive[positionInPrimitive]) {
positionInPrimitive++;
// Emit the primitive when done reading
if (positionInPrimitive === readingPrimitive.length) {
yield parse(targetString + string.slice(sliceStart, i + 1));
hasValue = false;
readingPrimitive = false;
positionInPrimitive = 0;
targetString = "";
sliceStart = i + 1;
}
} else {
// If the primitive is malformed, keep reading, maybe the next characters can be useful in the syntax error.
readingPrimitive = false;
positionInPrimitive = 0;
}
continue;
}
if (readingString) {
if (char === '"' && !escapeNext) {
readingString = false;
// When the nesting level is 0, it returns a string when '"' comes.
if (nestCount === 0 && hasValue) {
yield parse(targetString + string.slice(sliceStart, i + 1));
hasValue = false;
targetString = "";
sliceStart = i + 1;
}
}
escapeNext = !escapeNext && char === "\\";
continue;
}
// Parses number with a nesting level of 0.
// example: '0["foo"]' => 0, ["foo"]
// example: '3.14{"foo": "bar"}' => 3.14, {foo: "bar"}
if (
hasValue && nestCount === 0 &&
(char === "{" || char === "[" || char === '"' || char === " " ||
char === "n" || char === "t" || char === "f")
) {
yield parse(targetString + string.slice(sliceStart, i));
hasValue = false;
readingString = false;
targetString = "";
sliceStart = i;
i--;
continue;
}
switch (char) {
case '"':
readingString = true;
escapeNext = false;
break;
case "{":
case "[":
nestCount++;
break;
case "}":
case "]":
nestCount--;
break;
}
if (nestCount === 0 && primitives.has(char)) {
// The first letter of a primitive at top level was found
readingPrimitive = primitives.get(char)!;
positionInPrimitive = 1;
}
// parse object or array
if (
hasValue && nestCount === 0 &&
(char === "}" || char === "]")
) {
yield parse(targetString + string.slice(sliceStart, i + 1));
hasValue = false;
targetString = "";
sliceStart = i + 1;
continue;
}
if (!hasValue && !isBlankChar(char)) {
// We want to ignore the character string with only blank, so if there is a character other than blank, record it.
hasValue = true;
}
}
targetString += string.slice(sliceStart);
}
if (hasValue) {
yield parse(targetString);
}
}
}
|