json-stream-lite 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/parser.d.ts +14 -0
- package/dist/parser.js +25 -2
- package/dist/stringify.d.ts +2 -0
- package/dist/stringify.js +43 -8
- package/package.json +1 -1
package/dist/parser.d.ts
CHANGED
|
@@ -92,13 +92,27 @@ export declare abstract class JsonEntity<T> {
|
|
|
92
92
|
/**
|
|
93
93
|
* Serializes a value into a JSON string using json-stream-lite.
|
|
94
94
|
* See `jsonStreamStringify` for more details.
|
|
95
|
+
* @returns A generator yielding JSON string chunks
|
|
95
96
|
*/
|
|
96
97
|
static stringify(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<string>;
|
|
97
98
|
/**
|
|
98
99
|
* Serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
|
|
99
100
|
* See `jsonStreamStringifyBytes` for more details.
|
|
101
|
+
* @returns A generator yielding JSON byte chunks
|
|
100
102
|
*/
|
|
101
103
|
static stringifyBytes(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
|
|
104
|
+
/**
|
|
105
|
+
* Asynchronously serializes a value into a JSON string using json-stream-lite.
|
|
106
|
+
* See `jsonStreamStringifyAsync` for more details.
|
|
107
|
+
* @returns An async generator yielding JSON string chunks
|
|
108
|
+
* */
|
|
109
|
+
static stringifyAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<string>;
|
|
110
|
+
/**
|
|
111
|
+
* Asynchronously serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
|
|
112
|
+
* See `jsonStreamStringifyBytesAsync` for more details.
|
|
113
|
+
* @returns An async generator yielding JSON byte chunks
|
|
114
|
+
* */
|
|
115
|
+
static stringifyBytesAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<Uint8Array>;
|
|
102
116
|
}
|
|
103
117
|
/**
|
|
104
118
|
* Represents a JSON string value.
|
package/dist/parser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ByteBuffer } from './byte-buffer.js';
|
|
2
|
-
import { jsonStreamStringify, jsonStreamStringifyBytes, } from './stringify.js';
|
|
2
|
+
import { jsonStreamStringify, jsonStreamStringifyAsync, jsonStreamStringifyBytes, jsonStreamStringifyBytesAsync, } from './stringify.js';
|
|
3
3
|
import { bytesToNumber, bytesToString } from './utils.js';
|
|
4
4
|
/**
|
|
5
5
|
* Map of character names to their byte values for JSON parsing.
|
|
@@ -228,6 +228,7 @@ export class JsonEntity {
|
|
|
228
228
|
/**
|
|
229
229
|
* Serializes a value into a JSON string using json-stream-lite.
|
|
230
230
|
* See `jsonStreamStringify` for more details.
|
|
231
|
+
* @returns A generator yielding JSON string chunks
|
|
231
232
|
*/
|
|
232
233
|
static stringify(value, replacer, indent = 0, options) {
|
|
233
234
|
return jsonStreamStringify(value, replacer, indent, options);
|
|
@@ -235,10 +236,27 @@ export class JsonEntity {
|
|
|
235
236
|
/**
|
|
236
237
|
* Serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
|
|
237
238
|
* See `jsonStreamStringifyBytes` for more details.
|
|
239
|
+
* @returns A generator yielding JSON byte chunks
|
|
238
240
|
*/
|
|
239
241
|
static stringifyBytes(value, replacer, indent = 0, options) {
|
|
240
242
|
return jsonStreamStringifyBytes(value, replacer, indent, options);
|
|
241
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Asynchronously serializes a value into a JSON string using json-stream-lite.
|
|
246
|
+
* See `jsonStreamStringifyAsync` for more details.
|
|
247
|
+
* @returns An async generator yielding JSON string chunks
|
|
248
|
+
* */
|
|
249
|
+
static stringifyAsync(value, replacer, indent = 0, options) {
|
|
250
|
+
return jsonStreamStringifyAsync(value, replacer, indent, options);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Asynchronously serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
|
|
254
|
+
* See `jsonStreamStringifyBytesAsync` for more details.
|
|
255
|
+
* @returns An async generator yielding JSON byte chunks
|
|
256
|
+
* */
|
|
257
|
+
static stringifyBytesAsync(value, replacer, indent = 0, options) {
|
|
258
|
+
return jsonStreamStringifyBytesAsync(value, replacer, indent, options);
|
|
259
|
+
}
|
|
242
260
|
}
|
|
243
261
|
/**
|
|
244
262
|
* Represents a JSON string value.
|
|
@@ -591,7 +609,9 @@ export class JsonValue extends JsonEntity {
|
|
|
591
609
|
}
|
|
592
610
|
else {
|
|
593
611
|
super.consume();
|
|
612
|
+
this.value?.consume();
|
|
594
613
|
}
|
|
614
|
+
this.consumed = true;
|
|
595
615
|
}
|
|
596
616
|
/**
|
|
597
617
|
* Asynchronously consumes the value, ensuring it is fully read.
|
|
@@ -602,7 +622,9 @@ export class JsonValue extends JsonEntity {
|
|
|
602
622
|
}
|
|
603
623
|
else {
|
|
604
624
|
await super.consumeAsync();
|
|
625
|
+
await this.value?.consumeAsync();
|
|
605
626
|
}
|
|
627
|
+
this.consumed = true;
|
|
606
628
|
}
|
|
607
629
|
}
|
|
608
630
|
/**
|
|
@@ -696,7 +718,8 @@ export class JsonObject extends JsonEntity {
|
|
|
696
718
|
parse() {
|
|
697
719
|
const obj = {};
|
|
698
720
|
for (const { key, value } of this.members()) {
|
|
699
|
-
|
|
721
|
+
const keyString = key.read();
|
|
722
|
+
obj[keyString] = value.readValue();
|
|
700
723
|
}
|
|
701
724
|
return obj;
|
|
702
725
|
}
|
package/dist/stringify.d.ts
CHANGED
|
@@ -43,3 +43,5 @@ export declare function jsonStreamStringify(value: unknown, replacer?: any, inde
|
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
export declare function jsonStreamStringifyBytes(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
|
|
46
|
+
export declare function jsonStreamStringifyAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<string>;
|
|
47
|
+
export declare function jsonStreamStringifyBytesAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<Uint8Array>;
|
package/dist/stringify.js
CHANGED
|
@@ -98,10 +98,11 @@ function hasCustomToStringFunction(value) {
|
|
|
98
98
|
* @param replacer - Optional replacer function for value transformation
|
|
99
99
|
* @param indent - Number of spaces for indentation (0 for compact)
|
|
100
100
|
* @param currentDepth - Current nesting depth for indentation
|
|
101
|
+
* @param start - Indicates if this is the start of the stringification
|
|
101
102
|
* @param options - Stringification options
|
|
102
103
|
* @yields String chunks representing the JSON output
|
|
103
104
|
*/
|
|
104
|
-
function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth = 0, options) {
|
|
105
|
+
function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth = 0, start = true, options) {
|
|
105
106
|
if (replacer) {
|
|
106
107
|
value = replacer('', value);
|
|
107
108
|
}
|
|
@@ -113,6 +114,10 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
113
114
|
value = value.toString();
|
|
114
115
|
}
|
|
115
116
|
}
|
|
117
|
+
// If this is the first call and indenting, add initial indentation
|
|
118
|
+
if (start && indent > 0 && currentDepth > 0) {
|
|
119
|
+
yield ' '.repeat(indent * currentDepth);
|
|
120
|
+
}
|
|
116
121
|
if (value === null) {
|
|
117
122
|
yield 'null';
|
|
118
123
|
}
|
|
@@ -126,22 +131,23 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
126
131
|
yield* formatString(value, options?.stringChunkSize);
|
|
127
132
|
}
|
|
128
133
|
else if (Array.isArray(value)) {
|
|
129
|
-
yield '[';
|
|
130
134
|
const len = value.length;
|
|
131
135
|
const valueIndent = indent * (currentDepth + 1);
|
|
136
|
+
yield '[';
|
|
132
137
|
for (let i = 0; i < len; i++) {
|
|
133
138
|
const next = value[i];
|
|
134
139
|
if (i > 0) {
|
|
135
140
|
yield ',';
|
|
136
141
|
}
|
|
137
142
|
if (indent > 0) {
|
|
138
|
-
yield '\n'
|
|
143
|
+
yield '\n';
|
|
144
|
+
yield ' '.repeat(valueIndent);
|
|
139
145
|
}
|
|
140
146
|
if (shouldIgnore(next)) {
|
|
141
147
|
yield 'null';
|
|
142
148
|
continue;
|
|
143
149
|
}
|
|
144
|
-
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, options);
|
|
150
|
+
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, false, options);
|
|
145
151
|
}
|
|
146
152
|
if (indent > 0 && len > 0) {
|
|
147
153
|
yield '\n' + ' '.repeat(indent * currentDepth);
|
|
@@ -149,11 +155,11 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
149
155
|
yield ']';
|
|
150
156
|
}
|
|
151
157
|
else if (typeof value === 'object') {
|
|
152
|
-
yield '{';
|
|
153
158
|
const keys = Object.keys(value);
|
|
154
159
|
const len = keys.length;
|
|
155
160
|
const valueIndent = indent * (currentDepth + 1);
|
|
156
161
|
let count = 0;
|
|
162
|
+
yield '{';
|
|
157
163
|
for (let i = 0; i < len; i++) {
|
|
158
164
|
const key = keys[i];
|
|
159
165
|
const next = value[key];
|
|
@@ -164,11 +170,12 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
164
170
|
yield ',';
|
|
165
171
|
}
|
|
166
172
|
if (indent > 0) {
|
|
167
|
-
yield '\n'
|
|
173
|
+
yield '\n';
|
|
174
|
+
yield ' '.repeat(valueIndent);
|
|
168
175
|
}
|
|
169
176
|
yield* formatString(key, options?.stringChunkSize);
|
|
170
177
|
yield `:${indent > 0 ? ' ' : ''}`;
|
|
171
|
-
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, options);
|
|
178
|
+
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, false, options);
|
|
172
179
|
count++;
|
|
173
180
|
}
|
|
174
181
|
if (indent > 0 && len > 0) {
|
|
@@ -196,7 +203,7 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
196
203
|
* ```
|
|
197
204
|
*/
|
|
198
205
|
export function jsonStreamStringify(value, replacer, indent = 0, options) {
|
|
199
|
-
return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
|
|
206
|
+
return jsonStreamStringifyWithDepth(value, replacer, indent, 0, true, options);
|
|
200
207
|
}
|
|
201
208
|
/**
|
|
202
209
|
* Converts a JavaScript value to JSON format, yielding Uint8Array byte chunks.
|
|
@@ -222,3 +229,31 @@ export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options)
|
|
|
222
229
|
yield stringToBytes(chunk);
|
|
223
230
|
}
|
|
224
231
|
}
|
|
232
|
+
export async function* jsonStreamStringifyAsync(value, replacer, indent = 0, options) {
|
|
233
|
+
yield '[';
|
|
234
|
+
if (indent > 0)
|
|
235
|
+
yield '\n';
|
|
236
|
+
let isFirst = true;
|
|
237
|
+
for await (const item of value) {
|
|
238
|
+
const stringGenerator = jsonStreamStringifyWithDepth(item, replacer, indent, 1, true, options);
|
|
239
|
+
if (!isFirst) {
|
|
240
|
+
yield ',';
|
|
241
|
+
if (indent > 0)
|
|
242
|
+
yield '\n';
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
isFirst = false;
|
|
246
|
+
}
|
|
247
|
+
for (const chunk of stringGenerator) {
|
|
248
|
+
yield chunk;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (indent > 0)
|
|
252
|
+
yield '\n';
|
|
253
|
+
yield ']';
|
|
254
|
+
}
|
|
255
|
+
export async function* jsonStreamStringifyBytesAsync(value, replacer, indent = 0, options) {
|
|
256
|
+
for await (const chunk of jsonStreamStringifyAsync(value, replacer, indent, options)) {
|
|
257
|
+
yield stringToBytes(chunk);
|
|
258
|
+
}
|
|
259
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "A lightweight, memory-efficient, zero-dependency streaming JSON parser and stringifier for JavaScript and TypeScript. It works in both Node.js and browser environments.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|