Skip to content

Commit

Permalink
fix(lossless-parser): correctly handle null in objects
Browse files Browse the repository at this point in the history
the lossless parser now correctly handles null fields in objects

fixes #212
  • Loading branch information
jwulf committed Jul 29, 2024
1 parent 7f347c6 commit b712651
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"modeler",
"operate",
"camunda8",
"optimize"
"optimize",
"lossless-parser"
],
"editor.formatOnSave": true,

Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/lib/LosslessJsonParser.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,17 @@ test('LosslessStringify correctly handles nested Dtos', () => {
`{"total":3,"decisionsOutputs":[{"someInt32Field":123,"someInt64Field":123}]}`
)
})

test('LosslessJsonParser correctly handles null objects', () => {
const json = `{"abc": [null, null, null] }`

const parsedDto = losslessParse(json)
expect(parsedDto.abc).toMatchObject([null, null, null]) // 3 (string)
})

test('LosslessStringify correctly handles null objects', () => {
const json = { abc: [null, null, null] }

const stringifiedDto = losslessStringify(json)
expect(stringifiedDto).toBe(`{"abc":[null,null,null]}`) // 3 (string)
})
5 changes: 4 additions & 1 deletion src/lib/LosslessJsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ function parseArrayWithAnnotations<T>(
* Convert all `LosslessNumber` instances to a number or throw if any are unsafe
*/
function convertLosslessNumbersToNumberOrThrow<T>(obj: any): T {
debug(`Parsing LosslessNumbers to numbers for ${obj.constructor.name}`)
debug(`Parsing LosslessNumbers to numbers for ${obj?.constructor?.name}`)
if (!obj) {
return obj
}
let currentKey = ''
try {
Object.keys(obj).forEach((key) => {
Expand Down

0 comments on commit b712651

Please sign in to comment.