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 string while checking circular structures and respecting references.
The following Objects are supported
- String
- Number
- Boolean
- Object
- Array
- RegExp
- Error
- Date
- Buffer
- Int8Array, Uint8Array, Uint8ClampedArray
- Int16Array, Uint16Array
- Int32Array, Uint32Array, Float32Array
- Float64Array
- Set
- Map
Table of Contents
Methods
serialize
serialize(source, opts, opts.ignoreCircular, opts.reference)
serializes an object to javascript
Example - serializing regex, date, buffer, ...
const serialize = require('serialize-to-js')
const 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'),
set: new Set([1, 2, 3]),
map: new Map([['a': 1],['b': 2]])
}
console.log(serialize(obj))
Example - serializing while respecting references
var serialize = require('serialize-to-js')
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)
opts.unsafe: Boolean
, do not escape chars <>/
Returns: String
, serialized representation of source
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.