---
url: https://node-ts.github.io/bus/api/bus-core/interfaces/Serializer.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / Serializer

# Interface: Serializer

Defined in: [packages/bus-core/src/serialization/serializer.ts:10](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/serialization/serializer.ts#L10)

A serializer that's use to serialize/deserialize objects as they leave and enter the application boundary.

A serializer holds no state of its own bus, so one instance can be shared by several buses. Each bus passes
the message types it was configured with, using `withMessageTypes()`, to every call that restores an object.

## Methods

### deserialize()

```ts
deserialize<T>(
   val, 
   classType, 
   messageTypes?
): T;
```

Defined in: [packages/bus-core/src/serialization/serializer.ts:26](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/serialization/serializer.ts#L26)

Reads an object written by `serialize`

#### Type Parameters

| Type Parameter |
| ------ |
| `T` *extends* `object` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `val` | `string` | the serialized object |
| `classType` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<`T`> | the class the top-level object is created from |
| `messageTypes?` | [`MessageTypes`](../../bus-messages/interfaces/MessageTypes.md) | the message types of the bus that's reading the object, which say how to restore its nested values |

#### Returns

`T`

the object

***

### serialize()

```ts
serialize<T>(obj): string;
```

Defined in: [packages/bus-core/src/serialization/serializer.ts:16](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/serialization/serializer.ts#L16)

Writes an object as a string

#### Type Parameters

| Type Parameter |
| ------ |
| `T` *extends* `object` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `obj` | `T` | the message or workflow state to write |

#### Returns

`string`

the serialized object

***

### toClass()

```ts
toClass<T>(
   obj, 
   classConstructor, 
   messageTypes?
): T;
```

Defined in: [packages/bus-core/src/serialization/serializer.ts:47](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/serialization/serializer.ts#L47)

Converts a plain object, such as one returned by `toPlain`, back to its class

#### Type Parameters

| Type Parameter |
| ------ |
| `T` *extends* `object` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `obj` | `object` | the plain object |
| `classConstructor` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<`T`> | the class the top-level object is created from |
| `messageTypes?` | [`MessageTypes`](../../bus-messages/interfaces/MessageTypes.md) | the message types of the bus that's reading the object, which say how to restore its nested values |

#### Returns

`T`

the object

***

### toPlain()

```ts
toPlain<T>(obj): object;
```

Defined in: [packages/bus-core/src/serialization/serializer.ts:37](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/serialization/serializer.ts#L37)

Converts an object to a plain object that JSON can represent

#### Type Parameters

| Type Parameter |
| ------ |
| `T` *extends* `object` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `obj` | `T` | the message or workflow state to convert |

#### Returns

`object`

the plain object
