3 [![Downloads](https://img.shields.io/npm/dm/flatted.svg)](https://www.npmjs.com/package/flatted) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/flatted/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/flatted?branch=main) [![Build Status](https://travis-ci.com/WebReflection/flatted.svg?branch=main)](https://travis-ci.com/WebReflection/flatted) [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC) ![WebReflection status](https://offline.report/status/webreflection.svg)
5 ![snow flake](./flatted.jpg)
7 <sup>**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**</sup>
11 There is a standard approach to recursion and more data-types than what JSON allow, and it's part of this [Structured Clone Module](https://github.com/ungap/structured-clone/#readme).
13 Beside acting as a polyfill, its `@ungap/structured-clone/json` export provides both `stringify` and `parse`, and it's been tested for being faster than *flatted*, but its produced output is also smaller than *flatted*.
15 The *@ungap/structured-clone* module is, in short, a drop in replacement for *flatted*, but it's not compatible with *flatted* specialized syntax.
17 However, if recursion, as well as more data-types, are what you are after, or interesting for your projects, consider switching to this new module whenever you can 👍
21 A super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).
23 Now available also for **[PHP](./php/flatted.php)**.
29 Usable via [CDN](https://unpkg.com/flatted) or as regular module.
33 import {parse, stringify, toJSON, fromJSON} from 'flatted';
36 const {parse, stringify, toJSON, fromJSON} = require('flatted');
42 stringify(a); // [["1","0"],{"a":"0"}]
45 ## toJSON and from JSON
47 If you'd like to implicitly survive JSON serialization, these two helpers helps:
50 import {toJSON, fromJSON} from 'flatted';
52 class RecursiveMap extends Map {
53 static fromJSON(any) {
54 return new this(fromJSON(any));
57 return toJSON([...this.entries()]);
61 const recursive = new RecursiveMap;
64 recursive.set('same', same);
66 const asString = JSON.stringify(recursive);
67 const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
68 asMap.get('same') === asMap.get('same').same;
75 As it is for every other specialized format capable of serializing and deserializing circular data, you should never `JSON.parse(Flatted.stringify(data))`, and you should never `Flatted.parse(JSON.stringify(data))`.
77 The only way this could work is to `Flatted.parse(Flatted.stringify(data))`, as it is also for _CircularJSON_ or any other, otherwise there's no granted data integrity.
79 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't be serialized and unserialized as expected.
82 ### New in V1: Exact same JSON API
84 * Added a [reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Syntax) parameter to `.parse(string, reviver)` and revive your own objects.
85 * Added a [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Syntax) and a `space` parameter to `.stringify(object, replacer, space)` for feature parity with JSON signature.
89 All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
92 ### How does it work ?
93 While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
95 Once parsed, all indexes will be replaced through the flattened collection.
97 <sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
101 var a = [{one: 1}, {two: '2'}];
103 // a is the main object, will be at index '0'
104 // {one: 1} is the second object, index '1'
105 // {two: '2'} the third, in '2', and it has a string
106 // which will be found at index '3'
108 Flatted.stringify(a);
109 // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
110 // a[one,two] {one: 1, a} {two: '2'} '2'