📘
@node-test/bus
  • @node-ts/bus
  • Getting started
    • Installation
    • Handling messages
    • Shutting down cleanly
  • Reference
    • Bus
    • BusConfiguration
    • BusInstance
  • Getting help
  • Guide
    • Messages
      • Events
      • Commands
      • System messages
    • Message attributes
      • Correlation id
      • Attributes
      • Sticky attributes
    • Workflows
      • Creating a workflow
      • Starting
      • Handling
      • State
      • Completing
      • Example
    • Transports
      • RabbitMQ
      • Amazon SQS
      • Redis
      • Custom transports
    • Persistence
      • Postgres
      • MongoDB
      • Creating a persistence
    • Serializers
      • Class serializer
    • Loggers
      • Custom loggers
    • Middleware
    • Lifecycle hooks
    • Retry Strategies
    • Dependency injection
    • Long running processes
Powered by GitBook
On this page

Was this helpful?

  1. Guide

Serializers

PreviousCreating a persistenceNextClass serializer

Last updated 3 years ago

Was this helpful?

@node-ts/bus-core by default uses a naive serializer that leverages the built-in JSON.parse() and JSON.stringify() functions. This is used to convert messages to a serialized form when publishing them to the transport, and then deserializing them when they're read from the transport.

While this is fine for the simplest of cases, it suffers from the normal problems of deserializing into strong types.

For example consider the following message

import { Command } from '@node-ts/bus-messages'

export class RegisterUser extends Command {
  constructor (
    readonly dateOfBirth: Date
  ) {}
}

If this is run through the default serializer the effect will be the same as:

const command = new RegisterUser(new Date())
const plainCommand = JSON.parse(JSON.stringify(command))
plainCommand.getDate() // ERROR - getDate() does not exist on plainCommand

The default serializer is not recommended for serious projects given these limitations, and should be used where possible.

class serializer