json-stream-lite 1.0.2 → 1.0.3
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/README.md +1 -155
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -188,161 +188,7 @@ for (const chunk of jsonStreamStringify(data, null, 0, {
|
|
|
188
188
|
|
|
189
189
|
## API Reference
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
#### `JsonValue`
|
|
194
|
-
|
|
195
|
-
Represents any JSON value. Automatically detects the type and returns the appropriate entity.
|
|
196
|
-
|
|
197
|
-
```typescript
|
|
198
|
-
import { JsonValue } from 'json-stream-lite'
|
|
199
|
-
|
|
200
|
-
const parser = new JsonValue()
|
|
201
|
-
parser.feed(...bytes)
|
|
202
|
-
const valueEntity = parser.read() // Returns JsonString, JsonNumber, etc.
|
|
203
|
-
const actualValue = valueEntity.read()
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
#### `JsonObject`
|
|
207
|
-
|
|
208
|
-
Parses JSON objects.
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
class JsonObject<T = unknown> extends JsonEntity<T>
|
|
212
|
-
|
|
213
|
-
// Methods
|
|
214
|
-
members(): Generator<[JsonString, JsonValue]>
|
|
215
|
-
membersAsync(): AsyncGenerator<[JsonString, JsonValue]>
|
|
216
|
-
read(): T
|
|
217
|
-
readAsync(): Promise<T>
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
#### `JsonArray`
|
|
221
|
-
|
|
222
|
-
Parses JSON arrays.
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
class JsonArray<T = unknown> extends JsonEntity<T[]>
|
|
226
|
-
|
|
227
|
-
// Methods
|
|
228
|
-
items(): Generator<JsonValueType>
|
|
229
|
-
itemsAsync(): AsyncGenerator<JsonValueType>
|
|
230
|
-
read(): T[]
|
|
231
|
-
readAsync(): Promise<T[]>
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
#### `JsonString`, `JsonNumber`, `JsonBoolean`, `JsonNull`
|
|
235
|
-
|
|
236
|
-
Parse primitive JSON values.
|
|
237
|
-
|
|
238
|
-
```typescript
|
|
239
|
-
const str = new JsonString()
|
|
240
|
-
str.feed(...bytes)
|
|
241
|
-
console.log(str.read()) // Returns a string
|
|
242
|
-
|
|
243
|
-
const num = new JsonNumber()
|
|
244
|
-
num.feed(...bytes)
|
|
245
|
-
console.log(num.read()) // Returns a number
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
#### `JsonKeyValueParser`
|
|
249
|
-
|
|
250
|
-
Flattens nested JSON into key-value pairs.
|
|
251
|
-
|
|
252
|
-
```typescript
|
|
253
|
-
class JsonKeyValueParser extends JsonEntity<Generator<JsonKeyValuePair>>
|
|
254
|
-
|
|
255
|
-
// Methods
|
|
256
|
-
parse(): Generator<[string, JsonPrimitive]>
|
|
257
|
-
parseAsync(): AsyncGenerator<[string, JsonPrimitive]>
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
### Base Class: `JsonEntity`
|
|
261
|
-
|
|
262
|
-
All parser classes extend `JsonEntity<T>`:
|
|
263
|
-
|
|
264
|
-
```typescript
|
|
265
|
-
// Properties
|
|
266
|
-
consumed: boolean
|
|
267
|
-
bufferLength: number
|
|
268
|
-
maxBufferSize: number
|
|
269
|
-
|
|
270
|
-
// Methods
|
|
271
|
-
feed(...input: (number | number[])[]): void
|
|
272
|
-
read(): T
|
|
273
|
-
readAsync(): Promise<T>
|
|
274
|
-
consume(): void
|
|
275
|
-
consumeAsync(): Promise<void>
|
|
276
|
-
tryParse<T>(cb: (entity: this) => T): T | undefined
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### Generator Functions
|
|
280
|
-
|
|
281
|
-
#### `jsonKeyValueParser(bytes: Iterable<number> | string)`
|
|
282
|
-
|
|
283
|
-
Synchronously parse JSON into key-value pairs.
|
|
284
|
-
|
|
285
|
-
```typescript
|
|
286
|
-
import { jsonKeyValueParser } from 'json-stream-lite'
|
|
287
|
-
|
|
288
|
-
for (const [key, value] of jsonKeyValueParser('{"a": 1, "b": [2, 3]}')) {
|
|
289
|
-
console.log(key, value)
|
|
290
|
-
}
|
|
291
|
-
// a 1
|
|
292
|
-
// b[0] 2
|
|
293
|
-
// b[1] 3
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
#### `jsonKeyValueParserAsync(bytes: AsyncIterable<number> | ReadableStream | string)`
|
|
297
|
-
|
|
298
|
-
Asynchronously parse JSON into key-value pairs.
|
|
299
|
-
|
|
300
|
-
```typescript
|
|
301
|
-
import { jsonKeyValueParserAsync } from 'json-stream-lite'
|
|
302
|
-
|
|
303
|
-
for await (const [key, value] of jsonKeyValueParserAsync(stream)) {
|
|
304
|
-
console.log(key, value)
|
|
305
|
-
}
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
### Stringify Functions
|
|
309
|
-
|
|
310
|
-
#### `jsonStreamStringify(value, replacer?, indent?, options?)`
|
|
311
|
-
|
|
312
|
-
Generate JSON string chunks.
|
|
313
|
-
|
|
314
|
-
```typescript
|
|
315
|
-
function jsonStreamStringify(
|
|
316
|
-
value: unknown,
|
|
317
|
-
replacer?: any,
|
|
318
|
-
indent?: number,
|
|
319
|
-
options?: JsonStreamStringifyOptions,
|
|
320
|
-
): Generator<string>
|
|
321
|
-
|
|
322
|
-
interface JsonStreamStringifyOptions {
|
|
323
|
-
stringChunkSize?: number // Default: 1024
|
|
324
|
-
}
|
|
325
|
-
```
|
|
326
|
-
|
|
327
|
-
**Parameters:**
|
|
328
|
-
|
|
329
|
-
- `value`: The value to stringify
|
|
330
|
-
- `replacer`: Optional replacer function called on each value (with empty key) during stringification
|
|
331
|
-
- `indent`: Number of spaces for indentation (0 for compact)
|
|
332
|
-
- `options.stringChunkSize`: Maximum size of string chunks in bytes
|
|
333
|
-
|
|
334
|
-
#### `jsonStreamStringifyBytes(value, replacer?, indent?, options?)`
|
|
335
|
-
|
|
336
|
-
Generate JSON as Uint8Array chunks.
|
|
337
|
-
|
|
338
|
-
```typescript
|
|
339
|
-
function jsonStreamStringifyBytes(
|
|
340
|
-
value: unknown,
|
|
341
|
-
replacer?: any,
|
|
342
|
-
indent?: number,
|
|
343
|
-
options?: JsonStreamStringifyOptions,
|
|
344
|
-
): Generator<Uint8Array>
|
|
345
|
-
```
|
|
191
|
+
See [docs](https://jacobshirley.github.io/json-stream-lite/v1).
|
|
346
192
|
|
|
347
193
|
## Advanced Usage
|
|
348
194
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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",
|