Skip to content

Commit

Permalink
Add custom serializer info
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed May 24, 2016
1 parent 055964c commit b766307
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<p align="center">
<i>Advanced JavaScript objects serialization</i>
</p>
<p align="center">
<a href="https://travis-ci.org/inikulin/replicator"><img alt="Build Status" src="https://api.travis-ci.org/inikulin/replicator.svg"></a>
<a href="https://www.npmjs.com/package/replicator"><img alt="NPM Version" src="https://img.shields.io/npm/v/replicator.svg"></a>
</p>

- Can serialize circular references
- In addition to JSON-serializable types can serialize:
- `undefined`
Expand All @@ -17,8 +22,8 @@
- `Set`<sup>[3](#note3)</sup>
- `ArrayBuffer`<sup>[3](#note4)</sup>
- Typed arrays<sup>[3](#note5)</sup>
- Can be extended with [custom type transforms](#adding-custom-types-support)
- Can use any target serializer under the hood (JSON, BSON, protobuf, etc.)
- [Can be extended with custom type transforms](#adding-custom-types-support)
- [Can use any target serializer under the hood](#changing-serialization-format) (JSON, BSON, protobuf, etc.)

----
<a name="note1">1</a>: If decoding target platform doesn't support encoded error type, it will fallback to `Error` constructor.<br>
Expand All @@ -36,9 +41,13 @@ const Replicator = require('replicator');

const replicator = new Replicator();

const a = {};
a.b = a;

const str = replicator.encode({
key1: new Set([1, 2, 3]),
key2: /\s+/ig
key2: /\s+/ig,
key3: a
});

const obj = replicator.decode(str);
Expand Down Expand Up @@ -109,6 +118,23 @@ console.log(replicator.decode(str));
Built-in types support implemented using transforms, so you can take a look on `replicator` source code for more examples.

## Changing serialization format
By default `replicator` uses JSON under the hood. But you can use any serializer by passing serializer adapter to `Replicator`
constructor. E.g., let's use [BSON](https://www.npmjs.com/package/bson) as serializer:
```js
const Replicator = require('replicator');
const BSON = require('bson');

const replicator = new Replicator({
serialize (val) {
return BSON.serialize(val, false, true, false);
},

deserialize: BSON.deserialize
});

replicator.encode(['yo', 42]);
// > <Buffer>
```

## Author
[Ivan Nikulin](https://github.com/inikulin) ([email protected])
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ var TYPED_ARRAY_SUPPORTED = typeof ArrayBuffer === 'function';
var MAP_SUPPORTED = typeof Map === 'function';
var SET_SUPPORTED = typeof Set === 'function';


// Saved proto functions
var arrSlice = Array.prototype.slice;


// Default serializer
var JSONSerializer = {
serialize: function (val) {
return JSON.stringify(val);
},

deserialize: function (val) {
return JSON.parse(val);
}
};


// EncodingTransformer
var EncodingTransformer = function (val, transforms) {
this.references = val;
Expand Down Expand Up @@ -486,7 +500,7 @@ var builtInTransforms = [
var Replicator = module.exports = function (serializer) {
this.transforms = [];
this.transformsMap = Object.create(null);
this.serializer = serializer || JSON;
this.serializer = serializer || JSONSerializer;

this.addTransforms(builtInTransforms);
};
Expand Down Expand Up @@ -528,11 +542,11 @@ Replicator.prototype.encode = function (val) {
var transformer = new EncodingTransformer(val, this.transforms);
var references = transformer.transform();

return this.serializer.stringify(references);
return this.serializer.serialize(references);
};

Replicator.prototype.decode = function (val) {
var references = this.serializer.parse(val);
var references = this.serializer.deserialize(val);
var transformer = new DecodingTransformer(references, this.transformsMap);

return transformer.transform();
Expand Down

0 comments on commit b766307

Please sign in to comment.