{"id":13447221,"url":"https://github.com/WebReflection/flatted","last_synced_at":"2025-03-21T17:30:57.085Z","repository":{"id":31979209,"uuid":"131121620","full_name":"WebReflection/flatted","owner":"WebReflection","description":"A fast and minimal circular JSON parser.","archived":false,"fork":false,"pushed_at":"2025-02-18T08:54:45.000Z","size":898,"stargazers_count":1072,"open_issues_count":2,"forks_count":49,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-03-15T21:51:17.634Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"custom":"https://www.patreon.com/webreflection"}},"created_at":"2018-04-26T08:05:51.000Z","updated_at":"2025-03-13T13:28:40.000Z","dependencies_parsed_at":"2024-02-04T16:20:01.867Z","dependency_job_id":"c82bb2cb-948b-4bf4-b62b-49eb4f6d1665","html_url":"https://github.com/WebReflection/flatted","commit_stats":{"total_commits":120,"total_committers":12,"mean_commits":10.0,"dds":"0.31666666666666665","last_synced_commit":"866032c11960f3fc6a46b37b1e4eeed22d616f2a"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fflatted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fflatted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fflatted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fflatted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/flatted/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244706533,"owners_count":20496571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T05:01:11.261Z","updated_at":"2025-03-21T17:30:56.750Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","readme":"# flatted\n\n[![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)\n\n![snow flake](./flatted.jpg)\n\n\u003csup\u003e**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**\u003c/sup\u003e\n\nA super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).\n\nAvailable also for **[PHP](./php/flatted.php)**.\n\nAvailable also for **[Python](./python/flatted.py)**.\n\n- - -\n\n## Announcement 📣\n\nThere 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).\n\nBeside 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.\n\nThe *@ungap/structured-clone* module is, in short, a drop in replacement for *flatted*, but it's not compatible with *flatted* specialized syntax.\n\nHowever, 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 👍\n\n- - -\n\n```js\nnpm i flatted\n```\n\nUsable via [CDN](https://unpkg.com/flatted) or as regular module.\n\n```js\n// ESM\nimport {parse, stringify, toJSON, fromJSON} from 'flatted';\n\n// CJS\nconst {parse, stringify, toJSON, fromJSON} = require('flatted');\n\nconst a = [{}];\na[0].a = a;\na.push(a);\n\nstringify(a); // [[\"1\",\"0\"],{\"a\":\"0\"}]\n```\n\n## toJSON and fromJSON\n\nIf you'd like to implicitly survive JSON serialization, these two helpers helps:\n\n```js\nimport {toJSON, fromJSON} from 'flatted';\n\nclass RecursiveMap extends Map {\n  static fromJSON(any) {\n    return new this(fromJSON(any));\n  }\n  toJSON() {\n    return toJSON([...this.entries()]);\n  }\n}\n\nconst recursive = new RecursiveMap;\nconst same = {};\nsame.same = same;\nrecursive.set('same', same);\n\nconst asString = JSON.stringify(recursive);\nconst asMap = RecursiveMap.fromJSON(JSON.parse(asString));\nasMap.get('same') === asMap.get('same').same;\n// true\n```\n\n\n## Flatted VS JSON\n\nAs 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))`.\n\nThe 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.\n\nAlso 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.\n\n\n### New in V1: Exact same JSON API\n\n  * 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.\n  * 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.\n\n\n### Compatibility\nAll ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.\n\n\n### How does it work ?\nWhile stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`\n\nOnce parsed, all indexes will be replaced through the flattened collection.\n\n\u003csup\u003e\u003csub\u003e`*` represented as string to avoid conflicts with numbers\u003c/sub\u003e\u003c/sup\u003e\n\n```js\n// logic example\nvar a = [{one: 1}, {two: '2'}];\na[0].a = a;\n// a is the main object, will be at index '0'\n// {one: 1} is the second object, index '1'\n// {two: '2'} the third, in '2', and it has a string\n// which will be found at index '3'\n\nFlatted.stringify(a);\n// [[\"1\",\"2\"],{\"one\":1,\"a\":\"0\"},{\"two\":\"3\"},\"2\"]\n// a[one,two]    {one: 1, a}    {two: '2'}  '2'\n```\n","funding_links":["https://www.patreon.com/webreflection"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebReflection%2Fflatted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWebReflection%2Fflatted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebReflection%2Fflatted/lists"}