
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
serialize-to-js
Advanced tools
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.
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);
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.
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.
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 objects to javascript
Serialize objects into a require
-able module while checking circular structures and respecting references.
serialize(source, opts, opts.ignoreCircular, opts.reference)
serializes an object to javascript
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));
//> {object: {regexp: /^test?$/, date: new Date('2015-04-18T20:01:51.903Z'), buffer: new Buffer('ZGF0YQ==', 'base64'), number: 3.1415, string: 'test'}}
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));
//> {object: {regexp: /^test?$/}}
console.log(opts.references);
//> [ [ 'reference', 'object' ] ]
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(source, opts, opts.ignoreCircular, opts.reference, opts.beautify)
serialize to a module which can be require
ed.
var serialTM = require('serialize-to-js').serializeToModule;
var obj = { object: { regexp: /^test?$/ } };
obj.reference = obj.object;
console.log(serialTM(obj, { reference: true }));
//> var m = module.exports = {
//> object: {
//> regexp: /^test?$/
//> }
//> };
//> m.reference = m.object;
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
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.
Copyright (c) 2015 commenthol (MIT License)
See LICENSE for more info.
FAQs
serialize objects to javascript
We found that serialize-to-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.