What is superjson?
Superjson is a flexible and powerful library for serializing JavaScript data structures, including those that are not supported by JSON, such as Dates, RegExps, Maps, Sets, and more. It allows for seamless serialization and deserialization of complex data types, making it easier to work with complex data in applications that require data persistence or transmission.
What are superjson's main functionalities?
Serialization of complex types
Superjson can serialize complex JavaScript types like Dates, Maps, and Sets into a string representation that can be easily transmitted or stored. Upon deserialization, these strings are converted back into their original complex types.
{"date": "2023-04-14T12:00:00.000Z", "map": "[[\"key1\",\"value1\"],[\"key2\",\"value2\"]]", "set": "[\"a\",\"b\",\"c\"]"}
Custom serialization
Developers can define custom serialization and deserialization logic for unsupported types or to override the default behavior for supported types. This feature enhances flexibility and control over how data is serialized.
{"customType": "SerializedValue"}
Other packages similar to superjson
flatted
Flatted is a package that offers serialization and deserialization of complex JavaScript objects, including nested objects and circular references, using a flat structure. Compared to Superjson, Flatted focuses on solving circular reference issues but does not explicitly handle a wide range of JavaScript types like Superjson does.
serialize-javascript
Serialize-javascript provides serialization of JavaScript objects into a string, including regular expressions and functions. It is similar to Superjson in its ability to handle non-standard JSON data types, but it is more focused on including functions in the serialization process, which Superjson does not inherently support.
superjson
Extends JSON.stringify and JSON.parse to support additional JS types (Dates, RegExps, Functions, etc)
Installation
npm install superjson
Goal
The goal of this project is to properly serialize and deserialize JSON structures. It'd be nice if this
was as fast as possible, but consistent data types in and out is more important. If it's impossible
to consistently serialize or deserialize an object, the library should throw.
Example
var json = require('superjson');
var str = json.stringify({
date: new Date(),
regexp: /[abc]d/,
fn: function foo() {}
})
var obj = json.parse(str)
{
date: date,
regexp: /[abc]d/,
fn: function foo() {}
}
License
(The MIT License)
Copyright (c) 2014 Matthew Mueller <matt@lapwinglabs.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.