json-stream-lite 1.0.1 → 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/EXAMPLES.md +19 -12
- package/README.md +1 -155
- package/package.json +1 -1
package/EXAMPLES.md
CHANGED
|
@@ -468,27 +468,34 @@ const nestedJson = Array.from(jsonStreamStringify(nested, null, 2)).join('')
|
|
|
468
468
|
console.log(nestedJson)
|
|
469
469
|
|
|
470
470
|
// Example 4: Using a replacer function
|
|
471
|
+
// Note: The replacer function is called on each value (with the key parameter, which is '' for the root) during stringification.
|
|
471
472
|
console.log('\n=== Example 4: With Replacer Function ===')
|
|
472
|
-
const
|
|
473
|
-
username: 'alice',
|
|
474
|
-
password: 'secret123',
|
|
475
|
-
apiKey: 'key-12345',
|
|
476
|
-
public: 'visible data',
|
|
477
|
-
}
|
|
473
|
+
const numbers = { a: 1, b: 2, c: 3 }
|
|
478
474
|
|
|
479
475
|
const replacer = (key: string, value: any) => {
|
|
480
|
-
|
|
481
|
-
|
|
476
|
+
// Transform objects by doubling all number values
|
|
477
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
478
|
+
const transformed: Record<string, any> = {}
|
|
479
|
+
for (const [k, v] of Object.entries(value)) {
|
|
480
|
+
transformed[k] = typeof v === 'number' ? v * 2 : v
|
|
481
|
+
}
|
|
482
|
+
return transformed
|
|
482
483
|
}
|
|
483
484
|
return value
|
|
484
485
|
}
|
|
485
486
|
|
|
486
|
-
console.log('Original object:',
|
|
487
|
-
console.log('Stringified with replacer:')
|
|
488
|
-
const
|
|
487
|
+
console.log('Original object:', numbers)
|
|
488
|
+
console.log('Stringified with replacer (numbers doubled):')
|
|
489
|
+
const transformed = Array.from(jsonStreamStringify(numbers, replacer, 2)).join(
|
|
489
490
|
'',
|
|
490
491
|
)
|
|
491
|
-
console.log(
|
|
492
|
+
console.log(transformed)
|
|
493
|
+
// Output:
|
|
494
|
+
// {
|
|
495
|
+
// "a": 2,
|
|
496
|
+
// "b": 4,
|
|
497
|
+
// "c": 6
|
|
498
|
+
// }
|
|
492
499
|
|
|
493
500
|
// Example 5: Stringify to bytes
|
|
494
501
|
console.log('\n=== Example 5: Stringify to Bytes ===')
|
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 (like `JSON.stringify`)
|
|
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",
|