Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions packages/core/src/mapping-kit/__tests__/index.iso.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ describe('@json', () => {
expect(output).toStrictEqual({ neat: ['bar', 'baz'] })
})

test('decode_invalid_json_returns_original_string', () => {
const output = transform(
{ neat: { '@json': { mode: 'decode', value: { '@path': '$.foo' } } } },
{ foo: 'Hello World' }
)
expect(output).toStrictEqual({ neat: 'Hello World' })
})

test('invalid mode', () => {
expect(() => {
transform({ neat: { '@json': { mode: 'oops', value: { '@path': '$.foo' } } } }, { foo: 'bar' })
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/mapping-kit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ registerDirective('@json', (opts, payload) => {
return JSON.stringify(value)
} else if (opts.mode === 'decode') {
if (typeof value === 'string') {
return JSON.parse(value)
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'primiative' to 'primitive'.

Suggested change
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
//Placing this in a try-catch to safely handle any primitive values that are put in the JSON.parse

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should follow standard formatting with a space after the double slashes: // Placing this in a try-catch... Additionally, consider making the comment more precise about what primitive values are being handled (e.g., 'non-JSON strings').

Suggested change
//Placing this in a try-catch to safely handle any primiative values that are put in the JSON.parse
// Placing this in a try-catch to safely handle non-JSON strings or primitive values that may cause JSON.parse to throw

Copilot uses AI. Check for mistakes.
try {
return JSON.parse(value)
} catch (e) {
return value
}
}

return value
}
})
Expand Down
Loading