flatted

Downloadsarrow-up-right Coverage Statusarrow-up-right Build Statusarrow-up-right License: ISCarrow-up-right WebReflection status

snow flake

Social Media Photo by Matt Seymourarrow-up-right on Unsplasharrow-up-right

A super light (0.5K) and fast circular JSON parser, directly from the creator of CircularJSONarrow-up-right.

Available also for PHParrow-up-right.

Available also for Pythonarrow-up-right.


Announcement 📣

There is a standard approach to recursion and more data-types than what JSON allows, and it's part of the Structured Clone polyfillarrow-up-right.

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.

The @ungap/structured-clone module is, in short, a drop in replacement for flatted, but it's not compatible with flatted specialized syntax.

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 👍


Usable via CDNarrow-up-right or as regular module.

toJSON and fromJSON

If you'd like to implicitly survive JSON serialization, these two helpers helps:

Flatted VS JSON

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

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.

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.

New in V1: Exact same JSON API

  • Added a reviverarrow-up-right parameter to .parse(string, reviver) and revive your own objects.

  • Added a replacerarrow-up-right and a space parameter to .stringify(object, replacer, space) for feature parity with JSON signature.

Compatibility

All ECMAScript engines compatible with Map, Set, Object.keys, and Array.prototype.reduce will work, even if polyfilled.

How does it work ?

While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. *

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

* represented as string to avoid conflicts with numbers

Last updated