Serializers
@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
1
import { Command } from '@node-ts/bus-messages'
2
​
3
export class RegisterUser extends Command {
4
constructor (
5
readonly dateOfBirth: Date
6
) {}
7
}
Copied!
If this is run through the default serializer the effect will be the same as:
1
const command = new RegisterUser(new Date())
2
const plainCommand = JSON.parse(JSON.stringify(command))
3
plainCommand.getDate() // ERROR - getDate() does not exist on plainCommand
Copied!
The default serializer is not recommended for serious projects given these limitations, and class serializer should be used where possible.
Copy link