parse-json.js 653 B

12345678910111213141516171819202122232425
  1. 'use strict'
  2. var parseJsonWithErrors = require('json-parse-better-errors')
  3. var parseJSON = module.exports = function (content) {
  4. return parseJsonWithErrors(stripBOM(content))
  5. }
  6. parseJSON.noExceptions = function (content) {
  7. try {
  8. return parseJSON(content)
  9. } catch (ex) {
  10. }
  11. }
  12. // from read-package-json
  13. function stripBOM (content) {
  14. content = content.toString()
  15. // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  16. // because the buffer-to-string conversion in `fs.readFileSync()`
  17. // translates it to FEFF, the UTF-16 BOM.
  18. if (content.charCodeAt(0) === 0xFEFF) {
  19. content = content.slice(1)
  20. }
  21. return content
  22. }