index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict'
  2. const hexify = char => {
  3. const h = char.charCodeAt(0).toString(16).toUpperCase()
  4. return '0x' + (h.length % 2 ? '0' : '') + h
  5. }
  6. const parseError = (e, txt, context) => {
  7. if (!txt) {
  8. return {
  9. message: e.message + ' while parsing empty string',
  10. position: 0,
  11. }
  12. }
  13. const badToken = e.message.match(/^Unexpected token (.) .*position\s+(\d+)/i)
  14. const errIdx = badToken ? +badToken[2]
  15. : e.message.match(/^Unexpected end of JSON.*/i) ? txt.length - 1
  16. : null
  17. const msg = badToken ? e.message.replace(/^Unexpected token ./, `Unexpected token ${
  18. JSON.stringify(badToken[1])
  19. } (${hexify(badToken[1])})`)
  20. : e.message
  21. if (errIdx !== null && errIdx !== undefined) {
  22. const start = errIdx <= context ? 0
  23. : errIdx - context
  24. const end = errIdx + context >= txt.length ? txt.length
  25. : errIdx + context
  26. const slice = (start === 0 ? '' : '...') +
  27. txt.slice(start, end) +
  28. (end === txt.length ? '' : '...')
  29. const near = txt === slice ? '' : 'near '
  30. return {
  31. message: msg + ` while parsing ${near}${JSON.stringify(slice)}`,
  32. position: errIdx,
  33. }
  34. } else {
  35. return {
  36. message: msg + ` while parsing '${txt.slice(0, context * 2)}'`,
  37. position: 0,
  38. }
  39. }
  40. }
  41. class JSONParseError extends SyntaxError {
  42. constructor (er, txt, context, caller) {
  43. context = context || 20
  44. const metadata = parseError(er, txt, context)
  45. super(metadata.message)
  46. Object.assign(this, metadata)
  47. this.code = 'EJSONPARSE'
  48. this.systemError = er
  49. Error.captureStackTrace(this, caller || this.constructor)
  50. }
  51. get name () { return this.constructor.name }
  52. set name (n) {}
  53. get [Symbol.toStringTag] () { return this.constructor.name }
  54. }
  55. const kIndent = Symbol.for('indent')
  56. const kNewline = Symbol.for('newline')
  57. // only respect indentation if we got a line break, otherwise squash it
  58. // things other than objects and arrays aren't indented, so ignore those
  59. // Important: in both of these regexps, the $1 capture group is the newline
  60. // or undefined, and the $2 capture group is the indent, or undefined.
  61. const formatRE = /^\s*[{\[]((?:\r?\n)+)([\s\t]*)/
  62. const emptyRE = /^(?:\{\}|\[\])((?:\r?\n)+)?$/
  63. const parseJson = (txt, reviver, context) => {
  64. const parseText = stripBOM(txt)
  65. context = context || 20
  66. try {
  67. // get the indentation so that we can save it back nicely
  68. // if the file starts with {" then we have an indent of '', ie, none
  69. // otherwise, pick the indentation of the next line after the first \n
  70. // If the pattern doesn't match, then it means no indentation.
  71. // JSON.stringify ignores symbols, so this is reasonably safe.
  72. // if the string is '{}' or '[]', then use the default 2-space indent.
  73. const [, newline = '\n', indent = ' '] = parseText.match(emptyRE) ||
  74. parseText.match(formatRE) ||
  75. [, '', '']
  76. const result = JSON.parse(parseText, reviver)
  77. if (result && typeof result === 'object') {
  78. result[kNewline] = newline
  79. result[kIndent] = indent
  80. }
  81. return result
  82. } catch (e) {
  83. if (typeof txt !== 'string' && !Buffer.isBuffer(txt)) {
  84. const isEmptyArray = Array.isArray(txt) && txt.length === 0
  85. throw Object.assign(new TypeError(
  86. `Cannot parse ${isEmptyArray ? 'an empty array' : String(txt)}`
  87. ), {
  88. code: 'EJSONPARSE',
  89. systemError: e,
  90. })
  91. }
  92. throw new JSONParseError(e, parseText, context, parseJson)
  93. }
  94. }
  95. // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  96. // because the buffer-to-string conversion in `fs.readFileSync()`
  97. // translates it to FEFF, the UTF-16 BOM.
  98. const stripBOM = txt => String(txt).replace(/^\uFEFF/, '')
  99. module.exports = parseJson
  100. parseJson.JSONParseError = JSONParseError
  101. parseJson.noExceptions = (txt, reviver) => {
  102. try {
  103. return JSON.parse(stripBOM(txt), reviver)
  104. } catch (e) {}
  105. }