diff --git a/README.md b/README.md
index 88b6009..28bddf3 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,11 @@
Advanced JavaScript objects serialization
+
+
+
+
+
- Can serialize circular references
- In addition to JSON-serializable types can serialize:
- `undefined`
@@ -17,8 +22,8 @@
- `Set`[3](#note3)
- `ArrayBuffer`[3](#note4)
- Typed arrays[3](#note5)
-- 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.)
----
1: If decoding target platform doesn't support encoded error type, it will fallback to `Error` constructor.
@@ -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);
@@ -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]);
+// >
+```
## Author
[Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)
diff --git a/index.js b/index.js
index e65bbdf..6d67ff2 100644
--- a/index.js
+++ b/index.js
@@ -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;
@@ -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);
};
@@ -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();