json-stream-lite 1.0.8 → 1.0.10
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 +78 -7
- package/README.md +10 -9
- package/package.json +1 -1
package/EXAMPLES.md
CHANGED
|
@@ -2,9 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
This directory contains example scripts demonstrating how to use the JSON-Stream-Lite library.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Basic JSON Parsing Example
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
|
+
/**
|
|
9
|
+
* This example demonstrates how to parse a simple JSON object using json-stream-lite.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { JsonObject, JsonArray, JsonValue } from 'json-stream-lite'
|
|
13
|
+
|
|
14
|
+
// Helper to convert string to bytes
|
|
8
15
|
function stringToBytes(str: string): number[] {
|
|
9
16
|
return Array.from(new TextEncoder().encode(str))
|
|
10
17
|
}
|
|
@@ -50,9 +57,21 @@ const value = valueEntity.read()
|
|
|
50
57
|
console.log('Result:', value)
|
|
51
58
|
```
|
|
52
59
|
|
|
53
|
-
##
|
|
60
|
+
## Streaming Object Members Example
|
|
54
61
|
|
|
55
62
|
```typescript
|
|
63
|
+
/**
|
|
64
|
+
* This example shows how to iterate through object members without loading
|
|
65
|
+
* the entire object into memory at once.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
import { JsonObject } from 'json-stream-lite'
|
|
69
|
+
|
|
70
|
+
function stringToBytes(str: string): number[] {
|
|
71
|
+
return Array.from(new TextEncoder().encode(str))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Example 1: Stream through object members
|
|
56
75
|
console.log('=== Example 1: Stream Object Members ===')
|
|
57
76
|
const userJson =
|
|
58
77
|
'{"id": 123, "name": "Alice", "email": "alice@example.com", "age": 30}'
|
|
@@ -132,9 +151,17 @@ for (const { key: keyEntity, value: valueEntity } of searchParser.members()) {
|
|
|
132
151
|
console.log("Early exit - didn't process remaining fields")
|
|
133
152
|
```
|
|
134
153
|
|
|
135
|
-
##
|
|
154
|
+
## Key-Value Extraction Example
|
|
136
155
|
|
|
137
156
|
```typescript
|
|
157
|
+
/**
|
|
158
|
+
* This example demonstrates how to flatten nested JSON structures into
|
|
159
|
+
* key-value pairs using dot notation and array indices.
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
import { jsonKeyValueParser } from 'json-stream-lite'
|
|
163
|
+
|
|
164
|
+
// Example 1: Flatten a simple nested object
|
|
138
165
|
console.log('=== Example 1: Simple Nested Object ===')
|
|
139
166
|
const simpleNested = '{"user": {"name": "Alice", "age": 30}}'
|
|
140
167
|
|
|
@@ -240,9 +267,21 @@ for (const [key, value] of jsonKeyValueParser(arrayStructure)) {
|
|
|
240
267
|
// [2][1] = 6
|
|
241
268
|
```
|
|
242
269
|
|
|
243
|
-
##
|
|
270
|
+
## Async Streaming Example
|
|
244
271
|
|
|
245
272
|
```typescript
|
|
273
|
+
/**
|
|
274
|
+
* This example demonstrates how to process JSON from async sources
|
|
275
|
+
* like streams, fetch responses, or async iterables.
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
import {
|
|
279
|
+
JsonObject,
|
|
280
|
+
JsonArray,
|
|
281
|
+
jsonKeyValueParserAsync,
|
|
282
|
+
} from 'json-stream-lite'
|
|
283
|
+
|
|
284
|
+
// Helper to simulate an async byte stream
|
|
246
285
|
async function* createAsyncByteStream(
|
|
247
286
|
json: string,
|
|
248
287
|
chunkSize: number = 10,
|
|
@@ -414,9 +453,17 @@ await processLargeDataset()
|
|
|
414
453
|
console.log('\n=== All async examples completed ===')
|
|
415
454
|
```
|
|
416
455
|
|
|
417
|
-
##
|
|
456
|
+
## JSON Stringify Example
|
|
418
457
|
|
|
419
458
|
```typescript
|
|
459
|
+
/**
|
|
460
|
+
* This example demonstrates how to convert JavaScript objects to JSON
|
|
461
|
+
* in a streaming fashion, which is useful for large objects.
|
|
462
|
+
*/
|
|
463
|
+
|
|
464
|
+
import { jsonStreamStringify, jsonStreamStringifyBytes } from 'json-stream-lite'
|
|
465
|
+
|
|
466
|
+
// Example 1: Basic stringify
|
|
420
467
|
console.log('=== Example 1: Basic Stringify ===')
|
|
421
468
|
const simpleObj = {
|
|
422
469
|
name: 'Alice',
|
|
@@ -576,9 +623,21 @@ console.log('Output matches JSON.stringify:', streamResult === standardResult)
|
|
|
576
623
|
console.log('Stream result:', streamResult)
|
|
577
624
|
```
|
|
578
625
|
|
|
579
|
-
##
|
|
626
|
+
## Incremental Feeding Example
|
|
580
627
|
|
|
581
628
|
```typescript
|
|
629
|
+
/**
|
|
630
|
+
* This example shows how to feed bytes into the parser incrementally,
|
|
631
|
+
* which is useful when receiving data in chunks from a network or file.
|
|
632
|
+
*/
|
|
633
|
+
|
|
634
|
+
import { JsonObject, JsonArray } from 'json-stream-lite'
|
|
635
|
+
|
|
636
|
+
function stringToBytes(str: string): number[] {
|
|
637
|
+
return Array.from(new TextEncoder().encode(str))
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Example 1: Feed bytes one at a time
|
|
582
641
|
console.log('=== Example 1: Feed Bytes One at a Time ===')
|
|
583
642
|
const json1 = '{"name": "Alice"}'
|
|
584
643
|
const parser1 = new JsonObject()
|
|
@@ -729,9 +788,21 @@ console.log('Result:', result7)
|
|
|
729
788
|
console.log('Note: Can feed bytes individually, as arrays, or spread arrays')
|
|
730
789
|
```
|
|
731
790
|
|
|
732
|
-
##
|
|
791
|
+
## Real-World Scenarios Example
|
|
733
792
|
|
|
734
793
|
```typescript
|
|
794
|
+
/**
|
|
795
|
+
* This example demonstrates practical use cases for json-stream-lite
|
|
796
|
+
* in real-world applications.
|
|
797
|
+
*/
|
|
798
|
+
|
|
799
|
+
import { JsonObject, JsonArray, jsonKeyValueParser } from 'json-stream-lite'
|
|
800
|
+
|
|
801
|
+
function stringToBytes(str: string): number[] {
|
|
802
|
+
return Array.from(new TextEncoder().encode(str))
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// Scenario 1: Processing a configuration file
|
|
735
806
|
console.log('=== Scenario 1: Configuration File Processing ===')
|
|
736
807
|
function processConfig() {
|
|
737
808
|
const configJson = JSON.stringify({
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# json-stream-lite
|
|
4
4
|
|
|
5
|
-
A lightweight, memory-efficient streaming JSON parser and stringifier
|
|
5
|
+
A lightweight, memory-efficient, zero-dependency streaming JSON parser and stringifier written in TypeScript. Process large JSON files without loading them entirely into memory.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
@@ -58,7 +58,7 @@ const parser = new JsonObject()
|
|
|
58
58
|
parser.feed(...new TextEncoder().encode(json))
|
|
59
59
|
|
|
60
60
|
// Iterate through key-value pairs without loading the entire object
|
|
61
|
-
for (const
|
|
61
|
+
for (const { key: keyEntity, value: valueEntity } of parser) {
|
|
62
62
|
const key = keyEntity.read()
|
|
63
63
|
const value = valueEntity.read().read()
|
|
64
64
|
console.log(`${key}: ${value}`)
|
|
@@ -96,8 +96,9 @@ async function processStream(stream: ReadableStream<Uint8Array>) {
|
|
|
96
96
|
const parser = new JsonObject(stream)
|
|
97
97
|
|
|
98
98
|
// Asynchronously iterate through members
|
|
99
|
-
for await (const
|
|
100
|
-
|
|
99
|
+
for await (const { key: keyEntity, value: valueEntity } of parser) {
|
|
100
|
+
// If you want to read the key, make sure you call .read() before you read the value
|
|
101
|
+
const key = await keyEntity.readAsync()
|
|
101
102
|
const value = await valueEntity.readValueAsync()
|
|
102
103
|
console.log(`${key}: ${value}`)
|
|
103
104
|
}
|
|
@@ -202,8 +203,8 @@ async function processLargeFile(filePath: string) {
|
|
|
202
203
|
const stream = createReadStream(filePath)
|
|
203
204
|
const parser = new JsonObject(stream)
|
|
204
205
|
|
|
205
|
-
for await (const
|
|
206
|
-
const key = keyEntity.
|
|
206
|
+
for await (const { key: keyEntity, value: valueEntity } of parser) {
|
|
207
|
+
const key = await keyEntity.readAsync()
|
|
207
208
|
const value = await valueEntity.readValueAsync()
|
|
208
209
|
|
|
209
210
|
// Process each key-value pair without loading entire file
|
|
@@ -221,7 +222,7 @@ const json = '{"users": [{"name": "Alice"}, {"name": "Bob"}]}'
|
|
|
221
222
|
const parser = new JsonObject()
|
|
222
223
|
parser.feed(...new TextEncoder().encode(json))
|
|
223
224
|
|
|
224
|
-
for (const
|
|
225
|
+
for (const { key: keyEntity, value: valueEntity } of parser) {
|
|
225
226
|
const key = keyEntity.read()
|
|
226
227
|
const value = valueEntity.read()
|
|
227
228
|
|
|
@@ -260,8 +261,8 @@ async function processApiResponse(url: string) {
|
|
|
260
261
|
const response = await fetch(url)
|
|
261
262
|
const parser = new JsonObject(response.body!)
|
|
262
263
|
|
|
263
|
-
for await (const
|
|
264
|
-
const key = keyEntity.
|
|
264
|
+
for await (const { key: keyEntity, value: valueEntity } of parser) {
|
|
265
|
+
const key = await keyEntity.readAsync()
|
|
265
266
|
const value = await valueEntity.readValueAsync()
|
|
266
267
|
console.log(`Processing ${key}:`, value)
|
|
267
268
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
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",
|