json-stream-lite 1.0.7 → 1.0.9

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 CHANGED
@@ -60,7 +60,7 @@ const userParser = new JsonObject()
60
60
  userParser.feed(...stringToBytes(userJson))
61
61
 
62
62
  console.log('Processing members one by one:')
63
- for (const [keyEntity, valueEntity] of userParser.members()) {
63
+ for (const { key: keyEntity, value: valueEntity } of userParser.members()) {
64
64
  const key = keyEntity.read()
65
65
  const value = valueEntity.read().read()
66
66
  console.log(` ${key}: ${value} (${typeof value})`)
@@ -91,7 +91,7 @@ const dataParser = new JsonObject()
91
91
  dataParser.feed(...stringToBytes(dataJson))
92
92
 
93
93
  console.log('Only processing the "data" field:')
94
- for (const [keyEntity, valueEntity] of dataParser.members()) {
94
+ for (const { key: keyEntity, value: valueEntity } of dataParser.members()) {
95
95
  const key = keyEntity.read()
96
96
 
97
97
  if (key === 'data') {
@@ -117,7 +117,7 @@ const searchParser = new JsonObject()
117
117
  searchParser.feed(...stringToBytes(largeJson))
118
118
 
119
119
  console.log('Searching for "target" field:')
120
- for (const [keyEntity, valueEntity] of searchParser.members()) {
120
+ for (const { key: keyEntity, value: valueEntity } of searchParser.members()) {
121
121
  const key = keyEntity.read()
122
122
 
123
123
  if (key === 'target') {
@@ -267,7 +267,7 @@ async function parseAsyncObject() {
267
267
  const parser = new JsonObject(stream)
268
268
 
269
269
  console.log('Streaming members asynchronously:')
270
- for await (const [keyEntity, valueEntity] of parser) {
270
+ for await (const { key: keyEntity, value: valueEntity } of parser) {
271
271
  const key = keyEntity.read()
272
272
  const value = await valueEntity.readValueAsync()
273
273
  console.log(` ${key}: ${value}`)
@@ -338,7 +338,7 @@ async function processHttpResponse() {
338
338
  const parser = new JsonObject(stream)
339
339
 
340
340
  console.log('Processing HTTP response:')
341
- for await (const [keyEntity, valueEntity] of parser) {
341
+ for await (const { key: keyEntity, value: valueEntity } of parser) {
342
342
  const key = keyEntity.read()
343
343
 
344
344
  if (key === 'data') {
@@ -346,7 +346,10 @@ async function processHttpResponse() {
346
346
  const dataValue = await valueEntity.readAsync()
347
347
  const dataObj = dataValue as JsonObject
348
348
 
349
- for await (const [dataKey, dataValueEntity] of dataObj) {
349
+ for await (const {
350
+ key: dataKey,
351
+ value: dataValueEntity,
352
+ } of dataObj) {
350
353
  const dataKeyStr = dataKey.read()
351
354
  if (dataKeyStr === 'users') {
352
355
  console.log(` Found users array:`)
@@ -387,7 +390,7 @@ async function processLargeDataset() {
387
390
  let recordCount = 0
388
391
  let totalValue = 0
389
392
 
390
- for await (const [keyEntity, valueEntity] of parser) {
393
+ for await (const { key: keyEntity, value: valueEntity } of parser) {
391
394
  const key = keyEntity.read()
392
395
 
393
396
  if (key === 'records') {
@@ -621,7 +624,7 @@ const parser3 = new JsonObject()
621
624
  parser3.feed(...stringToBytes(json3))
622
625
 
623
626
  console.log('Parsing members after feeding:')
624
- for (const [keyEntity, valueEntity] of parser3.members()) {
627
+ for (const { key: keyEntity, value: valueEntity } of parser3.members()) {
625
628
  const key = keyEntity.read()
626
629
  const value = valueEntity.read().read()
627
630
  console.log(` ${key}: ${value}`)
@@ -797,7 +800,7 @@ function analyzeLogEntries() {
797
800
  const stats = { ERROR: 0, INFO: 0, WARN: 0 }
798
801
 
799
802
  console.log('Analyzing log entries:')
800
- for (const [keyEntity, valueEntity] of parser.members()) {
803
+ for (const { key: keyEntity, value: valueEntity } of parser.members()) {
801
804
  const key = keyEntity.read()
802
805
 
803
806
  if (key === 'logs') {
@@ -844,15 +847,15 @@ function transformApiResponse() {
844
847
  const transformedUsers: any[] = []
845
848
 
846
849
  console.log('Transforming API response:')
847
- for (const [keyEntity, valueEntity] of parser.members()) {
850
+ for (const { key: keyEntity, value: valueEntity } of parser.members()) {
848
851
  const key = keyEntity.read()
849
852
 
850
853
  if (key === 'data') {
851
854
  const dataValue = valueEntity.read() as JsonObject
852
- for (const [
853
- dataKeyEntity,
854
- dataValueEntity,
855
- ] of dataValue.members()) {
855
+ for (const {
856
+ key: dataKeyEntity,
857
+ value: dataValueEntity,
858
+ } of dataValue.members()) {
856
859
  const dataKey = dataKeyEntity.read()
857
860
 
858
861
  if (dataKey === 'users') {
@@ -894,7 +897,7 @@ function validateData() {
894
897
  const errors: string[] = []
895
898
 
896
899
  console.log('Validating user data:')
897
- for (const [keyEntity, valueEntity] of parser.members()) {
900
+ for (const { key: keyEntity, value: valueEntity } of parser.members()) {
898
901
  const key = keyEntity.read()
899
902
 
900
903
  if (key === 'users') {
@@ -1010,7 +1013,7 @@ function generateReport() {
1010
1013
 
1011
1014
  console.log('Sales Report:')
1012
1015
 
1013
- for (const [keyEntity, valueEntity] of parser.members()) {
1016
+ for (const { key: keyEntity, value: valueEntity } of parser.members()) {
1014
1017
  const key = keyEntity.read()
1015
1018
 
1016
1019
  if (key === 'period') {
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 for JavaScript and TypeScript. Process large JSON files without loading them entirely into memory.
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 [keyEntity, valueEntity] of parser.members()) {
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 [keyEntity, valueEntity] of parser.membersAsync()) {
100
- const key = keyEntity.read()
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 [keyEntity, valueEntity] of parser) {
206
- const key = keyEntity.read()
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 [keyEntity, valueEntity] of parser) {
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 [keyEntity, valueEntity] of parser.membersAsync()) {
264
- const key = keyEntity.read()
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/dist/parser.d.ts CHANGED
@@ -16,44 +16,56 @@ export declare abstract class JsonEntity<T> {
16
16
  consumeAsync(): Promise<void>;
17
17
  tryParse<T = this>(cb: (entity: this) => T): T | undefined;
18
18
  }
19
- export declare class JsonString extends JsonEntity<string> {
20
- protected parse(): string;
19
+ export declare class JsonString<T extends string = string> extends JsonEntity<T> {
20
+ protected parse(): T;
21
21
  }
22
- export declare class JsonNumber extends JsonEntity<number> {
23
- protected parse(): number;
22
+ export declare class JsonNumber<T extends number = number> extends JsonEntity<T> {
23
+ protected parse(): T;
24
24
  }
25
- export declare class JsonBoolean extends JsonEntity<boolean> {
26
- protected parse(): boolean;
25
+ export declare class JsonBoolean<T extends boolean = boolean> extends JsonEntity<T> {
26
+ protected parse(): T;
27
27
  }
28
28
  export declare class JsonNull extends JsonEntity<null> {
29
29
  protected parse(): null;
30
30
  }
31
31
  export type JsonPrimitiveType = JsonString | JsonNumber | JsonBoolean | JsonNull;
32
- export type JsonValueType = JsonPrimitiveType | JsonObject | JsonArray;
33
- export declare class JsonValue extends JsonEntity<JsonValueType> {
32
+ export type JsonValueType<T = unknown> = JsonPrimitiveType | JsonObject<T> | JsonArray<T>;
33
+ export declare class JsonValue<T extends unknown = unknown, K extends string = string> extends JsonEntity<JsonValueType<T>> {
34
34
  private key?;
35
35
  private value?;
36
- constructor(buffer?: ByteBuffer | ByteStream, key?: JsonString);
37
- protected parse(): JsonValueType;
38
- read(): JsonValueType;
36
+ constructor(buffer?: ByteBuffer | ByteStream, key?: JsonString<K>);
37
+ protected parse(): JsonValueType<T>;
38
+ read(): JsonValueType<T>;
39
39
  readValue(): unknown;
40
40
  readValueAsync(): Promise<unknown>;
41
- readAsync(): Promise<JsonValueType>;
41
+ readAsync(): Promise<JsonValueType<T>>;
42
42
  consume(): void;
43
43
  consumeAsync(): Promise<void>;
44
44
  }
45
45
  export declare class JsonObject<T = unknown> extends JsonEntity<T> {
46
- members(): Generator<[JsonString, JsonValue]>;
47
- membersAsync(): AsyncGenerator<[JsonString, JsonValue]>;
48
- [Symbol.iterator](): Generator<[JsonString, JsonValue], any, any>;
49
- [Symbol.asyncIterator](): AsyncGenerator<[JsonString, JsonValue], any, any>;
46
+ members(): Generator<{
47
+ key: JsonString<Extract<keyof T, string>>;
48
+ value: JsonValue<T>;
49
+ }>;
50
+ membersAsync(): AsyncGenerator<{
51
+ key: JsonString<Extract<keyof T, string>>;
52
+ value: JsonValue<T>;
53
+ }>;
54
+ [Symbol.iterator](): Generator<{
55
+ key: JsonString<Extract<keyof T, string>>;
56
+ value: JsonValue<T>;
57
+ }, any, any>;
58
+ [Symbol.asyncIterator](): AsyncGenerator<{
59
+ key: JsonString<Extract<keyof T, string>>;
60
+ value: JsonValue<T>;
61
+ }, any, any>;
50
62
  protected parse(): T;
51
63
  }
52
64
  export declare class JsonArray<T = unknown> extends JsonEntity<T[]> {
53
- items(): Generator<JsonValueType>;
54
- itemsAsync(): AsyncGenerator<JsonValueType>;
55
- [Symbol.iterator](): Generator<JsonValueType, any, any>;
56
- [Symbol.asyncIterator](): AsyncGenerator<JsonValueType, any, any>;
65
+ items(): Generator<JsonValueType<T>>;
66
+ itemsAsync(): AsyncGenerator<JsonValueType<T>>;
67
+ [Symbol.iterator](): Generator<JsonValueType<T>, any, any>;
68
+ [Symbol.asyncIterator](): AsyncGenerator<JsonValueType<T>, any, any>;
57
69
  protected parse(): T[];
58
70
  }
59
71
  export declare class JsonKeyValueParser extends JsonEntity<Generator<JsonKeyValuePair>> {
package/dist/parser.js CHANGED
@@ -274,7 +274,7 @@ export class JsonObject extends JsonEntity {
274
274
  }
275
275
  const key = new JsonString(this.buffer);
276
276
  const value = new JsonValue(this.buffer, key);
277
- yield [key, value];
277
+ yield { key, value };
278
278
  key.consume();
279
279
  value.consume();
280
280
  this.skipWhitespace();
@@ -301,8 +301,8 @@ export class JsonObject extends JsonEntity {
301
301
  }
302
302
  yield currentMember.value;
303
303
  // Make sure to consume the key and value asynchronously
304
- await currentMember.value[0].consumeAsync();
305
- await currentMember.value[1].consumeAsync();
304
+ await currentMember.value.key.consumeAsync();
305
+ await currentMember.value.value.consumeAsync();
306
306
  }
307
307
  }
308
308
  }
@@ -314,7 +314,7 @@ export class JsonObject extends JsonEntity {
314
314
  }
315
315
  parse() {
316
316
  const obj = {};
317
- for (const [key, value] of this.members()) {
317
+ for (const { key, value } of this.members()) {
318
318
  obj[key.read()] = value.read().read();
319
319
  }
320
320
  return obj;
@@ -372,7 +372,7 @@ export class JsonArray extends JsonEntity {
372
372
  }
373
373
  parse() {
374
374
  const values = [];
375
- for (const value of this.items()) {
375
+ for (const value of this) {
376
376
  values.push(value.read());
377
377
  }
378
378
  return values;
@@ -388,7 +388,8 @@ export class JsonKeyValueParser extends JsonEntity {
388
388
  }
389
389
  *parse() {
390
390
  if (this.container instanceof JsonObject) {
391
- for (const [keyEntity, valueEntity] of this.container) {
391
+ for (const { key: keyEntity, value: valueEntity } of this
392
+ .container) {
392
393
  const key = keyEntity.read();
393
394
  const value = valueEntity.read();
394
395
  const finalKey = this.parentKey
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
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",