{"library":"serialize-to-js","title":"Serialize JavaScript Objects","description":"serialize-to-js is a utility library for converting JavaScript objects into a string representation that can be safely evaluated as JavaScript code. Unlike `JSON.stringify`, it supports a wider range of JavaScript types including `RegExp`, `Date`, `Buffer`, `Set`, `Map`, `Error`, and various `TypedArray` types, while also handling circular references. The current stable version is 3.1.2. The library primarily focuses on robust serialization to executable JavaScript strings and has undergone breaking changes to enhance security, notably by removing the `deserialize` function in v2.0.0 due to Denial-of-Service vulnerabilities. It is particularly useful for scenarios requiring the exact re-creation of JavaScript objects, including their methods and non-primitive types, in environments where `eval` can be controlled.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install serialize-to-js"],"cli":null},"imports":["import serialize from 'serialize-to-js';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import serialize from 'serialize-to-js';\n\nconst obj = {\n  str: '<script>var a = 0 > 1</script>',\n  num: 3.1415,\n  bool: true,\n  nil: null,\n  undef: undefined,\n  obj: { foo: 'bar' },\n  arr: [1, '2'],\n  regexp: /^test?$/,\n  date: new Date('2023-01-15T10:00:00.000Z'), // Consistent date for example\n  buffer: Buffer.from('data'), // Requires Node.js Buffer or polyfill\n  set: new Set([1, 2, 3]),\n  map: new Map([['a', 1], ['b', 2]])\n};\n\nconst serializedString = serialize(obj);\nconsole.log(serializedString);\n\n// To deserialize, one might use eval() in a controlled environment\n// const deserializedObj = eval(`(${serializedString})`);\n// console.log(deserializedObj.date instanceof Date); // true\n","lang":"javascript","description":"This quickstart demonstrates how to serialize a complex JavaScript object, including various primitive types, objects, arrays, regular expressions, dates, buffers, sets, and maps, into a JavaScript string. It shows the output format and hints at how to (cautiously) deserialize it.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}