json-stream-lite 1.0.7 → 1.0.8
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 -16
- package/dist/parser.d.ts +32 -20
- package/dist/parser.js +7 -6
- package/package.json +1 -1
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
1016
|
+
for (const { key: keyEntity, value: valueEntity } of parser.members()) {
|
|
1014
1017
|
const key = keyEntity.read()
|
|
1015
1018
|
|
|
1016
1019
|
if (key === 'period') {
|
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<
|
|
20
|
-
protected parse():
|
|
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<
|
|
23
|
-
protected parse():
|
|
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<
|
|
26
|
-
protected parse():
|
|
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<
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
56
|
-
[Symbol.asyncIterator](): AsyncGenerator<JsonValueType
|
|
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
|
|
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
|
|
305
|
-
await currentMember.value
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
3
|
+
"version": "1.0.8",
|
|
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",
|