API reference / @node-ts/bus-core / JsonSerializer
Class: JsonSerializer
Defined in: packages/bus-core/src/serialization/json-serializer.ts:40
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
Constructors
Constructor
new JsonSerializer(): JsonSerializer;Returns
JsonSerializer
Methods
deserialize()
deserialize<ObjectType>(
serialized,
classConstructor,
messageTypes?
): ObjectType;Defined in: packages/bus-core/src/serialization/json-serializer.ts:47
Reads an object written by serialize
Type Parameters
| Type Parameter |
|---|
ObjectType extends object |
Parameters
| Parameter | Type | Description |
|---|---|---|
serialized | string | the serialized object |
classConstructor | ClassConstructor<ObjectType> | the class the top-level object is created from |
messageTypes? | MessageTypes | 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
serialize()
serialize<ObjectType>(obj): string;Defined in: packages/bus-core/src/serialization/json-serializer.ts:43
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
toClass()
toClass<T>(
obj,
classConstructor,
messageTypes?
): T;Defined in: packages/bus-core/src/serialization/json-serializer.ts:60
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<T> | undefined | the class the top-level object is created from |
messageTypes | MessageTypes | 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
toPlain()
toPlain<T>(obj): object;Defined in: packages/bus-core/src/serialization/json-serializer.ts:56
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