Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

get better errors

License

NotificationsYou must be signed in to change notification settings

npm/json-parse-even-better-errors

 
 

Repository files navigation

json-parse-even-better-errorsis 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.

Install

$ npm install --save json-parse-even-better-errors

Table of Contents

Example

constparseJson=require('json-parse-even-better-errors')parseJson('"foo"')// returns the string 'foo'parseJson('garbage')// more useful error messageparseJson.noExceptions('garbage')// returns undefined

Features

  • Like JSON.parse, but the errors are better.
  • Strips a leading byte-order-mark that you sometimes get reading files.
  • Has anoExceptions method that returns undefined rather than throwing.
  • Attaches the newline character(s) used to theSymbol.for('newline')property on objects and arrays.
  • Attaches the indentation character(s) used to theSymbol.for('indent')property on objects and arrays.

Indentation

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 ;)

API

parse(txt, reviver = null, context = 20)

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.

parse.noExceptions(txt, reviver = null)

Works just likeJSON.parse, but will returnundefined rather thanthrowing an error.

class JSONParseError(er, text, context = 20, caller = null)

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 theparseJsonfunction. 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

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp