What is typeson?
Typeson is a JavaScript library that allows you to serialize and deserialize complex data types to and from JSON. It extends the capabilities of JSON.stringify and JSON.parse to handle more complex data structures such as Dates, Maps, Sets, and more.
What are typeson's main functionalities?
Serialization and Deserialization
Typeson allows you to serialize and deserialize complex data types like Date objects. The example shows how to serialize an object containing a Date and then deserialize it back to its original form.
const Typeson = require('typeson');
const typeson = new Typeson();
const obj = { date: new Date() };
const serialized = typeson.stringify(obj);
const deserialized = typeson.parse(serialized);
console.log(serialized); // Output: {"date":"2023-10-05T14:48:00.000Z"}
console.log(deserialized); // Output: { date: 2023-10-05T14:48:00.000Z }
Custom Type Handlers
Typeson allows you to define custom type handlers for serialization and deserialization. The example demonstrates how to handle RegExp objects by defining custom test, replace, and revive functions.
const Typeson = require('typeson');
const typeson = new Typeson();
// Define a custom type handler for RegExp
const regExpType = {
test: x => x instanceof RegExp,
replace: x => x.toString(),
revive: x => new RegExp(x.slice(1, x.lastIndexOf('/')), x.slice(x.lastIndexOf('/') + 1))
};
// Register the custom type handler
typeson.register({ RegExp: regExpType });
const obj = { pattern: /abc/i };
const serialized = typeson.stringify(obj);
const deserialized = typeson.parse(serialized);
console.log(serialized); // Output: {"pattern":"/abc/i"}
console.log(deserialized); // Output: { pattern: /abc/i }
Handling Circular References
Typeson can handle circular references in objects. The example shows how to serialize and deserialize an object with a circular reference using Typeson's circular preset.
const Typeson = require('typeson');
const typeson = new Typeson.Typeson().register(Typeson.presets.circular);
const obj = {};
obj.self = obj;
const serialized = typeson.stringify(obj);
const deserialized = typeson.parse(serialized);
console.log(serialized); // Output: {"self":"~"}
console.log(deserialized); // Output: { self: [Circular] }
Other packages similar to typeson
flatted
Flatted is a lightweight library for serializing and deserializing JavaScript objects with circular references. Unlike Typeson, Flatted focuses specifically on handling circular references and does not support custom type handlers or complex data types like Dates or Maps.
circular-json
CircularJSON is another library for serializing and deserializing objects with circular references. It is similar to Flatted but has a different API. CircularJSON also does not support custom type handlers or complex data types, making it less versatile than Typeson.
superjson
SuperJSON is a library that extends JSON to support more complex data types like Dates, Maps, and Sets. It is similar to Typeson in its ability to handle complex data structures, but it does not offer the same level of customization for defining custom type handlers.