Skip to content

Commit 9811765

Browse files
committed
Fix issue with primitiveToJSON being set to JSON.parse
Cleanup code
1 parent 73de05d commit 9811765

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/JsonStreamStringify.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ function getType(value): Types {
4242
return Types.Primitive;
4343
}
4444

45-
const stackItemOpen = [];
46-
stackItemOpen[Types.Array] = '[';
47-
stackItemOpen[Types.Object] = '{';
48-
stackItemOpen[Types.ReadableString] = '"';
49-
stackItemOpen[Types.ReadableObject] = '[';
50-
51-
const stackItemEnd = [];
52-
stackItemEnd[Types.Array] = ']';
53-
stackItemEnd[Types.Object] = '}';
54-
stackItemEnd[Types.ReadableString] = '"';
55-
stackItemEnd[Types.ReadableObject] = ']';
56-
5745
function escapeString(string) {
5846
// Modified code, original code by Douglas Crockford
5947
// Original: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
@@ -72,15 +60,11 @@ function escapeString(string) {
7260
let primitiveToJSON: (value: any) => string;
7361

7462
if (global?.JSON?.stringify instanceof Function) {
75-
let canSerializeBigInt = true;
7663
try {
7764
if (JSON.stringify(global.BigInt ? global.BigInt('123') : '') !== '123') throw new Error();
65+
primitiveToJSON = JSON.stringify;
7866
} catch (err) {
79-
canSerializeBigInt = false;
80-
}
81-
if (canSerializeBigInt) {
82-
primitiveToJSON = JSON.parse;
83-
} else {
67+
// Add support for bigint for primitiveToJSON
8468
// eslint-disable-next-line no-confusing-arrow
8569
primitiveToJSON = (value) => typeof value === 'bigint' ? String(value) : JSON.stringify(value);
8670
}
@@ -126,7 +110,7 @@ function quoteString(string: string) {
126110
return str;
127111
}
128112

129-
function readAsPromised(stream, size?) {
113+
function readAsPromised(stream: Readable, size?) {
130114
const value = stream.read(size);
131115
if (value === null) {
132116
return new Promise((resolve, reject) => {
@@ -482,8 +466,6 @@ export class JsonStreamStringify extends Readable {
482466
return true;
483467
}
484468

485-
reading = false;
486-
readMore = false;
487469
readState: ReadState = ReadState.NotReading;
488470
async _read(size?: number) {
489471
if (this.readState === ReadState.Consumed) return;

0 commit comments

Comments
 (0)