Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typeson

Package Overview
Dependencies
Maintainers
2
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeson

Preserves types over JSON, BSON or socket.io

  • 5.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
256K
increased by16.65%
Maintainers
2
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 06 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc