UNPKG

1.38 kBMarkdownView Raw
1# jest-serializer
2
3Module 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
13Three kinds of API groups are exposed:
14
15### In-memory serialization: `serialize` and `deserialize`
16
17This 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
20import {deserialize, serialize} from 'jest-serializer';
21
22const myObject = {
23 foo: 'bar',
24 baz: [0, true, '2', [], {}],
25};
26
27const buffer = serialize(myObject);
28const myCopyObject = deserialize(buffer);
29```
30
31### Synchronous persistent filesystem: `readFileSync` and `writeFileSync`
32
33This 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
36import {readFileSync, writeFileSync} from 'jest-serializer';
37
38const myObject = {
39 foo: 'bar',
40 baz: [0, true, '2', [], {}],
41};
42
43const myFile = '/tmp/obj';
44
45writeFileSync(myFile, myObject);
46const myCopyObject = readFileSync(myFile);
47```