# serialize-to-js

> serialize objects to javascript

Latest version **3.1.2** (published 2022-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install serialize-to-js
pnpm add serialize-to-js
yarn add serialize-to-js
bun add serialize-to-js
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.2 |
| Published | 2022-04-03 |
| First published | 2015-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=4.0.0 |
| Dependencies | 0 |
| Unpacked size | 25.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | commenthol |
| Maintainers | commenthol |
| Keywords | javascript, objects, serialize |

## Links

- npm: https://www.npmjs.com/package/serialize-to-js
- Repository: https://github.com/commenthol/serialize-to-js
- Issues: https://github.com/commenthol/serialize-to-js/issues
- npm.io page: https://npm.io/package/serialize-to-js

## Recent versions

- 3.1.2 (latest) — 2022-04-03
- 3.1.1 — 2020-01-31
- 3.1.0 — 2020-01-27
- 3.0.3 — 2020-01-27
- 3.0.2 — 2019-12-07
- 3.0.1 — 2019-12-06
- 3.0.0 — 2019-05-26
- 2.0.1 — 2019-05-18
- 2.0.0 — 2019-05-15
- 1.2.2 — 2019-01-01
- 1.2.1 — 2018-05-16
- 1.2.0 — 2018-03-04
- 1.1.2 — 2018-03-03
- 1.1.1 — 2017-03-07
- 1.1.0 — 2017-03-05
- … 9 more at https://npm.io/package/serialize-to-js/versions

## README

# serialize-to-js

> serialize objects to javascript

[![NPM version](https://badge.fury.io/js/serialize-to-js.svg)](https://www.npmjs.com/package/serialize-to-js/)
[![Build Status](https://github.com/commenthol/serialize-to-js/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/commenthol/serialize-to-js/actions/workflows/ci.yml?query=branch%3Amaster)


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

<!-- !toc (minlevel=2 omit="Table of Contents") -->

* [Methods](#methods)
  * [serialize](#serialize)
* [Contribution and License Agreement](#contribution-and-license-agreement)
* [License](#license)

<!-- toc! -->

## Methods

### serialize

`serialize(source, opts, opts.ignoreCircular, opts.reference)`

serializes an object to javascript

#### Example - serializing regex, date, buffer, ...

```js
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))
//> '{str: "\u003Cscript\u003Evar a = 0 \u003E 1\u003C\u002Fscript\u003E",
//>   num: 3.1415, bool: true, nil: null, undef: undefined,
//>   obj: {foo: "bar"}, arr: [1, "2"], regexp: new RegExp("^test?$", ""),
//>   date: new Date("2019-12-29T10:37:36.613Z"),
//>   buffer: Buffer.from("ZGF0YQ==", "base64"), set: new Set([1, 2, 3]),
//>   map: new Map([["a", 1], ["b", 2]])}'
```

#### Example - serializing while respecting references

```js
var serialize = require('serialize-to-js')
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)  
**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.

[LICENSE]: ./LICENSE

---
_Source: https://npm.io/package/serialize-to-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
