{"library":"serialize-javascript","title":"Serialize JavaScript Values","description":"serialize-javascript is a utility library designed to convert JavaScript values, including complex types like functions, regular expressions, dates, Maps, Sets, BigInt, and URLs, into a string representation that is a superset of JSON. This serialized string is valid literal JavaScript code, suitable for embedding directly into HTML `<script>` tags or saving as `.js` files. Unlike `JSON.stringify()`, it gracefully handles these non-JSON-native types and automatically escapes HTML characters and JavaScript line terminators to prevent Cross-Site Scripting (XSS) vulnerabilities when embedded in HTML. The package is actively maintained, with the current stable version being 7.0.5, and typically sees regular maintenance updates and major version releases as needed. It originated as an internal module for `express-state` before becoming an independent npm package.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install serialize-javascript"],"cli":null},"imports":["import serialize from 'serialize-javascript';","const serialize = require('serialize-javascript');","import serialize from 'serialize-javascript';\n// ... later usage ...\nconst output = serialize(myObject, { unsafe: true });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import serialize from 'serialize-javascript';\n\nconst dataToSerialize = {\n    str  : 'hello world <script>',\n    num  : 123.45,\n    obj  : { key: 'value', nested: { foo: 'bar' } },\n    arr  : [1, null, new Date(), /test/gi],\n    bool : false,\n    nil  : null,\n    undef: undefined,\n    inf  : Infinity,\n    date : new Date('2023-10-27T10:00:00Z'),\n    map  : new Map([['id', 1], ['name', 'Example']]),\n    set  : new Set([10, 20, 30]),\n    fn   : function greet(name) { return `Hello, ${name}!`; },\n    re   : /^user_\\d+$/i,\n    big  : BigInt(9007199254740991n),\n    url  : new URL('https://example.com/path?query=param&id=123'),\n    nestedFunc: { action: () => console.log('This will be serialized') }\n};\n\n// Serialize with default options (pretty print with 2 spaces)\nconst serializedData = serialize(dataToSerialize, { space: 2 });\nconsole.log('Serialized Data:\\n', serializedData);\n\n// Example of deserialization (requires eval, use with caution on untrusted input)\n// In a real application, you would typically embed this in a script tag\n// or use it in a server-side rendering context where the source is trusted.\ntry {\n  const deserializedData = eval('(' + serializedData + ')');\n  console.log('\\nDeserialized Function Output:', deserializedData.fn('Registry'));\n  console.log('Deserialized Regex Test:', deserializedData.re.test('user_123'));\n} catch (e) {\n  console.error('\\nError during deserialization:', e.message);\n}\n","lang":"javascript","description":"This quickstart demonstrates how to serialize a diverse JavaScript object, including functions, regular expressions, dates, Maps, Sets, BigInt, and URLs, into a JavaScript string. It also shows the automatic HTML character escaping and an example of how the serialized string could be evaluated (with caution) back into an object, illustrating the execution of serialized functions and regexes.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}