# Class serializer

**@node-ts/bus-class-serializer** leverages [class transformer](https://www.npmjs.com/package/class-transformer) to provide serialization/deserialization to class instances. This means that class based properties of messages like javascript Dates can be consumed directly without having to do additional custom deserialization in each handler.

### Installation

Install **@node-ts/bus-class-serializer** and its dependencies

```bash
npm install @node-ts/bus-class-serializer reflect-metadata class-transformer
```

Configure bus to use the serializer

```typescript
import { Bus } from '@node-ts/bus-core'
import { ClassSerializer } from '@node-ts/bus-class-serializer'

Bus
  .configure()
  .withSerializer(new ClassSerializer())
  .initialize()
```

{% hint style="info" %}
This package relies on [class transformer](https://www.npmjs.com/package/class-transformer) that requires [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) to be installed and called at the start of your application before any other imports. Please follow their guides on how to configure your app and contracts to serialize correctly.
{% endhint %}

### Strongly typed messages

Once the **@node-ts/bus-class-serializer** has been installed and configured, you can consume class transformer notation for object properties of your messages.&#x20;

For example

```typescript
import { Command } from '@node-ts/bus-messages'
import { Type } from 'class-transformer'

class Update extends Command {
  // Provide a @Type hint so that class-transformer can deserialize this at runtime
  @Type(() => Date) readonly date: Date
  constructor (
    date: Date
  ) {
    this.date = date
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bus.node-ts.com/guide/serializers/class-serializer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
