diff --git a/src/__tests__/path-detector.test.ts b/src/__tests__/path-detector.test.ts index 2534f00..95a39ce 100644 --- a/src/__tests__/path-detector.test.ts +++ b/src/__tests__/path-detector.test.ts @@ -46,4 +46,54 @@ test("PathDetector adds path", async () => { { ...objectEnd(), path: ["object"] }, { ...objectEnd(), path: [] } ]); -}); \ No newline at end of file +}); + +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: [] } + ]); +}); diff --git a/src/json-path-detector.ts b/src/json-path-detector.ts index 5d30738..ea7a7ce 100644 --- a/src/json-path-detector.ts +++ b/src/json-path-detector.ts @@ -52,8 +52,9 @@ export class JsonPathDetector extends AbstractTransformStream