summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/parse-json.js
blob: c4149d282d131dec14f7117d1c0c9c3bd984133d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'use strict'
var parseJsonWithErrors = require('json-parse-better-errors')
var parseJSON = module.exports = function (content) {
  return parseJsonWithErrors(stripBOM(content))
}

parseJSON.noExceptions = function (content) {
  try {
    return parseJSON(content)
  } catch (ex) {
    return
  }
}

// from read-package-json
function stripBOM (content) {
  content = content.toString()
  // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  // because the buffer-to-string conversion in `fs.readFileSync()`
  // translates it to FEFF, the UTF-16 BOM.
  if (content.charCodeAt(0) === 0xFEFF) {
    content = content.slice(1)
  }
  return content
}