1 | # TeleJSON
|
2 |
|
3 | A library for teleporting rich data to another place.
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```sh
|
8 | yarn add telejson
|
9 | ```
|
10 |
|
11 | ## What can it do, what can't it do:
|
12 |
|
13 | `JSON.parse` & `JSON.stringify` have limitation by design, because there are no data formats for things like
|
14 |
|
15 | - Date
|
16 | - Function
|
17 | - Class
|
18 | - Symbol
|
19 | - Error
|
20 | - etc.
|
21 |
|
22 | Also JSON doesn't support cyclic data structures.
|
23 |
|
24 | This library allows you to pass in data with all of the above properties.
|
25 | It will transform the properties to something that's allowed by the JSON spec whilst stringifying,
|
26 | and then convert back to the cyclic data structure when parsing.
|
27 |
|
28 | When parsing, **class instances** will be given the Class's name again.
|
29 | The prototype isn't copied over.
|
30 |
|
31 | **Functions** are supported, they are stringified and will be eval-ed when called.
|
32 | This lazy eval is important for performance.
|
33 | The eval happens via `eval()`
|
34 | Functions are stripped of comments and whitespace.
|
35 |
|
36 | > Obviously calling the function will only really work as expected if the functions were pure the begin with.
|
37 |
|
38 | **Regular expressions** just work.
|
39 |
|
40 | **Symbol** will be re-created with the same string. (resulting in a similar, but different symbol)
|
41 |
|
42 | **Dates** are parsed back into actual Date objects.
|
43 |
|
44 | **DOM Events** are processed to extract the internal (hidden) properties, resulting in an object containing the same properties but not being an instance of the original class.
|
45 |
|
46 | ## API
|
47 |
|
48 | You have 2 choices:
|
49 |
|
50 | ```js
|
51 | import { stringify, parse } from 'telejson';
|
52 |
|
53 | const Foo = function () {};
|
54 |
|
55 | const root = {
|
56 | date: new Date('2018'),
|
57 | regex1: /foo/,
|
58 | regex2: /foo/g,
|
59 | regex2: new RegExp('foo', 'i'),
|
60 | fn1: () => 'foo',
|
61 | fn2: function fn2() {
|
62 | return 'foo';
|
63 | },
|
64 | Foo: new Foo(),
|
65 | };
|
66 |
|
67 | // something cyclic
|
68 | root.root = root;
|
69 |
|
70 | const stringified = stringify(root);
|
71 | const parsed = parse(stringified);
|
72 | ```
|
73 |
|
74 | stringify and parse do not conform to the JSON.stringify or JSON.parse api.
|
75 | they take an data object and a option object.
|
76 |
|
77 | OR you can use use the `replacer` and `reviver`:
|
78 |
|
79 | ```js
|
80 | import { replacer, reviver } from 'telejson';
|
81 | import data from 'somewhere';
|
82 |
|
83 | const stringified = JSON.stringify(data, replacer(), 2);
|
84 | const parsed = JSON.parse(stringified, reviver(), 2);
|
85 | ```
|
86 |
|
87 | notice that both replacer and reviver need to be called! doing the following will NOT WORK:
|
88 |
|
89 | ```
|
90 | const stringified = JSON.stringify(data, replacer, 2);
|
91 | const parsed = JSON.parse(stringified, reviver, 2);
|
92 | ```
|
93 |
|
94 | ## options
|
95 |
|
96 | You either pass the options-object to `replacer` or as a second argument to `stringify`:
|
97 |
|
98 | ```js
|
99 | replacer({ maxDepth: 10 });
|
100 | stringify(date, { maxDepth: 10 });
|
101 | ```
|
102 |
|
103 | ### replacer
|
104 |
|
105 | `maxDepth`: controls how deep to keep stringifying. When max depth is reach,
|
106 | objects will be replaced with `"[Object]"`, arrays will be replaced with `"[Array(<length>)]"`.
|
107 | default value is `10`
|
108 | This option is really useful if your object is huge/complex, and you don't care about the deeply nested data.
|
109 |
|
110 | `space`: controls how to prettify the output string.
|
111 | default value is `undefined`, no white space is used.
|
112 | Only relevant when using `stringify`.
|
113 |
|
114 | `allowFunction`: When set to false, functions will not be serialized. (default = true)
|
115 |
|
116 | `allowRegExp`: When set to false, regular expressions will not be serialized. (default = true)
|
117 |
|
118 | `allowClass`: When set to false, class instances will not be serialized. (default = true)
|
119 |
|
120 | `allowError`: When set to false, error instances will not be serialized. (default = true)
|
121 |
|
122 | `allowDate`: When set to false, Date objects will not be serialized. (default = true)
|
123 |
|
124 | `allowUndefined`: When set to false, `undefined` will not be serialized. (default = true)
|
125 |
|
126 | `allowSymbol`: When set to false, Symbols will not be serialized. (default = true)
|
127 |
|
128 | ### reviver
|
129 |
|
130 | `lazyEval`: When set to false, lazy eval will be disabled. (default true)
|
131 |
|
132 | Note: disabling lazy eval will affect performance. Consider disabling it only if you truly need to.
|
133 |
|
134 | ## Requirements
|
135 |
|
136 | `telejson` depends on the collection type `Map`. If you support older browsers and devices which may not yet provide these natively (e.g. IE < 11) or which have non-compliant implementations (e.g. IE 11), consider including a global polyfill in your bundled application, such as `core-js` or `babel-polyfill`.
|
137 |
|
138 | ## Contributing
|
139 |
|
140 | If you have any suggestions, please open an issue.
|
141 |
|
142 | All contributions are welcome!
|
143 |
|
144 | ### run tests:
|
145 |
|
146 | first, build the package:
|
147 |
|
148 | ```sh
|
149 | yarn build
|
150 | ```
|
151 |
|
152 | then run the tests:
|
153 |
|
154 | ```sh
|
155 | yarn test
|
156 | ```
|
157 |
|
\ | No newline at end of file |