What is serialize-to-js?
The serialize-to-js npm package is used to serialize JavaScript objects into a string format that can be deserialized back into the original object. This is particularly useful for saving the state of an object, transferring data between different parts of an application, or sending data over a network.
What are serialize-to-js's main functionalities?
Basic Serialization
This feature allows you to convert a JavaScript object into a serialized string. The code sample demonstrates how to serialize a simple object with properties 'name' and 'age'.
const serialize = require('serialize-to-js');
const obj = { name: 'John', age: 30 };
const serialized = serialize(obj);
console.log(serialized);
Deserialization
This feature allows you to convert a serialized string back into a JavaScript object. The code sample demonstrates how to deserialize a string back into an object with properties 'name' and 'age'.
const serialize = require('serialize-to-js');
const serialized = '({ name: "John", age: 30 })';
const obj = eval(serialized);
console.log(obj);
Handling Functions
This feature allows you to serialize objects that contain functions. The code sample demonstrates how to serialize an object with a method 'greet'.
const serialize = require('serialize-to-js');
const obj = { greet: function() { return 'Hello'; } };
const serialized = serialize(obj);
console.log(serialized);
Other packages similar to serialize-to-js
json-stringify-safe
The json-stringify-safe package provides a way to safely stringify objects that contain circular references. Unlike serialize-to-js, it focuses on handling circular references rather than serializing functions or other complex data types.
circular-json
The circular-json package is another option for serializing objects with circular references. It is similar to json-stringify-safe but offers different methods for handling circular structures. It does not support serializing functions like serialize-to-js.
flatted
The flatted package is designed to serialize and deserialize JavaScript objects containing circular references. It is a more modern alternative to circular-json and json-stringify-safe, but it does not support serializing functions or other complex data types like serialize-to-js.
serialize-to-js
serialize objects to javascript
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 = { object: {
regexp: /^test?$/,
date: new Date(),
buffer: new Buffer('data'),
number: 3.1415,
string: "test" } };
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
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) 2015 commenthol (MIT License)
See LICENSE for more info.