universal-serialize is a JavaScript utility for serializing and deserializing complex objects, including built-in types like `Date`, `Error`, and `RegExp` that standard `JSON.stringify`/`parse` do not handle natively. It also provides a mechanism for developers to define custom serialization and deserialization logic for any bespoke type. The package is currently at version 1.0.10, with its last known update several years ago, indicating it is likely in an abandoned state and not actively maintained or receiving new features/security patches. Its key differentiators include out-of-the-box support for common non-primitive types and an extensible API for custom types, making it more robust than simple JSON operations for certain use cases.
npm install universal-serializeVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic serialization and deserialization of an object containing strings, dates, errors, and regular expressions, showing how `universal-serialize` preserves their types.
Consider migrating to a actively maintained serialization library that supports modern JavaScript features and provides ongoing security support. For simple cases, `JSON.stringify`/`parse` with custom `replacer`/`reviver` functions can suffice.
Avoid `eval()` for deserializing executable code. If functions must be transmitted, consider safer alternatives like serializing ASTs or limiting functions to a pre-defined whitelist of known, safe operations. Re-evaluate if deserializing functions directly is a necessary requirement.
Ensure objects to be serialized are free of circular references. If circular references are unavoidable, pre-process the object to break cycles (e.g., replacing references with placeholders) or use a serializer specifically designed to handle them.
Implement custom serialization and deserialization handlers for any modern JavaScript types or custom classes that need to be preserved. For `BigInt`, ensure it's converted to a string before serialization and parsed back on deserialization. For `Map`/`Set`, convert to arrays/objects.
Ensure the input to `deserialize()` is a valid JSON string. Check that the string was correctly produced by `serialize()` and not corrupted or truncated. Use a JSON linter to validate the string if necessary.
While `universal-serialize` claims to handle `Error` objects, ensure that the deserialized `error` property is correctly re-instantiated as an `Error` object. If custom serialization was used, verify the `deserialize` handler correctly reconstructs the `Error` instance.
Add `import { TYPE } from 'universal-serialize';` to the top of your file where `TYPE` is used.No dependency data recorded yet.