serialize-to-js
serialize objects to javascript
![Build Status](https://secure.travis-ci.org/commenthol/serialize-to-js.svg?branch=master)
Serialize objects into a require
-able module while checking circular structures and respecting references.
Table of Contents
Methods
serialize
serialize(source, opts, opts.ignoreCircular, opts.reference)
serializes an object to javascript
Example - serializing regex, date, buffer, ...
var serialize = require('serialize-to-js').serialize;
var obj = {
str: '<script>var a = 0 > 1</script>',
num: 3.1415,
bool: true,
nil: null,
undef: undefined,
obj: { foo: 'bar' },
arr: [1, '2'],
regexp: /^test?$/,
date: new Date(),
buffer: new Buffer('data'),
}
console.log(serialize(obj))
Example - serializing while respecting references
var serialize = require('serialize-to-js').serialize;
var obj = { object: { regexp: /^test?$/ } };
obj.reference = obj.object;
var opts = { reference: true };
console.log(serialize(obj, opts));
console.log(opts.references);
Parameters
source: Object | Array | function | Any
, source to serialize
opts: Object
, options
opts.ignoreCircular: Boolean
, ignore circular objects
opts.reference: Boolean
, reference instead of a copy (requires post-processing of opts.references)
Returns: String
, serialized representation of source
deserialize
deserialize(str)
deserialize a serialized object to javascript
Example - serializing regex, date, buffer, ...
var str = '{obj: {foo: "bar"}, arr: [1, "2"], regexp: /^test?$/, date: new Date("2016-04-15T16:22:52.009Z")}'
var res = deserialize(str)
console.log(res)
Parameters
str: String
, string containing serilaized data
Returns: Any
, deserialized data
serializeToModule
serializeToModule(source, opts, opts.ignoreCircular, opts.reference, opts.beautify)
serialize to a module which can be require
ed.
Example - serializing while respecting references
var serialTM = require('serialize-to-js').serializeToModule;
var obj = { object: { regexp: /^test?$/ } };
obj.reference = obj.object;
console.log(serialTM(obj, { reference: true }));
Parameters
source: Object | Array | function | Any
, source to serialize
opts: Object
, options
opts.ignoreCircular: Boolean
, ignore circular objects
opts.reference: Boolean
, reference instead of a copy (requires post-processing of opts.references)
opts.beautify: Boolean | Object
, beautify output - default is false
. If Object then use je-beautify options.
Returns: String
, serialized representation of source
as module
Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your
code to be distributed under the MIT license. You are also implicitly
verifying that all code is your original work or correctly attributed
with the source of its origin and licence.
License
Copyright (c) 2016 commenthol (MIT License)
See LICENSE for more info.