 \href{https://coveralls.io/github/WebReflection/flatted?branch=master}{\texttt{ }} \href{https://travis-ci.org/WebReflection/flatted}{\texttt{ }} \href{https://opensource.org/licenses/ISC}{\texttt{ }} 

A super light (0.\+5K) and fast circular JSON parser, directly from the creator of \href{https://github.com/WebReflection/circular-json/\#circularjson}{\texttt{ Circular\+JSON}}.


\begin{DoxyCode}{0}
\DoxyCodeLine{npm i flatted }

\end{DoxyCode}


Usable via \href{https://unpkg.com/flatted}{\texttt{ CDN}} or as regular module.


\begin{DoxyCode}{0}
\DoxyCodeLine{// ESM}
\DoxyCodeLine{import \{parse, stringify\} from 'flatted/esm';}
\DoxyCodeLine{}
\DoxyCodeLine{// CJS}
\DoxyCodeLine{const \{parse, stringify\} = require('flatted/cjs');}
\DoxyCodeLine{}
\DoxyCodeLine{const a = [\{\}];}
\DoxyCodeLine{a[0].a = a;}
\DoxyCodeLine{a.push(a);}
\DoxyCodeLine{}
\DoxyCodeLine{stringify(a); // [["{}1"{},"{}0"{}],\{"{}a"{}:"{}0"{}\}] }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_flatted_README_autotoc_md10802}{}\doxysection{Flatted VS JSON}\label{md_sfcode_node_modules_flatted_README_autotoc_md10802}
As it is for every other specialized format capable of serializing and deserializing circular data, you should never {\ttfamily JSON.\+parse(Flatted.\+stringify(data))}, and you should never {\ttfamily Flatted.\+parse(JSON.\+stringify(data))}.

The only way this could work is to {\ttfamily Flatted.\+parse(Flatted.\+stringify(data))}, as it is also for {\itshape Circular\+JSON} or any other, otherwise there\textquotesingle{}s no granted data integrity.

Also please note this project serializes and deserializes only data compatible with JSON, so that sockets, or anything else with internal classes different from those allowed by JSON standard, won\textquotesingle{}t be serialized and unserialized as expected.\hypertarget{md_sfcode_node_modules_flatted_README_autotoc_md10803}{}\doxysubsection{New in V1\+: Exact same JSON API}\label{md_sfcode_node_modules_flatted_README_autotoc_md10803}

\begin{DoxyItemize}
\item Added a \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse\#Syntax}{\texttt{ reviver}} parameter to {\ttfamily .parse(string, reviver)} and revive your own objects.
\item Added a \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify\#Syntax}{\texttt{ replacer}} and a {\ttfamily space} parameter to {\ttfamily .stringify(object, replacer, space)} for feature parity with JSON signature.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_flatted_README_autotoc_md10804}{}\doxysubsection{Compatibility}\label{md_sfcode_node_modules_flatted_README_autotoc_md10804}
All ECMAScript engines compatible with {\ttfamily Map}, {\ttfamily Set}, {\ttfamily Object.\+keys}, and {\ttfamily Array.\+prototype.\+reduce} will work, even if polyfilled.\hypertarget{md_sfcode_node_modules_flatted_README_autotoc_md10805}{}\doxysubsection{How does it work ?}\label{md_sfcode_node_modules_flatted_README_autotoc_md10805}
While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. {\ttfamily $\ast$}

Once parsed, all indexes will be replaced through the flattened collection.

\textsuperscript{\textsubscript{{\ttfamily $\ast$} represented as string to avoid conflicts with numbers}}


\begin{DoxyCode}{0}
\DoxyCodeLine{// logic example}
\DoxyCodeLine{var a = [\{one: 1\}, \{two: '2'\}];}
\DoxyCodeLine{a[0].a = a;}
\DoxyCodeLine{// a is the main object, will be at index '0'}
\DoxyCodeLine{// \{one: 1\} is the second object, index '1'}
\DoxyCodeLine{// \{two: '2'\} the third, in '2', and it has a string}
\DoxyCodeLine{// which will be found at index '3'}
\DoxyCodeLine{}
\DoxyCodeLine{Flatted.stringify(a);}
\DoxyCodeLine{// [["{}1"{},"{}2"{}],\{"{}one"{}:1,"{}a"{}:"{}0"{}\},\{"{}two"{}:"{}3"{}\},"{}2"{}]}
\DoxyCodeLine{// a[one,two]    \{one: 1, a\}    \{two: '2'\}  '2' }

\end{DoxyCode}
 