1 | # jest-serializer
|
2 |
|
3 | Module for serializing and deserializing object into memory and disk. By default, the `v8` implementations are used, but if not present, it defaults to `JSON` implementation. Both serializers have the advantage of being able to serialize `Map`, `Set`, `undefined`, `NaN`, etc, although the JSON one does it through a replacer/reviver.
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```sh
|
8 | $ yarn add jest-serializer
|
9 | ```
|
10 |
|
11 | ## API
|
12 |
|
13 | Three kinds of API groups are exposed:
|
14 |
|
15 | ### In-memory serialization: `serialize` and `deserialize`
|
16 |
|
17 | This set of functions take or return a `Buffer`. All the process happens in memory. This is useful when willing to transfer over HTTP, TCP or via UNIX pipes.
|
18 |
|
19 | ```javascript
|
20 | import {deserialize, serialize} from 'jest-serializer';
|
21 |
|
22 | const myObject = {
|
23 | foo: 'bar',
|
24 | baz: [0, true, '2', [], {}],
|
25 | };
|
26 |
|
27 | const buffer = serialize(myObject);
|
28 | const myCopyObject = deserialize(buffer);
|
29 | ```
|
30 |
|
31 | ### Synchronous persistent filesystem: `readFileSync` and `writeFileSync`
|
32 |
|
33 | This set of functions allow to send to disk a serialization result and retrieve it back, in a synchronous way. It mimics the `fs` API so it looks familiar.
|
34 |
|
35 | ```javascript
|
36 | import {readFileSync, writeFileSync} from 'jest-serializer';
|
37 |
|
38 | const myObject = {
|
39 | foo: 'bar',
|
40 | baz: [0, true, '2', [], {}],
|
41 | };
|
42 |
|
43 | const myFile = '/tmp/obj';
|
44 |
|
45 | writeFileSync(myFile, myObject);
|
46 | const myCopyObject = readFileSync(myFile);
|
47 | ```
|