版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # flatted
  2. [![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)
  3. ![snow flake](./flatted.jpg)
  4. <sup>**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**</sup>
  5. ## Announcement 📣
  6. 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).
  7. 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.
  8. The *@ungap/structured-clone* module is, in short, a drop in replacement for *flatted*, but it's not compatible with *flatted* specialized syntax.
  9. 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 👍
  10. - - -
  11. A super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).
  12. Now available also for **[PHP](./php/flatted.php)**.
  13. ```js
  14. npm i flatted
  15. ```
  16. Usable via [CDN](https://unpkg.com/flatted) or as regular module.
  17. ```js
  18. // ESM
  19. import {parse, stringify, toJSON, fromJSON} from 'flatted';
  20. // CJS
  21. const {parse, stringify, toJSON, fromJSON} = require('flatted');
  22. const a = [{}];
  23. a[0].a = a;
  24. a.push(a);
  25. stringify(a); // [["1","0"],{"a":"0"}]
  26. ```
  27. ## toJSON and fromJSON
  28. If you'd like to implicitly survive JSON serialization, these two helpers helps:
  29. ```js
  30. import {toJSON, fromJSON} from 'flatted';
  31. class RecursiveMap extends Map {
  32. static fromJSON(any) {
  33. return new this(fromJSON(any));
  34. }
  35. toJSON() {
  36. return toJSON([...this.entries()]);
  37. }
  38. }
  39. const recursive = new RecursiveMap;
  40. const same = {};
  41. same.same = same;
  42. recursive.set('same', same);
  43. const asString = JSON.stringify(recursive);
  44. const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
  45. asMap.get('same') === asMap.get('same').same;
  46. // true
  47. ```
  48. ## Flatted VS JSON
  49. 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))`.
  50. 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.
  51. 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.
  52. ### New in V1: Exact same JSON API
  53. * 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.
  54. * 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.
  55. ### Compatibility
  56. All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
  57. ### How does it work ?
  58. While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
  59. Once parsed, all indexes will be replaced through the flattened collection.
  60. <sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
  61. ```js
  62. // logic example
  63. var a = [{one: 1}, {two: '2'}];
  64. a[0].a = a;
  65. // a is the main object, will be at index '0'
  66. // {one: 1} is the second object, index '1'
  67. // {two: '2'} the third, in '2', and it has a string
  68. // which will be found at index '3'
  69. Flatted.stringify(a);
  70. // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
  71. // a[one,two] {one: 1, a} {two: '2'} '2'
  72. ```