Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # json-parse-even-better-errors
  2. [`json-parse-even-better-errors`](https://github.com/npm/json-parse-even-better-errors)
  3. is a Node.js library for getting nicer errors out of `JSON.parse()`,
  4. including context and position of the parse errors.
  5. It also preserves the newline and indentation styles of the JSON data, by
  6. putting them in the object or array in the `Symbol.for('indent')` and
  7. `Symbol.for('newline')` properties.
  8. ## Install
  9. `$ npm install --save json-parse-even-better-errors`
  10. ## Table of Contents
  11. * [Example](#example)
  12. * [Features](#features)
  13. * [Contributing](#contributing)
  14. * [API](#api)
  15. * [`parse`](#parse)
  16. ### Example
  17. ```javascript
  18. const parseJson = require('json-parse-even-better-errors')
  19. parseJson('"foo"') // returns the string 'foo'
  20. parseJson('garbage') // more useful error message
  21. parseJson.noExceptions('garbage') // returns undefined
  22. ```
  23. ### Features
  24. * Like JSON.parse, but the errors are better.
  25. * Strips a leading byte-order-mark that you sometimes get reading files.
  26. * Has a `noExceptions` method that returns undefined rather than throwing.
  27. * Attaches the newline character(s) used to the `Symbol.for('newline')`
  28. property on objects and arrays.
  29. * Attaches the indentation character(s) used to the `Symbol.for('indent')`
  30. property on objects and arrays.
  31. ## Indentation
  32. To preserve indentation when the file is saved back to disk, use
  33. `data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and
  34. if you want to preserve windows `\r\n` newlines, replace the `\n` chars in
  35. the string with `data[Symbol.for('newline')]`.
  36. For example:
  37. ```js
  38. const txt = await readFile('./package.json', 'utf8')
  39. const data = parseJsonEvenBetterErrors(txt)
  40. const indent = Symbol.for('indent')
  41. const newline = Symbol.for('newline')
  42. // .. do some stuff to the data ..
  43. const string = JSON.stringify(data, null, data[indent]) + '\n'
  44. const eolFixed = data[newline] === '\n' ? string
  45. : string.replace(/\n/g, data[newline])
  46. await writeFile('./package.json', eolFixed)
  47. ```
  48. Indentation is determined by looking at the whitespace between the initial
  49. `{` and `[` and the character that follows it. If you have lots of weird
  50. inconsistent indentation, then it won't track that or give you any way to
  51. preserve it. Whether this is a bug or a feature is debatable ;)
  52. ### API
  53. #### <a name="parse"></a> `parse(txt, reviver = null, context = 20)`
  54. Works just like `JSON.parse`, but will include a bit more information when
  55. an error happens, and attaches a `Symbol.for('indent')` and
  56. `Symbol.for('newline')` on objects and arrays. This throws a
  57. `JSONParseError`.
  58. #### <a name="parse"></a> `parse.noExceptions(txt, reviver = null)`
  59. Works just like `JSON.parse`, but will return `undefined` rather than
  60. throwing an error.
  61. #### <a name="jsonparseerror"></a> `class JSONParseError(er, text, context = 20, caller = null)`
  62. Extends the JavaScript `SyntaxError` class to parse the message and provide
  63. better metadata.
  64. Pass in the error thrown by the built-in `JSON.parse`, and the text being
  65. parsed, and it'll parse out the bits needed to be helpful.
  66. `context` defaults to 20.
  67. Set a `caller` function to trim internal implementation details out of the
  68. stack trace. When calling `parseJson`, this is set to the `parseJson`
  69. function. If not set, then the constructor defaults to itself, so the
  70. stack trace will point to the spot where you call `new JSONParseError`.