Marshalling
Lightbus has four stages of data marshalling:
- Encode / Decode
- Serialize / Deserialize
- Validation
- Deform / Cast
An inbound message will go through this process from top to bottom. An outbound message will go through this process from bottom to top.
Inbound flow¶
Messages arriving from the bus go through the following stages in order to prepare the data for use:
- Decode: Decode the incoming data (JSON decoding by default)
- Deserialise: Convert decoded data into a
Message
object - Validate: Validate the incoming message against the JSON schema available on the bus.
- Cast: Best effort casting of parameters/results based on
the locally available type hinting. This can be disabled with the
cast_values
configuration option.
Outbound flow¶
This is the reverse of the inbound flow. Messages being sent will go through the following process in order to prepare the data for transmission on bus:
- Deform: Lightbus handles NamedTuples, dataclasses and other classes by converting them into dictionaries. Other common types such as datetimes, Decimals etc are converted into strings. Internally this is referred to as the deform process and is the inverse of the cast process.
- Validate: Validate the outgoing message against the JSON schema available on the bus.
- Serialize: Structures the data in a way suitable for the transport.
- Encode: Converts the data to a form suitable for transmission. This typically means stringifying it, for which lightbus uses JSON encoding by default.
About casting¶
Casting is separate from validation, although both rely on type hints. Whereas validation uses a shared bus-wide schema to check data validity, casting uses any Python type hints available in the local codebase to marshall event and RPC parameters into a format useful to the service's developer.