Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/__tests__/path-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,54 @@ test("PathDetector adds path", async () => {
{ ...objectEnd(), path: ["object"] },
{ ...objectEnd(), path: [] }
]);
});
});

test("PathDetector handles empty object", async () => {
const stream = serializeJsonValue({
outer: {
inner: {}
}
}).pipeThrough(new JsonPathDetector());

expect(await streamToArray(stream)).toEqual([
{ ...objectStart(), path: [] },
{ ...stringStart(StringRole.KEY), path: [] },
{ ...stringChunk("outer", StringRole.KEY), path: [] },
{ ...stringEnd(StringRole.KEY), path: [] },
{ ...colon(), path: [] },
{ ...objectStart(), path: ["outer"] },
{ ...stringStart(StringRole.KEY), path: ["outer"] },
{ ...stringChunk("inner", StringRole.KEY), path: ["outer"] },
{ ...stringEnd(StringRole.KEY), path: ["outer"] },
{ ...colon(), path: ["outer"] },
{ ...objectStart(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer"] },
{ ...objectEnd(), path: [] }
]);
});

test("PathDetector handles empty array", async () => {
const stream = serializeJsonValue({
outer: {
inner: []
}
}).pipeThrough(new JsonPathDetector());

expect(await streamToArray(stream)).toEqual([
{ ...objectStart(), path: [] },
{ ...stringStart(StringRole.KEY), path: [] },
{ ...stringChunk("outer", StringRole.KEY), path: [] },
{ ...stringEnd(StringRole.KEY), path: [] },
{ ...colon(), path: [] },
{ ...objectStart(), path: ["outer"] },
{ ...stringStart(StringRole.KEY), path: ["outer"] },
{ ...stringChunk("inner", StringRole.KEY), path: ["outer"] },
{ ...stringEnd(StringRole.KEY), path: ["outer"] },
{ ...colon(), path: ["outer"] },
{ ...arrayStart(), path: ["outer", "inner"] },
{ ...arrayEnd(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer"] },
{ ...objectEnd(), path: [] }
]);
});
5 changes: 3 additions & 2 deletions src/json-path-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export class JsonPathDetector extends AbstractTransformStream<JsonChunk, JsonChu
} else if (chunk.type === JsonChunkType.ARRAY_START) {
this.stack.push({ type: "array", state: "next", key: 0 });
} else if (chunk.type === JsonChunkType.OBJECT_END || chunk.type === JsonChunkType.ARRAY_END) {
this.stack.pop();
this.path.pop();
if (this.stack.pop()?.state !== "pending") {
this.path.pop();
}
} else {
const current = this.stack[this.stack.length - 1];
if (current?.type === "object") {
Expand Down