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

class-transformer

Package Overview
Dependencies
Maintainers
3
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-transformer

Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors

  • 0.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created

What is class-transformer?

The class-transformer package allows developers to transform plain object to class instances and vice versa. It also provides ways to serialize and deserialize object based on class definitions and handle custom transformations.

What are class-transformer's main functionalities?

Plain to class transformation

Transforms a plain object to an instance of the class, allowing for easier manipulation and access to class methods.

import { plainToClass } from 'class-transformer';
class User {
  id: number;
  firstName: string;
  lastName: string;
}
const plain = { id: 1, firstName: 'John', lastName: 'Doe' };
const userInstance = plainToClass(User, plain);

Class to plain transformation

Transforms a class instance back into a plain object, which is useful for preparing objects for storage or transfer.

import { classToPlain } from 'class-transformer';
class User {
  id: number;
  firstName: string;
  lastName: string;
}
const userInstance = new User();
userInstance.id = 1;
userInstance.firstName = 'John';
userInstance.lastName = 'Doe';
const plain = classToPlain(userInstance);

Custom transformation

Allows for custom transformations using decorators to apply specific logic during the transformation process.

import { Transform, plainToClass } from 'class-transformer';
class User {
  id: number;
  @Transform(({ value }) => value.toUpperCase())
  firstName: string;
}
const plain = { id: 1, firstName: 'John' };
const userInstance = plainToClass(User, plain);

Serialization and deserialization

Provides methods to serialize a class instance to JSON and deserialize JSON back to a class instance.

import { serialize, deserialize, Deserialize } from 'class-transformer';
class User {
  id: number;
  firstName: string;
  lastName: string;
}
const userInstance = new User();
userInstance.id = 1;
userInstance.firstName = 'John';
userInstance.lastName = 'Doe';
const json = serialize(userInstance);
const deserializedUser = deserialize(User, json);

Other packages similar to class-transformer

FAQs

Package last updated on 22 Nov 2021

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