json-stream-lite 1.1.3 → 1.1.5

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 CHANGED
@@ -32,7 +32,7 @@ yarn add json-stream-lite
32
32
 
33
33
  ### Parsing JSON
34
34
 
35
- #### Parse a complete JSON object incrementally
35
+ #### Parse a complete JSON object
36
36
 
37
37
  ```typescript
38
38
  import { JsonObject } from 'json-stream-lite'
@@ -48,7 +48,7 @@ const result = parser.read()
48
48
  console.log(result) // { name: 'Alice', age: 30, active: true }
49
49
  ```
50
50
 
51
- #### Stream through object members
51
+ #### Stream through object members incrementally
52
52
 
53
53
  ```typescript
54
54
  import { JsonObject } from 'json-stream-lite'
@@ -1,4 +1,4 @@
1
- import type { ByteStream, JsonStreamInput } from './types.js';
1
+ import type { ByteStream, StreamInput } from './types.js';
2
2
  /**
3
3
  * A buffer for managing byte-level input with support for async streams.
4
4
  * Provides lookahead, consumption tracking, and buffer compaction capabilities.
@@ -49,7 +49,7 @@ export declare class ByteBuffer {
49
49
  *
50
50
  * @param input - Input items to add to the buffer
51
51
  */
52
- feed(...input: JsonStreamInput[]): void;
52
+ feed(...input: StreamInput[]): void;
53
53
  push(byte: number): void;
54
54
  /**
55
55
  * Checks if end of file has been reached and buffer is exhausted.
@@ -54,7 +54,9 @@ export class ByteBuffer {
54
54
  asyncIterable instanceof ReadableStream
55
55
  ? readableStreamToAsyncIterable(asyncIterable)
56
56
  : typeof asyncIterable === 'string'
57
- ? [asyncIterable]
57
+ ? (function* () {
58
+ yield* [asyncIterable];
59
+ })()
58
60
  : asyncIterable;
59
61
  }
60
62
  /**
@@ -185,13 +187,11 @@ export class ByteBuffer {
185
187
  */
186
188
  next() {
187
189
  if (this.bufferIndex >= this.buffer.length) {
190
+ if (this.readStream()) {
191
+ return this.next();
192
+ }
188
193
  if (!this.eof) {
189
- if (!this.readStream()) {
190
- throw new NoMoreTokensError('No more items available');
191
- }
192
- else {
193
- return this.next();
194
- }
194
+ throw new NoMoreTokensError('No more items available');
195
195
  }
196
196
  throw new EofReachedError('End of file reached');
197
197
  }
package/dist/parser.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ByteBuffer } from './byte-buffer.js';
2
- import { ByteStream, JsonKeyValuePair, JsonStreamInput } from './types.js';
2
+ import { JsonStreamStringifyOptions } from './stringify.js';
3
+ import { ByteStream, JsonKeyValuePair, StreamInput } from './types.js';
3
4
  /**
4
5
  * Abstract base class for all JSON entities.
5
6
  * Provides common functionality for parsing, reading, and consuming JSON values.
@@ -46,7 +47,7 @@ export declare abstract class JsonEntity<T> {
46
47
  *
47
48
  * @param input - One or more strings, numbers, arrays of numbers, or Uint8Arrays to add to the buffer
48
49
  */
49
- feed(...input: JsonStreamInput[]): void;
50
+ feed(...input: StreamInput[]): void;
50
51
  /**
51
52
  * Abstract method that subclasses must implement to parse their specific type.
52
53
  *
@@ -88,6 +89,16 @@ export declare abstract class JsonEntity<T> {
88
89
  * @throws Error if the entity has already been consumed
89
90
  */
90
91
  tryParse<T = this>(cb: (entity: this) => T): T | undefined;
92
+ /**
93
+ * Serializes a value into a JSON string using json-stream-lite.
94
+ * See `jsonStreamStringify` for more details.
95
+ */
96
+ static stringify(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<string>;
97
+ /**
98
+ * Serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
99
+ * See `jsonStreamStringifyBytes` for more details.
100
+ */
101
+ static stringifyBytes(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
91
102
  }
92
103
  /**
93
104
  * Represents a JSON string value.
@@ -188,7 +199,7 @@ export declare class JsonNull extends JsonEntity<null> {
188
199
  */
189
200
  export type JsonPrimitiveType = JsonString | JsonNumber | JsonBoolean | JsonNull;
190
201
  /**
191
- * Union type representing any JSON value entity type (primitive, object, or array).
202
+ * Converts a standard JS primitive type into a json-stream-lite parser type
192
203
  *
193
204
  * @typeParam T - The expected type of the value
194
205
  */
package/dist/parser.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ByteBuffer } from './byte-buffer.js';
2
+ import { jsonStreamStringify, jsonStreamStringifyBytes, } from './stringify.js';
2
3
  import { bytesToNumber, bytesToString } from './utils.js';
3
4
  /**
4
5
  * Map of character names to their byte values for JSON parsing.
@@ -224,6 +225,20 @@ export class JsonEntity {
224
225
  this.buffer.locked = false;
225
226
  });
226
227
  }
228
+ /**
229
+ * Serializes a value into a JSON string using json-stream-lite.
230
+ * See `jsonStreamStringify` for more details.
231
+ */
232
+ static stringify(value, replacer, indent = 0, options) {
233
+ return jsonStreamStringify(value, replacer, indent, options);
234
+ }
235
+ /**
236
+ * Serializes a value into JSON as Uint8Array byte chunks using json-stream-lite.
237
+ * See `jsonStreamStringifyBytes` for more details.
238
+ */
239
+ static stringifyBytes(value, replacer, indent = 0, options) {
240
+ return jsonStreamStringifyBytes(value, replacer, indent, options);
241
+ }
227
242
  }
228
243
  /**
229
244
  * Represents a JSON string value.
package/dist/types.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
- * Union type representing valid JSON stream input formats.
2
+ * Union type representing valid stream input formats.
3
3
  */
4
- export type JsonStreamInput = string | number | number[] | Uint8Array;
4
+ export type StreamInput = string | number | number[] | Uint8Array;
5
5
  /**
6
6
  * An async iterable stream of JSON input that can be consumed incrementally.
7
7
  * Supports strings, numbers, arrays of numbers, or Uint8Arrays as stream items.
8
8
  */
9
- export type ByteStream = AsyncIterable<JsonStreamInput> | Iterable<JsonStreamInput> | ReadableStream<JsonStreamInput>;
9
+ export type ByteStream = AsyncIterable<StreamInput> | Iterable<StreamInput> | ReadableStream<StreamInput>;
10
10
  /**
11
11
  * Union type representing all JSON primitive value types.
12
12
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
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",