\href{https://github.com/npm/json-parse-even-better-errors}{\texttt{ {\ttfamily json-\/parse-\/even-\/better-\/errors}}} is a Node.\+js library for getting nicer errors out of {\ttfamily JSON.\+parse()}, including context and position of the parse errors.

It also preserves the newline and indentation styles of the JSON data, by putting them in the object or array in the `Symbol.\+for(\textquotesingle{}indent'){\ttfamily and }Symbol.\+for(\textquotesingle{}newline\textquotesingle{})\`{} properties.\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14293}{}\doxysection{Install}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14293}
{\ttfamily \$ npm install -\/-\/save json-\/parse-\/even-\/better-\/errors}\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14294}{}\doxysection{Table of Contents}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14294}

\begin{DoxyItemize}
\item \href{\#example}{\texttt{ Example}}
\item \href{\#features}{\texttt{ Features}}
\item \href{\#contributing}{\texttt{ Contributing}}
\item \href{\#api}{\texttt{ API}}
\begin{DoxyItemize}
\item \href{\#parse}{\texttt{ {\ttfamily parse}}}
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14295}{}\doxysubsection{Example}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14295}

\begin{DoxyCode}{0}
\DoxyCodeLine{const parseJson = require('json-\/parse-\/even-\/better-\/errors')}
\DoxyCodeLine{}
\DoxyCodeLine{parseJson('"{}foo"{}') // returns the string 'foo'}
\DoxyCodeLine{parseJson('garbage') // more useful error message}
\DoxyCodeLine{parseJson.noExceptions('garbage') // returns undefined }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14296}{}\doxysubsection{Features}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14296}

\begin{DoxyItemize}
\item Like JSON.\+parse, but the errors are better.
\item Strips a leading byte-\/order-\/mark that you sometimes get reading files.
\item Has a {\ttfamily no\+Exceptions} method that returns undefined rather than throwing.
\item Attaches the newline character(s) used to the `Symbol.\+for(\textquotesingle{}newline')\`{} property on objects and arrays.
\item Attaches the indentation character(s) used to the `Symbol.\+for(\textquotesingle{}indent')\`{} property on objects and arrays.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14297}{}\doxysection{Indentation}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14297}
To preserve indentation when the file is saved back to disk, use `data\mbox{[}Symbol.\+for(\textquotesingle{}indent')\mbox{]}{\ttfamily as the third argument to}JSON.\+stringify$<$tt$>$, and if you want to preserve windows\textbackslash{}r~\newline
{\ttfamily newlines, replace the}~\newline
{\ttfamily chars in the string with}data\mbox{[}Symbol.\+for(\textquotesingle{}newline\textquotesingle{})\mbox{]}\`{}.

For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const txt = await readFile('./package.json', 'utf8')}
\DoxyCodeLine{const data = parseJsonEvenBetterErrors(txt)}
\DoxyCodeLine{const indent = Symbol.for('indent')}
\DoxyCodeLine{const newline = Symbol.for('newline')}
\DoxyCodeLine{// .. do some stuff to the data ..}
\DoxyCodeLine{const string = JSON.stringify(data, null, data[indent]) + '\(\backslash\)n'}
\DoxyCodeLine{const eolFixed = data[newline] === '\(\backslash\)n' ? string}
\DoxyCodeLine{  : string.replace(/\(\backslash\)n/g, data[newline])}
\DoxyCodeLine{await writeFile('./package.json', eolFixed) }

\end{DoxyCode}


Indentation is determined by looking at the whitespace between the initial {\ttfamily \{} and {\ttfamily \mbox{[}} and the character that follows it. If you have lots of weird inconsistent indentation, then it won\textquotesingle{}t track that or give you any way to preserve it. Whether this is a bug or a feature is debatable ;)\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14298}{}\doxysubsection{API}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14298}
\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14299}{}\doxysubsubsection{$<$a name=\char`\"{}parse\char`\"{}$>$$<$/a$>$ $<$tt$>$parse(txt, reviver = null, context = 20)$<$/tt$>$}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14299}
Works just like {\ttfamily JSON.\+parse}, but will include a bit more information when an error happens, and attaches a `Symbol.\+for(\textquotesingle{}indent'){\ttfamily and }Symbol.\+for(\textquotesingle{}newline\textquotesingle{}){\ttfamily on objects and arrays. This throws a }JSONParse\+Error\`{}.\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14300}{}\doxysubsubsection{$<$a name=\char`\"{}parse\char`\"{}$>$$<$/a$>$ $<$tt$>$parse.\+no\+Exceptions(txt, reviver = null)$<$/tt$>$}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14300}
Works just like {\ttfamily JSON.\+parse}, but will return {\ttfamily undefined} rather than throwing an error.\hypertarget{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14301}{}\doxysubsubsection{$<$a name=\char`\"{}jsonparseerror\char`\"{}$>$$<$/a$>$ $<$tt$>$class JSONParse\+Error(er, text, context = 20, caller = null)$<$/tt$>$}\label{md_sfcode_node_modules_json_parse_even_better_errors_README_autotoc_md14301}
Extends the Java\+Script {\ttfamily Syntax\+Error} class to parse the message and provide better metadata.

Pass in the error thrown by the built-\/in {\ttfamily JSON.\+parse}, and the text being parsed, and it\textquotesingle{}ll parse out the bits needed to be helpful.

{\ttfamily context} defaults to 20.

Set a {\ttfamily caller} function to trim internal implementation details out of the stack trace. When calling {\ttfamily parse\+Json}, this is set to the {\ttfamily parse\+Json} function. If not set, then the constructor defaults to itself, so the stack trace will point to the spot where you call {\ttfamily new JSONParse\+Error}. 