Skip to content

Ignore BOM char #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2020
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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function parse (text, reviver, options) {
const protoAction = options.protoAction || 'error'
const constructorAction = options.constructorAction || 'error'

// BOM checker
if (text && text.charCodeAt(0) === 0xFEFF) {
text = text.slice(1)
}

// Parse normally, allowing exceptions
const obj = JSON.parse(text, reviver)

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,13 @@ test('safeParse', t => {

t.end()
})

test('parse string with BOM', t => {
const theJson = { hello: 'world' }
const buffer = Buffer.concat([
Buffer.from([239, 187, 191]), // the utf8 BOM
Buffer.from(JSON.stringify(theJson))
])
t.deepEqual(j.parse(buffer.toString()), theJson)
t.end()
})