- Notifications
You must be signed in to change notification settings - Fork4
npm/json-parse-even-better-errors
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
json-parse-even-better-errors
is a Node.js library for getting nicer errors out ofJSON.parse()
,including context and position of the parse errors.
It also preserves the newline and indentation styles of the JSON data, byputting them in the object or array in theSymbol.for('indent')
andSymbol.for('newline')
properties.
$ npm install --save json-parse-even-better-errors
constparseJson=require('json-parse-even-better-errors')parseJson('"foo"')// returns the string 'foo'parseJson('garbage')// more useful error messageparseJson.noExceptions('garbage')// returns undefined
- Like JSON.parse, but the errors are better.
- Strips a leading byte-order-mark that you sometimes get reading files.
- Has a
noExceptions
method that returns undefined rather than throwing. - Attaches the newline character(s) used to the
Symbol.for('newline')
property on objects and arrays. - Attaches the indentation character(s) used to the
Symbol.for('indent')
property on objects and arrays.
To preserve indentation when the file is saved back to disk, usedata[Symbol.for('indent')]
as the third argument toJSON.stringify
, andif you want to preserve windows\r\n
newlines, replace the\n
chars inthe string withdata[Symbol.for('newline')]
.
For example:
consttxt=awaitreadFile('./package.json','utf8')constdata=parseJsonEvenBetterErrors(txt)constindent=Symbol.for('indent')constnewline=Symbol.for('newline')// .. do some stuff to the data ..conststring=JSON.stringify(data,null,data[indent])+'\n'consteolFixed=data[newline]==='\n' ?string :string.replace(/\n/g,data[newline])awaitwriteFile('./package.json',eolFixed)
Indentation is determined by looking at the whitespace between the initial{
and[
and the character that follows it. If you have lots of weirdinconsistent indentation, then it won't track that or give you any way topreserve it. Whether this is a bug or a feature is debatable ;)
Works just likeJSON.parse
, but will include a bit more information whenan error happens, and attaches aSymbol.for('indent')
andSymbol.for('newline')
on objects and arrays. This throws aJSONParseError
.
Works just likeJSON.parse
, but will returnundefined
rather thanthrowing an error.
Extends the JavaScriptSyntaxError
class to parse the message and providebetter metadata.
Pass in the error thrown by the built-inJSON.parse
, and the text beingparsed, and it'll parse out the bits needed to be helpful.
context
defaults to 20.
Set acaller
function to trim internal implementation details out of thestack trace. When callingparseJson
, this is set to theparseJson
function. If not set, then the constructor defaults to itself, so thestack trace will point to the spot where you callnew JSONParseError
.
About
get better errors
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- JavaScript100.0%