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

# Class: JsonSerializer

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

The default serializer. Messages and workflow state are written as plain JSON, with Maps as
objects, Sets as arrays and bigints as strings, so nothing is added to the payload.

When reading, the top-level object is created from its class' prototype, without running its
constructor. Dates, Maps, Sets, bigints and class instances are restored at any depth using the message
types the reading bus passes, which are the files generated by `bus generate-message-types` (in
`@node-ts/bus-cli`) given to `withMessageTypes()`. Nested values of other messages stay as JSON parsed
them, e.g. Dates stay ISO strings.

It holds no state, so one instance can be shared by several buses.

## Implements

* [`Serializer`](../interfaces/Serializer.md)

## Constructors

### Constructor

```ts
new JsonSerializer(): JsonSerializer;
```

#### Returns

`JsonSerializer`

## Methods

### deserialize()

```ts
deserialize<ObjectType>(
   serialized, 
   classConstructor, 
   messageTypes?
): ObjectType;
```

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

Reads an object written by `serialize`

#### Type Parameters

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

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `serialized` | `string` | the serialized object |
| `classConstructor` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<`ObjectType`> | 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

`ObjectType`

the object

#### Implementation of

[`Serializer`](../interfaces/Serializer.md).[`deserialize`](../interfaces/Serializer.md#deserialize)

***

### serialize()

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

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

Writes an object as a string

#### Type Parameters

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

#### Parameters

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

#### Returns

`string`

the serialized object

#### Implementation of

[`Serializer`](../interfaces/Serializer.md).[`serialize`](../interfaces/Serializer.md#serialize)

***

### toClass()

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

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

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

#### Type Parameters

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

#### Parameters

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

#### Returns

`T`

the object

#### Implementation of

[`Serializer`](../interfaces/Serializer.md).[`toClass`](../interfaces/Serializer.md#toclass)

***

### toPlain()

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

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

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

#### Implementation of

[`Serializer`](../interfaces/Serializer.md).[`toPlain`](../interfaces/Serializer.md#toplain)
