Skip to content

API reference / @node-ts/bus-core / MessageSerializer

Class: MessageSerializer ​

Defined in: packages/bus-core/src/serialization/message-serializer.ts:16

This a wrapper around the real serializer. Unlike JsonSerializer, whose sole job is parsing data, this class will do some plumbing work to look up the Handler Registry for the message constructor.

Normally, transports will use this instead of the real serializer. Each bus has its own, which restores messages with that bus' handlers and message types.

Constructors ​

Constructor ​

ts
new MessageSerializer(
   serializer, 
   handlerRegistry, 
   messageTypes
): MessageSerializer;

Defined in: packages/bus-core/src/serialization/message-serializer.ts:22

Parameters ​

ParameterTypeDescription
serializerSerializerthe serializer the bus is configured with
handlerRegistryHandlerRegistrythe handlers of the bus, which give the class of each message
messageTypesMessageTypesthe message types the bus is configured with

Returns ​

MessageSerializer

Methods ​

deserialize() ​

ts
deserialize<MessageType>(serializedMessage): MessageType;

Defined in: packages/bus-core/src/serialization/message-serializer.ts:41

Parses a message. A message that has a handler registered is created from its message class, or as a plain object for a message declared with defineCommand or defineEvent. With the default JsonSerializer, a message without one, such as one handled by a custom handler, is still restored as a plain object when the bus' message types have it, e.g. for a message declared as an interface. A custom serializer only gets the messages that have a handler, since it may not use message types.

Type Parameters ​

Type Parameter
MessageType extends Message

Parameters ​

ParameterTypeDescription
serializedMessagestringthe message as it was received

Returns ​

MessageType

the message


serialize() ​

ts
serialize<MessageType>(message): string;

Defined in: packages/bus-core/src/serialization/message-serializer.ts:28

Type Parameters ​

Type Parameter
MessageType extends Message

Parameters ​

ParameterType
messageMessageType

Returns ​

string

Released under the MIT License.