Skip to content

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 ​

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

Reads an object written by serialize

Type Parameters ​

Type Parameter
ObjectType extends object

Parameters ​

ParameterTypeDescription
serializedstringthe serialized object
classConstructorClassConstructor<ObjectType>the class the top-level object is created from
messageTypes?MessageTypesthe 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.deserialize


serialize() ​

ts
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 ​

ParameterTypeDescription
objObjectTypethe message or workflow state to write

Returns ​

string

the serialized object

Implementation of ​

Serializer.serialize


toClass() ​

ts
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 ​

ParameterTypeDefault valueDescription
objobjectundefinedthe plain object
classConstructorClassConstructor<T>undefinedthe class the top-level object is created from
messageTypesMessageTypesNO_MESSAGE_TYPESthe 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.toClass


toPlain() ​

ts
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 ​

ParameterTypeDescription
objTthe message or workflow state to convert

Returns ​

object

the plain object

Implementation of ​

Serializer.toPlain

Released under the MIT License.