Skip to content

Commit

Permalink
resolve #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Faleij committed Dec 1, 2023
1 parent a29fbfe commit 08133a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/JsonStreamStringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export class JsonStreamStringify extends Readable {
wasFirst = true;
if (!len) {
that._push('[]');
that.unvisit(input);
that.item = parent;
return;
}
Expand Down
16 changes: 14 additions & 2 deletions test-src/JsonStreamStringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ describe('JsonStreamStringify', () => {
b: 2,
},
'{"a":[1],"b":2}'));

it('{a: array, "b": array } should be {"a":[],"b":[]}', () => {
const array = [];
const data = { a: array, b: array };
return createTest(data, '{"a":[],"b":[]}')();
});

it('{ partiallyEvaluated: { resolved: array }, evaluated: array } should be {"partiallyEvaluated":{"resolved":[]},"evaluated":[]}', () => {
const array = [];
const data = { raw: { reference: "/items" }, partiallyEvaluated: { resolved: array }, evaluated: array, };
return createTest(data, '{"raw":{"reference":"/items"},"partiallyEvaluated":{"resolved":[]},"evaluated":[]}')();
});

it('[] should be []', createTest([], '[]'));

Expand Down Expand Up @@ -323,13 +335,13 @@ describe('JsonStreamStringify', () => {

it('{ a: [], b: [] } should be {"a":[],"b":[]}', () => {
const cyclicData : any = { a: [], b: [] };
return createTest(cyclicData, '{"a":[],"b":[]}', undefined, undefined, true)
return createTest(cyclicData, '{"a":[],"b":[]}', undefined, undefined, true)();
});

it('{ a: a, b: a } should be {"a":[],"b":{"$ref":"$"}}', () => {
const cyclicData : any = { a: [] };
cyclicData.b = cyclicData.a;
return createTest(cyclicData, '{"a":[],"b":{"$ref":"$[\\"a\\"]"}}', undefined, undefined, true)
return createTest(cyclicData, '{"a":[],"b":{"$ref":"$[\\"a\\"]"}}', undefined, undefined, true)();
});

const cyclicData1 : any = {};
Expand Down

0 comments on commit 08133a4

Please sign in to comment.