[d565449] | 1 | # flatted
|
---|
| 2 |
|
---|
| 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)
|
---|
| 4 |
|
---|
| 5 | ![snow flake](./flatted.jpg)
|
---|
| 6 |
|
---|
| 7 | <sup>**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**</sup>
|
---|
| 8 |
|
---|
| 9 | A super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).
|
---|
| 10 |
|
---|
| 11 | Available also for **[PHP](./php/flatted.php)**.
|
---|
| 12 |
|
---|
| 13 | Available also for **[Python](./python/flatted.py)**.
|
---|
| 14 |
|
---|
| 15 | - - -
|
---|
| 16 |
|
---|
| 17 | ## Announcement 📣
|
---|
| 18 |
|
---|
| 19 | There is a standard approach to recursion and more data-types than what JSON allows, and it's part of the [Structured Clone polyfill](https://github.com/ungap/structured-clone/#readme).
|
---|
| 20 |
|
---|
| 21 | 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* in general.
|
---|
| 22 |
|
---|
| 23 | The *@ungap/structured-clone* module is, in short, a drop in replacement for *flatted*, but it's not compatible with *flatted* specialized syntax.
|
---|
| 24 |
|
---|
| 25 | However, if recursion, as well as more data-types, are what you are after, or interesting for your projects/use cases, consider switching to this new module whenever you can 👍
|
---|
| 26 |
|
---|
| 27 | - - -
|
---|
| 28 |
|
---|
| 29 | ```js
|
---|
| 30 | npm i flatted
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | Usable via [CDN](https://unpkg.com/flatted) or as regular module.
|
---|
| 34 |
|
---|
| 35 | ```js
|
---|
| 36 | // ESM
|
---|
| 37 | import {parse, stringify, toJSON, fromJSON} from 'flatted';
|
---|
| 38 |
|
---|
| 39 | // CJS
|
---|
| 40 | const {parse, stringify, toJSON, fromJSON} = require('flatted');
|
---|
| 41 |
|
---|
| 42 | const a = [{}];
|
---|
| 43 | a[0].a = a;
|
---|
| 44 | a.push(a);
|
---|
| 45 |
|
---|
| 46 | stringify(a); // [["1","0"],{"a":"0"}]
|
---|
| 47 | ```
|
---|
| 48 |
|
---|
| 49 | ## toJSON and fromJSON
|
---|
| 50 |
|
---|
| 51 | If you'd like to implicitly survive JSON serialization, these two helpers helps:
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | import {toJSON, fromJSON} from 'flatted';
|
---|
| 55 |
|
---|
| 56 | class RecursiveMap extends Map {
|
---|
| 57 | static fromJSON(any) {
|
---|
| 58 | return new this(fromJSON(any));
|
---|
| 59 | }
|
---|
| 60 | toJSON() {
|
---|
| 61 | return toJSON([...this.entries()]);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | const recursive = new RecursiveMap;
|
---|
| 66 | const same = {};
|
---|
| 67 | same.same = same;
|
---|
| 68 | recursive.set('same', same);
|
---|
| 69 |
|
---|
| 70 | const asString = JSON.stringify(recursive);
|
---|
| 71 | const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
|
---|
| 72 | asMap.get('same') === asMap.get('same').same;
|
---|
| 73 | // true
|
---|
| 74 | ```
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | ## Flatted VS JSON
|
---|
| 78 |
|
---|
| 79 | 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))`.
|
---|
| 80 |
|
---|
| 81 | 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.
|
---|
| 82 |
|
---|
| 83 | 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.
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | ### New in V1: Exact same JSON API
|
---|
| 87 |
|
---|
| 88 | * 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.
|
---|
| 89 | * 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.
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | ### Compatibility
|
---|
| 93 | All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | ### How does it work ?
|
---|
| 97 | While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
|
---|
| 98 |
|
---|
| 99 | Once parsed, all indexes will be replaced through the flattened collection.
|
---|
| 100 |
|
---|
| 101 | <sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
|
---|
| 102 |
|
---|
| 103 | ```js
|
---|
| 104 | // logic example
|
---|
| 105 | var a = [{one: 1}, {two: '2'}];
|
---|
| 106 | a[0].a = a;
|
---|
| 107 | // a is the main object, will be at index '0'
|
---|
| 108 | // {one: 1} is the second object, index '1'
|
---|
| 109 | // {two: '2'} the third, in '2', and it has a string
|
---|
| 110 | // which will be found at index '3'
|
---|
| 111 |
|
---|
| 112 | Flatted.stringify(a);
|
---|
| 113 | // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
|
---|
| 114 | // a[one,two] {one: 1, a} {two: '2'} '2'
|
---|
| 115 | ```
|
---|