Universal Serialize
Universal serializer and deserializer, which supports many build-in javascript types, and any number of custom types you want to support
Simple serialization
import { serialize, deserialize } from 'universal-serialize';
const originalObject = {
foo: 'bar',
date: new Date(),
error: new Error('world')
};
const jsonString = serialize(originalObject);
const deserializedObject = deserialize(jsonString);
console.log(
deserializedObject.foo,
deserializedObject.date.toUTCString(),
deserializedObject.error.stack
);
By default universal serialize will serialize:
- Errors
- Regexes
- Dates
- Arrays
- Objects
- Strings
- Numbers
- Booleans
- Nulls
Custom serialization
import { serialize, deserialize, serializeType, TYPE } from 'universal-serialize';
const SERIALIZED_FUNCTION = 'SERIALIZED_FUNCTION';
const originalObject = {
sayHello: () => {
console.log('Hello world!');
};
};
const jsonString = serialize(originalObject, {
[ TYPE.FUNCTION ]: (val) => {
return serializeType(SERIALIZED_FUNCTION, val.toString());
}
});
const deserializedObject = deserialize(jsonString, {
[ SERIALIZED_FUNCTION ]: (fnString) => {
return eval(fnString);
}
});
deserializedObject.sayHello();
Quick Start
Getting Started
- Fork the module
- Run setup:
npm install
- Start editing code in
./src
and writing tests in ./tests
npm run build
Building
npm run build
Tests
Testing with different/multiple browsers
npm run karma -- --browser=PhantomJS
npm run karma -- --browser=Chrome
npm run karma -- --browser=Safari
npm run karma -- --browser=Firefox
npm run karma -- --browser=PhantomJS,Chrome,Safari,Firefox
Keeping the browser open after tests
npm run karma -- --browser=Chrome --keep-open
Publishing
Before you publish for the first time:
- Delete the example code in
./src
, ./test/tests
and ./demo
- Edit the module name in
package.json
- Edit
README.md
and CONTRIBUTING.md
Then:
- Publish your code:
npm run release
to add a patch
- Or
npm run release:path
, npm run release:minor
, npm run release:major